From 2d6d5c4e2b5c6c511b80923673ab54d97cf2e669 Mon Sep 17 00:00:00 2001 From: BlackGownQ <110607665+BlackGownQ@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:21:13 +0800 Subject: [PATCH] feat(i18n): register and document Simplified Chinese --- web/src/i18n/README.md | 29 +++++++++++++++++++++++++++++ web/src/i18n/index.ts | 28 +++++++++++++++++++++------- 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 web/src/i18n/README.md diff --git a/web/src/i18n/README.md b/web/src/i18n/README.md new file mode 100644 index 0000000..b1781bf --- /dev/null +++ b/web/src/i18n/README.md @@ -0,0 +1,29 @@ +# UI localization + +English source text is the translation key. `t(key, vars)` is shared by React +components, module constants, toasts and non-React helpers; missing keys fall back +to English. Keep keys, `{name}` placeholders, HTML formatting tags, Go template +actions and literal command/API examples intact when adding translations. + +The registered locales are English (`en`), Turkish (`tr`) and Simplified Chinese +(`zh-CN`). The Preferences language control renders `LOCALES` directly. A saved +`artex.locale` browser preference takes priority over browser language detection; +otherwise an exact language tag, then its base language, is matched. Chinese +browser variants use the available Simplified Chinese dictionary. Static export +and unsupported languages retain the English default. Switching language saves +the preference and reloads the page, following the existing module-level design. + +To add a locale, add `locales/.json` and register it in `Locale`, `LOCALES`, +`DICTIONARIES` and `INTL_LOCALES` in `index.ts`. Date/number formatting uses +`intlLocale`, and the HTML `lang` attribute follows the selected locale. + +Run `npm run test:i18n` and `npm run build:static` from `web/`. The dictionary +checker covers every locale, checks literal call sites, requires matching keys +including dynamic entries, and validates placeholders. Runtime tests exercise +language detection, persistence, English fallback and prototype-safe lookups. + +The initial Simplified Chinese dictionary reuses Chinese wording from this +repository's parent of commit `e233c02` (the English translation migration), with +additional translations for current functionality and normalized UI terminology. +This only localizes interface text; backend data, user content, model responses, +protocol identifiers and tool output are not rewritten. diff --git a/web/src/i18n/index.ts b/web/src/i18n/index.ts index 41aee64..6c3d6c8 100644 --- a/web/src/i18n/index.ts +++ b/web/src/i18n/index.ts @@ -8,11 +8,12 @@ // setLocale() stores the choice and reloads. That keeps t() usable anywhere (components, // module constants, toasts, non-React helpers) without hooks or re-render plumbing. // -// To translate the UI into another language, add locales/.json and list it in LOCALES. +// To translate the UI into another language, add locales/.json and register it below. import tr from "./locales/tr.json"; +import zhCN from "./locales/zh-CN.json"; -export type Locale = "tr" | "en"; +export type Locale = "tr" | "en" | "zh-CN"; // Used when nothing is saved and the browser language is not one of LOCALES, // and for the static prerender, which has no browser. @@ -21,6 +22,7 @@ export const DEFAULT_LOCALE: Locale = "en"; export const LOCALES: { code: Locale; label: string }[] = [ { code: "tr", label: "Türkçe" }, { code: "en", label: "English" }, + { code: "zh-CN", label: "简体中文" }, ]; const STORAGE_KEY = "artex.locale"; @@ -28,6 +30,13 @@ const STORAGE_KEY = "artex.locale"; const DICTIONARIES: Record> = { tr: tr as Record, en: {}, + "zh-CN": zhCN as Record, +}; + +const INTL_LOCALES: Record = { + tr: "tr-TR", + en: "en-US", + "zh-CN": "zh-CN", }; function detectLocale(): Locale { @@ -35,13 +44,18 @@ function detectLocale(): Locale { if (typeof window === "undefined") return DEFAULT_LOCALE; try { const saved = window.localStorage.getItem(STORAGE_KEY); - if (saved === "tr" || saved === "en") return saved; + const match = LOCALES.find((l) => l.code === saved); + if (match) return match.code; } catch { /* storage blocked: fall through to the browser language */ } for (const lang of window.navigator.languages ?? [window.navigator.language]) { - const base = lang?.toLowerCase().split("-")[0]; - const match = LOCALES.find((l) => l.code === base); + const tag = lang?.toLowerCase(); + const base = tag?.split("-")[0]; + // Prefer a full BCP 47 match, then the available locale for that language. + const match = + LOCALES.find((l) => l.code.toLowerCase() === tag) ?? + LOCALES.find((l) => l.code.toLowerCase().split("-")[0] === base); if (match) return match.code; } return DEFAULT_LOCALE; @@ -52,7 +66,7 @@ export const locale: Locale = detectLocale(); if (typeof document !== "undefined") document.documentElement.lang = locale; /** BCP 47 tag for Intl / toLocaleString calls, e.g. date.toLocaleString(intlLocale). */ -export const intlLocale = locale === "tr" ? "tr-TR" : "en-US"; +export const intlLocale = INTL_LOCALES[locale]; const dict = DICTIONARIES[locale]; @@ -71,7 +85,7 @@ export function setLocale(next: Locale) { try { window.localStorage.setItem(STORAGE_KEY, next); } catch { - /* storage blocked: the switch lasts only until the next load */ + /* storage blocked: fall back to browser-language detection on reload */ } window.location.reload(); }