дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 09:41:34 +02:00
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 <[email protected]>
Цей коміт міститься в:
1 джерело
632ee77580
коміт
e5642b0631
3 files changed
+56
-2
No files matched your search
@@ -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";
|
||||
|
||||
@@ -111,17 +111,36 @@ async function loadTexts(locale: string): Promise<Catalog> {
|
||||
return catalog;
|
||||
}
|
||||
|
||||
/** English source texts, loaded with any other locale (see `translateText`). */
|
||||
let sources: Catalog | undefined;
|
||||
|
||||
async function loadSources(): Promise<void> {
|
||||
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<Locale> {
|
||||
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;
|
||||
|
||||
@@ -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("合併失敗");
|
||||
});
|
||||
Reference in new issue
Заблокувати користувача