feat(i18n): register and document Simplified Chinese

This commit is contained in:
BlackGownQ
2026-11-20 23:21:13 +08:00
committed by GitHub
parent 12f0ec0432
commit 2d6d5c4e2b
2 changed files with 50 additions and 7 deletions
+29
View File
@@ -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/<code>.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.
+21 -7
View File
@@ -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/<code>.json and list it in LOCALES.
// To translate the UI into another language, add locales/<code>.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<Locale, Record<string, string>> = {
tr: tr as Record<string, string>,
en: {},
"zh-CN": zhCN as Record<string, string>,
};
const INTL_LOCALES: Record<Locale, string> = {
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();
}