From e5642b063106bbd194868a6fd7e489789ef708ea Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Sat, 5 Dec 2026 01:16:05 +0800 Subject: [PATCH] fix(i18n): keep known text a locale lacks in English instead of half-translating it (#8083) In a locale that lacked a sentence, txTemplate matched the English source against loose templates such as '{count} class' and translated only the literal part, so hints and notifications showed half-translated text. A text that is a key of text/en.json and missing from the locale now comes back in English with no template matching (decided in #8047); runtime text that is not in the catalog still matches templates, and the same check runs before text built by a tx tag is rebuilt. text/en.json is loaded with any other locale; if it fails to load, translation behaves as before. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- src/platform/i18n.test.ts | 13 ++++++++++++ src/platform/i18n.ts | 27 +++++++++++++++++++++++-- src/platform/i18nSourcesFailure.test.ts | 18 +++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/platform/i18nSourcesFailure.test.ts diff --git a/src/platform/i18n.test.ts b/src/platform/i18n.test.ts index ed97d57302..1985415eb3 100644 --- a/src/platform/i18n.test.ts +++ b/src/platform/i18n.test.ts @@ -190,6 +190,19 @@ describe("t / locales", () => { ).toBe("语言服务器无法重命名此符号(Ruby 语言);仅重命名插件索引的名称会导致重命名不完整"); }); + it("keeps known text a locale lacks in English rather than matching it with a template", async () => { + await setLocale("zh-TW"); + const rearrange = "Nothing to rearrange: members are in order, or moving them could change the class"; + expect(txTemplate(rearrange)).toBe(rearrange); + expect(txTemplate("Nothing to put in a gist")).toBe("Nothing to put in a gist"); + // Also when a `tx` tag has built the same text. + expect(tx`${txPart("Amend commit")} failed`).toBe("Amend commit失敗"); + expect(txTemplate("Amend commit failed")).toBe("Amend commit failed"); + expect(tx("Amend commit failed")).toBe("Amend commit failed"); + // Text built at runtime still matches templates. + expect(txTemplate("ruff is not installed")).toBe(tx`${"ruff"} is not installed`); + }); + it("translates text built by the tx tag, again after the locale changes", async () => { await setLocale("en"); const name = "orders"; diff --git a/src/platform/i18n.ts b/src/platform/i18n.ts index 4b11bfaa69..e519c1ae5b 100644 --- a/src/platform/i18n.ts +++ b/src/platform/i18n.ts @@ -111,17 +111,36 @@ async function loadTexts(locale: string): Promise { return catalog; } +/** English source texts, loaded with any other locale (see `translateText`). */ +let sources: Catalog | undefined; + +async function loadSources(): Promise { + try { + sources ??= await textLoaders["./locales/text/en.json"]!(); + } catch (e) { + console.warn("cannot load the English source texts", e); + } +} + +/** + * Known text the locale lacks stays in English: a template, or a `tx` tag that built the same + * text, would translate only part of it ("…could change the class" → "…could change the 個類別"). + */ +const keptInEnglish = (text: string) => sources !== undefined && Object.hasOwn(sources, text); + let switchToken = 0; /** Switches the UI locale (loading its catalog on first use). Unsupported tags fall back to English. */ export async function setLocale(input: string | null | undefined): Promise { const locale = resolveLocale(input); const token = ++switchToken; + const sourcesLoaded = locale !== DEFAULT_LOCALE && loadSources(); try { await Promise.all([loadCatalog(locale), loadTexts(locale)]); } catch (e) { console.warn(`cannot load locale ${locale}`, e); } + await sourcesLoaded; if (token === switchToken) { useI18n.setState({ locale, messages: merged(locale), texts: textCatalogs.get(locale) ?? {} }); if (typeof document !== "undefined") document.documentElement.lang = LOCALES[locale].tag; @@ -237,7 +256,8 @@ export const txPart = (text: string): unknown => new TxPart(text); function translateSource(texts: Catalog, text: string | TemplateStringsArray | undefined, values: unknown[]) { // A tag without `${}` spells out its whole text, which is looked up as written. if (text !== undefined && typeof text !== "string" && !values.length) text = text[0]!; - if (text === undefined || typeof text === "string") return text && (texts[text] ?? rebuild(texts, text) ?? text); + if (text === undefined || typeof text === "string") + return text && (texts[text] ?? (keptInEnglish(text) ? text : (rebuild(texts, text) ?? text))); return buildText(texts, { strings: text, values }); } @@ -330,8 +350,11 @@ function templatesOf(texts: Catalog): Template[] { } function translateText(texts: Catalog, text: string): string { - const exact = texts[text] ?? rebuild(texts, text); + const exact = texts[text]; if (exact !== undefined) return exact; + if (keptInEnglish(text)) return text; + const rebuilt = rebuild(texts, text); + if (rebuilt !== undefined) return rebuilt; for (const { pattern, names, translation } of templatesOf(texts)) { const m = pattern.exec(text); if (!m) continue; diff --git a/src/platform/i18nSourcesFailure.test.ts b/src/platform/i18nSourcesFailure.test.ts new file mode 100644 index 0000000000..aa00aa9ead --- /dev/null +++ b/src/platform/i18nSourcesFailure.test.ts @@ -0,0 +1,18 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { _resetI18nForTests, setLocale, t, tx, txTemplate, useI18n } from "./i18n"; + +vi.mock("./locales/text/en.json", () => { + throw new Error("unavailable"); +}); + +afterEach(() => _resetI18nForTests()); + +it("loads a locale even when the English source texts fail to load", async () => { + vi.spyOn(console, "warn").mockImplementation(() => {}); + await setLocale("zh-TW"); + expect(useI18n.getState().locale).toBe("zh-TW"); + expect(t("common.cancel")).not.toBe("Cancel"); + expect(tx("Sync with OS")).not.toBe("Sync with OS"); + // Without them txTemplate matches templates as before. + expect(txTemplate("Merge failed")).toBe("合併失敗"); +});