Файли
openidea/src/platform/i18n.test.ts
T
LU XIANGRUIтаwildcatDownstairs 0cfabdbbbd feat(swift): choose the Xcode a project uses and pass it to its tools as DEVELOPER_DIR (#6964)
Settings > Languages > Swift gets an Xcode field (empty = the xcode-select one). For a trusted project the chosen Xcode.app or Contents/Developer becomes DEVELOPER_DIR in the project environment, so sourcekit-lsp, xcodebuild, xcrun, simulators, debug adapters and terminals use it; it overrides a DEVELOPER_DIR from .envrc and is not applied to untrusted projects or LightEdit.

Closes #6963

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-30 20:43:48 +08:00

472 рядки
18 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
import { readFileSync, readdirSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import {
_resetI18nForTests,
formatMessage,
isValidMessage,
keysWithPrefix,
registerMessages,
resolveLocale,
setLocale,
SUPPORTED_LOCALES,
t,
tx,
txPart,
txTemplate,
useI18n,
} from "./i18n";
import en from "./locales/en.json";
afterEach(() => _resetI18nForTests());
describe("formatMessage", () => {
it("substitutes parameters and leaves unknown ones", () => {
expect(formatMessage("Open {name}", { name: "openidea" })).toBe("Open openidea");
expect(formatMessage("Open {name} in {where}", { name: "openidea" })).toBe("Open openidea in {where}");
expect(formatMessage("plain text")).toBe("plain text");
});
it("formats numbers per locale", () => {
expect(formatMessage("{n} files", { n: 12345 }, "en")).toBe("12,345 files");
expect(formatMessage("{n, number}", { n: 1234.5 }, "en")).toBe("1,234.5");
});
it("selects plural categories, exact matches first", () => {
const m = "{count, plural, =0 {No files} one {# file} other {# files}}";
expect(formatMessage(m, { count: 0 })).toBe("No files");
expect(formatMessage(m, { count: 1 })).toBe("1 file");
expect(formatMessage(m, { count: 2 })).toBe("2 files");
expect(formatMessage(m, { count: 1000 })).toBe("1,000 files");
});
it("uses CLDR rules of the locale (Chinese has only `other`)", () => {
const m = "{count, plural, one {# one} other {# other}}";
expect(formatMessage(m, { count: 1 }, "zh")).toBe("1 other");
expect(formatMessage(m, { count: 1 }, "en")).toBe("1 one");
});
it("uses CLDR rules of the locale (French zero is singular, `many` falls back to `other`)", () => {
const m = "{count, plural, one {# fichier} other {# fichiers}}";
expect(formatMessage(m, { count: 0 }, "fr")).toBe("0 fichier");
expect(formatMessage(m, { count: 2 }, "fr")).toBe("2 fichiers");
expect(formatMessage(m, { count: 1_000_000 }, "fr")).toBe(
`${new Intl.NumberFormat("fr").format(1_000_000)} fichiers`,
);
});
it("supports select and nesting", () => {
const m = "{os, select, macos {Finder} windows {Explorer} other {Files}}";
expect(formatMessage(m, { os: "macos" })).toBe("Finder");
expect(formatMessage(m, { os: "linux" })).toBe("Files");
const nested = "{count, plural, one {{who} has # file} other {{who} has # files}}";
expect(formatMessage(nested, { count: 3, who: "Ann" })).toBe("Ann has 3 files");
const deep = "{os, select, macos {{count, plural, one {# window} other {# windows}}} other {#}}";
expect(formatMessage(deep, { os: "macos", count: 2 })).toBe("2 windows");
});
it("falls back to `other` when the parameter is missing", () => {
expect(formatMessage("{n, plural, one {one} other {many}}")).toBe("many");
});
it("returns malformed messages unformatted", () => {
expect(formatMessage("{n, plural, one {x}", { n: 1 })).toBe("{n, plural, one {x}");
expect(formatMessage("{n, weird, a {b}}", { n: 1 })).toBe("{n, weird, a {b}}");
expect(isValidMessage("{n, plural, one {x}")).toBe(false);
expect(isValidMessage("{n, plural, one {x} other {y}}")).toBe(true);
});
});
describe("t / locales", () => {
it("resolves locale tags", () => {
expect(resolveLocale("zh-CN")).toBe("zh");
expect(resolveLocale("zh_Hans")).toBe("zh");
expect(resolveLocale("zh-Hans-HK")).toBe("zh");
expect(resolveLocale("zh-TW")).toBe("zh-TW");
expect(resolveLocale("zh_tw")).toBe("zh-TW");
expect(resolveLocale("zh-HK")).toBe("zh-TW");
expect(resolveLocale("zh-Hant")).toBe("zh-TW");
expect(resolveLocale("zh-Hant-TW")).toBe("zh-TW");
expect(resolveLocale("ko-KR")).toBe("ko");
expect(resolveLocale("pt-BR")).toBe("pt");
expect(resolveLocale("pt_PT")).toBe("pt");
expect(resolveLocale("EN-us")).toBe("en");
expect(resolveLocale("fr")).toBe("fr");
expect(resolveLocale("fr-CA")).toBe("fr");
expect(resolveLocale("pl_PL")).toBe("pl");
expect(resolveLocale("tr-TR")).toBe("tr");
expect(resolveLocale("pl-PL")).toBe("pl");
expect(resolveLocale("es-MX")).toBe("es");
expect(resolveLocale("es_MX")).toBe("es");
expect(resolveLocale("ru-RU")).toBe("ru");
expect(resolveLocale("ru_RU")).toBe("ru");
expect(resolveLocale("xx")).toBe("en");
expect(resolveLocale(undefined)).toBe(resolveLocale(navigator.language));
});
it("translates with English fallback, then the key itself", async () => {
expect(t("common.cancel")).toBe("Cancel");
await setLocale("zh");
expect(useI18n.getState().locale).toBe("zh");
expect(t("common.cancel")).toBe("取消");
registerMessages("en", { "test.onlyEnglish": "Only English" });
expect(t("test.onlyEnglish")).toBe("Only English");
expect(t("test.missing.key")).toBe("test.missing.key");
});
it("applies params and plurals in the active locale", async () => {
expect(t("app.unsaved.body", { count: 1, project: "openidea" })).toBe(
"1 file has unsaved changes in openidea. Save before closing?",
);
await setLocale("zh-CN");
expect(t("app.keymap.count", { count: 1 })).toBe("1 个快捷键");
await setLocale("zh-Hant-TW");
expect(t("app.keymap.count", { count: 1 })).toBe("1 個快速鍵");
await setLocale("pt-BR");
expect(t("app.keymap.count", { count: 1 })).toBe("1 atalho");
expect(t("app.keymap.count", { count: 5 })).toBe("5 atalhos");
await setLocale("ru-RU");
expect(t("app.keymap.count", { count: 1 })).toBe("1 сочетание");
expect(t("app.keymap.count", { count: 2 })).toBe("2 сочетания");
expect(t("app.keymap.count", { count: 5 })).toBe("5 сочетаний");
});
it("switches to Korean", async () => {
await setLocale("ko-KR");
expect(useI18n.getState().locale).toBe("ko");
expect(t("common.cancel")).toBe("취소");
expect(tx("Cancel")).toBe("취소");
});
it("falls back to English for unsupported locales", async () => {
await setLocale("xx");
expect(useI18n.getState().locale).toBe("en");
});
it("registerMessages adds and removes feature strings", async () => {
const off = registerMessages("zh", { "feature.hello": "你好" });
registerMessages("en", { "feature.hello": "Hello" });
expect(t("feature.hello")).toBe("Hello");
await setLocale("zh");
expect(t("feature.hello")).toBe("你好");
off();
expect(t("feature.hello")).toBe("Hello");
});
it("translates source text through the locale's text catalog", async () => {
await setLocale("en");
expect(tx("Sync with OS")).toBe("Sync with OS");
await setLocale("zh");
expect(tx("Sync with OS")).toBe("与操作系统同步");
expect(tx("Some text nobody translated")).toBe("Some text nobody translated");
expect(tx(undefined)).toBeUndefined();
});
it("translates a tx tag without values like the same text as a string", async () => {
await setLocale("zh");
expect(tx`Sync with OS`).toBe("与操作系统同步");
expect(tx`Sync with OS`).toBe(tx("Sync with OS"));
expect(tx`Some text nobody translated`).toBe("Some text nobody translated");
});
it("matches source-text templates, translating the substituted text", async () => {
expect(txTemplate("Push failed")).toBe("Push failed");
await setLocale("zh");
expect(txTemplate("ruff is not installed")).toBe("ruff 未安装");
expect(txTemplate("Language server 'gopls' is not installed")).toBe("语言服务器 'gopls' 未安装");
expect(txTemplate("Retry failed")).toBe(`${tx("Retry")}失败`);
expect(txTemplate("Sync with OS")).toBe("与操作系统同步");
expect(txTemplate("Something else entirely")).toBe("Something else entirely");
expect(
txTemplate(
"The PHP language server can't rename this symbol; renaming only the plugin's indexed names would leave it half renamed",
),
).toBe("语言服务器无法重命名此符号(PHP);仅重命名插件索引的名称会导致重命名不完整");
expect(
txTemplate(
"The Ruby language server can't rename this symbol; renaming only the plugin's indexed names would leave it half renamed",
),
).toBe("语言服务器无法重命名此符号(Ruby 语言);仅重命名插件索引的名称会导致重命名不完整");
});
it("translates text built by the tx tag, again after the locale changes", async () => {
await setLocale("en");
const name = "orders";
const built = tx`Run '${name}'`;
expect(built).toBe("Run 'orders'");
expect(tx`Unknown ${name} text`).toBe("Unknown orders text");
await setLocale("zh");
// Built before the catalog loaded: primitives passing it through `tx` translate it.
expect(tx(built)).toBe("运行 'orders'");
expect(txTemplate(built)).toBe("运行 'orders'");
const zh = tx`Run '${name}'`;
expect(zh).toBe("运行 'orders'");
// Values are inserted as written, even when the catalog has an entry for them…
expect(tx`Rename directory '${"tasks"}' and its usages to:`).toBe("将目录“tasks”及其用法重命名为:");
expect(tx`Run '${"Retry"}'`).toBe("运行 'Retry'");
// …unless they are marked as UI text, which is translated again after a locale change.
const marked = tx`Run '${txPart("Retry")}'`;
expect(marked).toBe(`运行 '${tx("Retry")}'`);
// Values are inserted as they are, braces included.
expect(tx`Drop ${"stash@{0}"}? This cannot be undone.`).toBe("是否丢弃 stash@{0}?此操作无法撤消。");
await setLocale("en");
expect(tx(zh)).toBe("Run 'orders'");
expect(tx(marked)).toBe("Run 'Retry'");
});
it("loads the Portuguese catalogs", async () => {
await setLocale("pt-BR");
expect(useI18n.getState().locale).toBe("pt");
expect(t("common.cancel")).toBe("Cancelar");
expect(t("app.keymap.count", { count: 1 })).toBe("1 atalho");
expect(t("app.keymap.count", { count: 5 })).toBe("5 atalhos");
});
it("loads the French catalogs", async () => {
await setLocale("fr-FR");
expect(useI18n.getState().locale).toBe("fr");
expect(t("common.cancel")).toBe("Annuler");
expect(t("app.keymap.count", { count: 0 })).toBe("0 raccourci");
expect(tx("Sync with OS")).toBe("Synchroniser avec le système");
expect(tx`Run '${"orders"}'`).toBe("Exécuter 'orders'");
expect(txTemplate("ruff is not installed")).toBe("ruff n'est pas installé");
});
it("loads the Polish catalogs", async () => {
await setLocale("pl-PL");
expect(useI18n.getState().locale).toBe("pl");
expect(t("common.cancel")).toBe("Anuluj");
expect(t("app.keymap.count", { count: 1 })).toBe("1 skrót");
expect(t("app.keymap.count", { count: 5 })).toBe("5 skrótów");
});
it("loads the Turkish catalogs", async () => {
await setLocale("tr-TR");
expect(useI18n.getState().locale).toBe("tr");
expect(t("common.cancel")).toBe("İptal");
expect(t("app.keymap.count", { count: 1 })).toBe("1 kısayol");
});
it("loads the Spanish catalogs", async () => {
await setLocale("es-MX");
expect(useI18n.getState().locale).toBe("es");
expect(t("common.cancel")).toBe("Cancelar");
expect(t("app.keymap.count", { count: 1 })).toBe("1 atajo");
expect(t("app.keymap.count", { count: 5 })).toBe("5 atajos");
});
it("lists keys by prefix", () => {
const keys = keysWithPrefix("menu.");
expect(keys).toContain("menu.file.save");
expect(keys).toContain("menu.file.saveAs");
expect(keys.every((k) => k.startsWith("menu."))).toBe(true);
});
});
describe("catalogs", () => {
type Catalog = Record<string, string>;
const LOCALES_DIR = join(dirname(fileURLToPath(import.meta.url)), "locales");
/** Catalog files of a folder as `{ locale: catalog }`, read from disk so that a new file is always seen. */
const readCatalogs = (dir: string) =>
Object.fromEntries(
readdirSync(dir)
.filter((file) => file.endsWith(".json"))
.map((file) => [file.slice(0, -5), JSON.parse(readFileSync(join(dir, file), "utf8")) as Catalog]),
);
const { en: _en, ...translations } = readCatalogs(LOCALES_DIR);
const texts = readCatalogs(join(LOCALES_DIR, "text"));
const catalogs: Record<string, Catalog> = { en, ...translations };
/** Must have every key; the other locales show English for keys they lack. */
const COMPLETE = ["zh"];
/** Written in another script, so a text equal to its English source was not translated. */
const OWN_SCRIPT = new Set(["zh", "ko"]);
/** Product names intentionally kept as written in every locale. */
const SAME_AS_ENGLISH = new Set([
// PostgreSQL storage parameters, the placeholder of Modify Table's Storage options field.
"fillfactor=70, autovacuum_enabled=false",
// The placeholder of an Xcode run configuration's project or workspace field.
"App.xcodeproj",
// The Swift settings field naming the Xcode a project uses.
"Xcode",
// GitHub's product name, the title of its tool window.
"GitHub Actions",
"Flutter DevTools",
// The package index's name, as a project link in the Python Packages tool window.
"PyPI",
// Pub / Flutter command names on the pubspec.yaml banner.
"Pub get",
"Pub upgrade",
"Pub outdated",
"Flutter doctor",
"4096 MB",
"6144 MB",
"8192 MB",
"12288 MB",
"16384 MB",
"Docker",
// The OpenPGP signing format, in Configure GPG Signature.
"OpenPGP",
"URL",
// The character set's name, as the Memory View's text column header.
"ASCII",
"ID:",
"Code Vision",
"JUnit",
"HotSwap",
"⌘Enter",
"Fork",
"Shell",
"ID",
"my-agent",
"Java",
"Python",
"Python {short}",
"FastAPI",
// The FastAPI configuration's Application placeholder.
"main:app",
"Flutter {framework}",
"Dart {dart}",
"C/C++",
"C#",
"Go",
"Rust",
"Kotlin",
"Swift",
"Claude Code",
"GitHub Copilot",
"Codex",
"SKILL.md",
"context: fork",
"paths ({count})",
"paths",
"Cursor",
"PHP",
"SQL",
"COPY",
"INSERT",
"pg_restore",
"psql",
"JavaScript",
"TypeScript",
"Vue Router",
// Python profilers (Settings › Python › Profiler).
"cProfile",
"py-spy",
// Python unittest: the runner's name and a dotted module placeholder.
"unittest",
"tests.test_models",
"calc.py",
"Pinia",
"Vitest",
"ESLint",
"HTML",
"CSS",
"JSON",
"YAML",
"XML",
"Maven",
"Flutter",
"Thymeleaf",
"sbt",
"Gradle",
"npm",
"pnpm",
"Yarn",
"Cargo",
"CMake",
"f-string",
"Tailwind CSS",
"Bun",
"Deno",
"Node.js",
"bun-dap (Bun)",
// DuckDB and DuckLake product names, the JDBC URL field and its path placeholder.
"DuckDB",
"DuckLake",
"JDBC URL",
"path/to/database.duckdb",
// Unreal Engine projects: the product name and the .uproject path placeholder.
"Unreal Engine",
"…/Game.uproject",
// Celery run configuration: the product and its worker, and the module / task placeholders.
"Celery",
"Worker",
"proj",
"proj.tasks",
"add",
]);
const translated = SUPPORTED_LOCALES.filter((locale) => locale !== "en").sort();
it("exist for exactly the supported locales", () => {
expect(translated.length).toBeGreaterThan(0);
expect(Object.keys(translations).sort()).toEqual(translated);
expect(Object.keys(texts).sort()).toEqual([...SUPPORTED_LOCALES].sort());
});
it("are the locales the backend picks from the OS language on first launch", () => {
const rust = readFileSync(join(LOCALES_DIR, "../../../crates/ide-workspace/src/settings.rs"), "utf8");
const list = /pub const UI_LOCALES: &\[&str\] = &\[([^\]]*)\]/.exec(rust)?.[1] ?? "";
expect([...list.matchAll(/"([^"]+)"/g)].map((m) => m[1]).sort()).toEqual([...SUPPORTED_LOCALES].sort());
});
it("list every key once", () => {
// JSON.parse keeps the last of two equal keys, so a merge that adds a key twice goes unnoticed.
for (const dir of [LOCALES_DIR, join(LOCALES_DIR, "text")]) {
for (const file of readdirSync(dir).filter((f) => f.endsWith(".json"))) {
const keys = [...readFileSync(join(dir, file), "utf8").matchAll(/^ {2}("(?:[^"\\]|\\.)*"):/gm)].map(
(m) => JSON.parse(m[1]!) as string,
);
const seen = new Set<string>();
const repeated = keys.filter((key) => seen.has(key) || !seen.add(key));
expect(repeated, `${file} in ${dir}`).toEqual([]);
}
}
});
it("translate source text into non-empty text", () => {
for (const [locale, catalog] of Object.entries(texts))
for (const [source, text] of Object.entries(catalog)) expect(text.trim(), `${locale}:${source}`).not.toBe("");
});
it("translate source text into text different from English, in locales with another script", () => {
for (const [locale, catalog] of Object.entries(texts))
if (OWN_SCRIPT.has(locale))
for (const [source, text] of Object.entries(catalog))
if (!SAME_AS_ENGLISH.has(source)) expect(text, `${locale}:${source}`).not.toBe(source);
});
it("keep the placeholders of source-text templates", () => {
const names = (m: string) => [...m.matchAll(/\{(\w+)\}/g)].map((x) => x[1]).sort();
for (const [locale, catalog] of Object.entries(texts))
for (const [source, text] of Object.entries(catalog))
expect(names(text), `${locale}:${source}`).toEqual(names(source));
});
it.each(COMPLETE)("have the same keys as English (%s)", (locale) => {
expect(Object.keys(translations[locale]!).sort()).toEqual(Object.keys(en).sort());
});
it("have no keys English lacks", () => {
for (const [locale, catalog] of Object.entries(translations))
expect(
Object.keys(catalog).filter((key) => !Object.hasOwn(en, key)),
locale,
).toEqual([]);
});
it("contain only well-formed messages", () => {
for (const [locale, catalog] of Object.entries(catalogs))
for (const [key, message] of Object.entries(catalog))
expect(isValidMessage(message), `${locale}:${key}`).toBe(true);
});
it("use the same parameters across locales", () => {
const params = (m: string) => [...m.matchAll(/\{\s*([a-z]\w*)\s*[,}]/g)].map((x) => x[1]).sort();
for (const [locale, catalog] of Object.entries(translations))
for (const [key, message] of Object.entries(catalog))
expect(params(message), `${locale}:${key}`).toEqual(params((en as Record<string, string>)[key] ?? ""));
});
});