Skip to content

Commit 0661c62

Browse files
HoneyxiliaLena Montenotjordanvidrine
authored
Add a locale filter to links (#30)
* Add a locale attribute to custom header links This attribute is to be set to a language code like 'fr', 'en' or 'de'. * Filter out links which locale is different from the html document's * prettier * more syntax --------- Co-authored-by: Lena Montenot <lena.montenot@commown.coop> Co-authored-by: Jordan Vidrine <jordan@jordanvidrine.com>
1 parent be4c69f commit 0661c62

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

javascripts/discourse/initializers/discourse-custom-header-links.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ export default {
2424
.split("|")
2525
.filter(Boolean)
2626
.map((customHeaderLinksArray) => {
27-
const [linkText, linkTitle, linkHref, device, target, keepOnScroll] =
28-
customHeaderLinksArray
29-
.split(",")
30-
.filter(Boolean)
31-
.map((x) => x.trim());
27+
const [
28+
linkText,
29+
linkTitle,
30+
linkHref,
31+
device,
32+
target,
33+
keepOnScroll,
34+
locale,
35+
] = customHeaderLinksArray
36+
.split(",")
37+
.filter(Boolean)
38+
.map((x) => x.trim());
3239

3340
const deviceClass = `.${device}`;
3441
const linkTarget = target === "self" ? "" : "_blank";
@@ -37,6 +44,8 @@ export default {
3744
.toLowerCase()
3845
.replace(/\s/gi, "-")}-custom-header-links`;
3946

47+
const localeClass = locale === "" ? "" : `.${locale}`;
48+
4049
const anchorAttributes = {
4150
title: linkTitle,
4251
href: linkHref,
@@ -45,9 +54,17 @@ export default {
4554
anchorAttributes.target = linkTarget;
4655
}
4756

57+
if (
58+
locale !== "" &&
59+
document.documentElement.lang &&
60+
document.documentElement.lang !== locale
61+
) {
62+
return;
63+
}
64+
4865
headerLinks.push(
4966
h(
50-
`li.headerLink${deviceClass}${keepOnScrollClass}${linkClass}`,
67+
`li.headerLink${deviceClass}${keepOnScrollClass}${localeClass}${linkClass}`,
5168
h("a", anchorAttributes, linkText)
5269
)
5370
);

settings.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Custom_header_links:
22
type: list
33
list_type: simple
4-
default: "External link, this link will open in a new tab, https://meta.discourse.org, vdo, blank, remove|Most Liked, Posts with the most amount of likes, /latest/?order=op_likes, vdo, self, keep|Privacy, Our Privacy Policy, /privacy, vdm, self, keep"
4+
default: "External link, this link will open in a new tab, https://meta.discourse.org, vdo, blank, remove, en|Most Liked, Posts with the most amount of likes, /latest/?order=op_likes, vdo, self, keep, en|Privacy, Our Privacy Policy, /privacy, vdm, self, keep, en"
55
description:
6-
en: "Comma delimited in this order: link text, link title, URL, view, target, hide on scroll<br><b>Link text:</b> The text for the link<br><b>Link title:</b> the text that shows when the link is hovered<br><b>URL:</b> The path for the link (can be relative)<br><b>View:</b> vdm = desktop and mobile, vdo = desktop only, vmo = mobile only<br><b>Target:</b> blank = opens in a new tab, self = opens in the same tab<br><b>Hide on scroll:</b> remove = hides the link when the title is expanded on topic pages keep = keeps the link visible even when the title is visible on topic pages"
6+
en: "Comma delimited in this order: link text, link title, URL, view, target, hide on scroll<br><b>Link text:</b> The text for the link<br><b>Link title:</b> the text that shows when the link is hovered<br><b>URL:</b> The path for the link (can be relative)<br><b>View:</b> vdm = desktop and mobile, vdo = desktop only, vmo = mobile only<br><b>Target:</b> blank = opens in a new tab, self = opens in the same tab<br><b>Hide on scroll:</b> remove = hides the link when the title is expanded on topic pages keep = keeps the link visible even when the title is visible on topic pages<br><b>Language:</b> blank = no locale assoaciated to the link, else insert a locale code (en, fr, de, ...)"
77

88
links_position:
99
default: right

0 commit comments

Comments
 (0)