Skip to content

Commit 697d81a

Browse files
committed
Add per-language translation support for custom nav links
1 parent 22a1303 commit 697d81a

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

javascripts/discourse/initializers/init-nav-bar-additions.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
import { withPluginApi } from "discourse/lib/plugin-api";
2+
import { i18n } from "discourse-i18n";
3+
import { themePrefix } from "virtual:theme";
4+
5+
/**
6+
* Resolve a localized field for a nav link.
7+
* Looks up a translation key based on the link's display_name (e.g.
8+
* "Latest" -> themePrefix("nav_links.latest.display_name")).
9+
* Falls back to the raw settings value if no translation key exists.
10+
*/
11+
function getLocalizedField(link, fieldName) {
12+
const key = link.display_name.replace(/\s+/g, "_").toLowerCase();
13+
const translationKey = themePrefix(`nav_links.${key}.${fieldName}`);
14+
const translated = i18n(translationKey);
15+
16+
// i18n returns the key path when no translation is found
17+
if (translated !== translationKey) {
18+
return translated;
19+
}
20+
21+
return link[fieldName];
22+
}
223

324
// Built-in Discourse filter routes that have associated topic counts
425
const COUNTED_FILTERS = {
@@ -39,15 +60,14 @@ export default {
3960
"service:topic-tracking-state"
4061
);
4162

42-
for (const {
43-
display_name: displayName,
44-
title,
45-
url,
46-
} of settings.nav_links) {
63+
for (const link of settings.nav_links) {
64+
const { display_name: displayName, url } = link;
65+
const localizedDisplayName = getLocalizedField(link, "display_name");
66+
const localizedTitle = getLocalizedField(link, "title");
4767
const filterType = COUNTED_FILTERS[url];
4868
const itemConfig = {
4969
name: `custom_${displayName.replace(/\s+/g, "-").toLowerCase()}`,
50-
title,
70+
title: localizedTitle,
5171
href: url,
5272
forceActive: (category, args, router) =>
5373
router.currentURL?.split("?")[0] === url,
@@ -58,10 +78,12 @@ export default {
5878
// reactively, picking up count changes from TopicTrackingState
5979
itemConfig.displayName = () => {
6080
const count = getCountForFilter(topicTrackingState, filterType);
61-
return count > 0 ? `${displayName} (${count})` : displayName;
81+
return count > 0
82+
? `${localizedDisplayName} (${count})`
83+
: localizedDisplayName;
6284
};
6385
} else {
64-
itemConfig.displayName = displayName;
86+
itemConfig.displayName = localizedDisplayName;
6587
}
6688

6789
api.addNavigationBarItem(itemConfig);

locales/en.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ en:
1414
url:
1515
label: URL
1616
description: The link (e.g., /latest or https://discourse.org)
17+
nav_links:
18+
latest:
19+
display_name: Latest
20+
title: topics with recent posts
21+
categories:
22+
display_name: Categories
23+
title: all topics grouped by category
24+
top:
25+
display_name: Top
26+
title: the most active topics in the last year, month, week or day

0 commit comments

Comments
 (0)