|
1 | 1 | import { withPluginApi } from "discourse/lib/plugin-api"; |
2 | | -import { i18n } from "discourse-i18n"; |
3 | | -import { themePrefix } from "virtual:theme"; |
| 2 | +import I18n from "I18n"; |
4 | 3 |
|
5 | 4 | /** |
6 | | - * Resolve a localized field for a nav link. |
7 | | - * |
8 | | - * Priority: |
9 | | - * 1. Theme translations (locales/*.yml via i18n/themePrefix) — for default |
10 | | - * links that have keys defined in locale files. |
11 | | - * 2. Settings translations (nested translations objects) — for custom links |
12 | | - * where admins add per-locale overrides inside the settings editor. |
13 | | - * 3. Raw settings value — the base display_name/title from the link. |
| 5 | + * Resolve a field value from a nav link, checking for a locale-specific |
| 6 | + * translation first. Falls back to the base field value. |
14 | 7 | */ |
15 | 8 | function getLocalizedField(link, fieldName) { |
16 | | - // 1. Try theme translations (locale files) |
17 | | - const key = link.display_name.replace(/\s+/g, "_").toLowerCase(); |
18 | | - const translationKey = themePrefix(`nav_links.${key}.${fieldName}`); |
19 | | - const themeTranslated = i18n(translationKey); |
20 | | - |
21 | | - if (themeTranslated !== translationKey) { |
22 | | - return themeTranslated; |
23 | | - } |
24 | | - |
25 | | - // 2. Try settings translations (nested objects) |
26 | 9 | const translations = link.translations; |
27 | | - if (translations && translations.length > 0) { |
28 | | - const locale = i18n.currentLocale?.() || i18n.locale || ""; |
| 10 | + if (!translations || translations.length === 0) { |
| 11 | + return link[fieldName]; |
| 12 | + } |
29 | 13 |
|
30 | | - // Exact match first (e.g. "pt_BR"), then prefix match (e.g. "pt") |
31 | | - const match = |
32 | | - translations.find((t) => t.locale === locale) || |
33 | | - translations.find((t) => locale.startsWith(t.locale)); |
| 14 | + const locale = I18n.currentLocale(); |
34 | 15 |
|
35 | | - if (match && match[fieldName]) { |
36 | | - return match[fieldName]; |
37 | | - } |
38 | | - } |
| 16 | + // Try exact match first (e.g. "pt_BR"), then prefix match (e.g. "pt") |
| 17 | + const match = |
| 18 | + translations.find((t) => t.locale === locale) || |
| 19 | + translations.find((t) => locale.startsWith(t.locale)); |
39 | 20 |
|
40 | | - // 3. Fallback to raw settings value |
41 | | - return link[fieldName]; |
| 21 | + return (match && match[fieldName]) || link[fieldName]; |
42 | 22 | } |
43 | 23 |
|
44 | 24 | // Built-in Discourse filter routes that have associated topic counts |
|
0 commit comments