Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(i18n): annotate language names with their English equivalent in dev
Elian Doran9e3b0472f3fc
diff --git a/apps/client/src/services/i18n.spec.ts b/apps/client/src/services/i18n.spec.tsindex 8749c427d3..ac4e8e5ef5 100644--- a/apps/client/src/services/i18n.spec.ts+++ b/apps/client/src/services/i18n.spec.ts@@ -2,7 +2,7 @@ import { MESSAGE_KEY_PREFIX, MESSAGE_OVERRIDES, slugify } from "@triliumnext/ckeimport { dayjs, findDuplicateJsonKeys, findPluralKeyConflicts, LOCALES } from "@triliumnext/commons";import { readdirSync, readFileSync } from "fs";import { join } from "path";-import { describe, expect, it, vi } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";// Mock the http backend so i18next.init() never hits the network. The real// backend would try to fetch translation JSON over HTTP and the awaited init()@@ -222,9 +222,49 @@ describe("i18n", () => {});describe("getAvailableLocales", () => {+ function setDevBuild(isDev: boolean) {+ (window as unknown as { glob: { isDev: boolean } }).glob.isDev = isDev;+ }++ afterEach(() => setDevBuild(false));+it("returns the full LOCALES list", () => {expect(getAvailableLocales()).toBe(LOCALES);});++ it("annotates the names with their English equivalent in a development build", () => {+ setDevBuild(true);+ const names = Object.fromEntries(getAvailableLocales().map((l) => [ l.id, l.name ]));++ expect(names).toMatchObject({+ de: "Deutsch (German)",+ ko: "한국어 (Korean)",+ pt_br: "Português (Brasil) (Brazilian Portuguese)",+ // Resolved through `zh-Hans`/`zh-Hant`, so the script — the thing that separates the+ // two entries — is what the annotation states.+ cn: "简体中文 (Simplified Chinese)",+ tw: "繁體中文 (Traditional Chinese)"+ });+ });++ it("leaves the English entries alone, including the malformed dev-only tag", () => {+ setDevBuild(true);+ const names = Object.fromEntries(getAvailableLocales().map((l) => [ l.id, l.name ]));++ expect(names).toMatchObject({+ en: "English (United States)",+ "en-GB": "English (United Kingdom)",+ en_rtl: "English RTL"+ });+ });++ it("does not mutate the shared LOCALES entries", () => {+ const before = LOCALES.map((l) => l.name);+ setDevBuild(true);+ getAvailableLocales();++ expect(LOCALES.map((l) => l.name)).toEqual(before);+ });});describe("getLocaleById", () => {diff --git a/apps/client/src/services/i18n.ts b/apps/client/src/services/i18n.tsindex 0743596731..856ee7a04f 100644--- a/apps/client/src/services/i18n.ts+++ b/apps/client/src/services/i18n.ts@@ -1,4 +1,4 @@-import { LOCALE_IDS, LOCALES, setDayjsLocale } from "@triliumnext/commons";+import { type Locale, LOCALE_IDS, LOCALES, setDayjsLocale } from "@triliumnext/commons";import i18next from "i18next";import i18nextHttpBackend from "i18next-http-backend";import { initReactI18next } from "react-i18next";@@ -24,8 +24,20 @@ export async function initLocale(locale: LOCALE_IDS = "en") {translationsInitializedPromise.resolve();}-export function getAvailableLocales() {- return LOCALES;+/**+ * The locales offered everywhere a language is picked: the display and formatting language, the+ * enabled content languages and a note's own language.+ *+ * A development build annotates each name with its English equivalent — `Deutsch (German)` — because+ * the list is written in each language's own script, and a developer checking how the app behaves in+ * one of them cannot otherwise tell `한국어` from `हिन्दी`. Production keeps the native names alone,+ * which is what a speaker looking for their own language expects to find.+ */+export function getAvailableLocales(): Locale[] {+ if (!window.glob.isDev) return LOCALES;++ const englishNames = new Intl.DisplayNames([ "en" ], { type: "language" });+ return LOCALES.map((locale) => ({ ...locale, name: withEnglishName(locale, englishNames) }));}/**@@ -41,3 +53,42 @@ export function getLocaleById(localeId: string | null | undefined) {export const t = i18next.t;export const getCurrentLanguage = () => i18next.language;++/** `Deutsch (German)`, or the native name alone where an English equivalent would add nothing. */+function withEnglishName(locale: Locale, englishNames: Intl.DisplayNames): string {+ const englishName = getEnglishName(locale.id, englishNames);+ return englishName && englishName !== locale.name+ ? `${locale.name} (${englishName})`+ : locale.name;+}++/**+ * The English name of a locale, or `null` when there is none worth showing: the English entries are+ * named in English already, and anything `Intl` does not recognize has nothing to offer.+ */+function getEnglishName(localeId: string, englishNames: Intl.DisplayNames): string | null {+ const tag = ENGLISH_NAME_TAGS[localeId] ?? localeId.replaceAll("_", "-");++ // Covers `en_rtl` too, which is not even a well-formed tag — `Intl` would throw on it.+ if (tag === "en" || tag.startsWith("en-")) return null;++ try {+ return englishNames.of(tag) ?? null;+ } catch {+ // A malformed tag is a RangeError rather than an empty result.+ return null;+ }+}++/**+ * Locale ids that are not BCP-47 tags, mapped to one.+ *+ * The Chinese pair deliberately resolves through the script subtags rather than the regions+ * `normalizeLocale` (in `utils/formatters`) maps them to: `zh-Hans` reads as "Simplified Chinese",+ * whereas `zh-CN` would say "Chinese (China)" — the wrong distinction for entries differing by+ * script.+ */+const ENGLISH_NAME_TAGS: Record<string, string> = {+ cn: "zh-Hans",+ tw: "zh-Hant"+};