Fix translations and structured message rendering across all locales
This commit is contained in:
parent
905a2277ad
commit
f45b2107ee
43 changed files with 1882 additions and 964 deletions
|
|
@ -1,37 +1,65 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { languages } from "../packages/viewer/src/localization/runtime.js";
|
||||
|
||||
const directory = process.argv[2];
|
||||
if (!directory) {
|
||||
throw Error("Usage: node check-locales.mjs <localization directory>");
|
||||
}
|
||||
const source = JSON.parse(await readFile(join(directory, "en.json"), "utf8"));
|
||||
const placeholders = (text) => [...text.matchAll(/\{\w+\}/g)].map((match) => match[0]).sort();
|
||||
const locales = [
|
||||
"en",
|
||||
"de",
|
||||
"fr",
|
||||
"pl",
|
||||
"ro",
|
||||
"el",
|
||||
"hi",
|
||||
"ru",
|
||||
"da",
|
||||
"pt-BR",
|
||||
"ko",
|
||||
"zh-TW",
|
||||
"zh-CN",
|
||||
"vi",
|
||||
"it",
|
||||
"nl",
|
||||
"fa",
|
||||
];
|
||||
function placeholders(text) {
|
||||
const tokens = [];
|
||||
let start = -1;
|
||||
let depth = 0;
|
||||
let quote = "";
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const char = text[i];
|
||||
if (quote) {
|
||||
if (char === "\\") {
|
||||
i++;
|
||||
} else if (char === quote) {
|
||||
quote = "";
|
||||
}
|
||||
} else if (depth > 0 && (char === '"' || char === "'" || char === "`")) {
|
||||
quote = char;
|
||||
} else if (char === "{") {
|
||||
if (depth++ === 0) {
|
||||
start = i;
|
||||
}
|
||||
} else if (char === "}") {
|
||||
if (depth === 0) {
|
||||
tokens.push(char);
|
||||
} else if (--depth === 0) {
|
||||
tokens.push(text.slice(start, i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Legacy extraction left unfinished expressions; preserve those verbatim too.
|
||||
if (depth > 0) {
|
||||
tokens.push(text.slice(start));
|
||||
}
|
||||
return tokens.sort();
|
||||
}
|
||||
|
||||
async function readCatalog(locale) {
|
||||
const bytes = await readFile(join(directory, `${locale}.json`));
|
||||
const catalog = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
||||
assert.ok(catalog && typeof catalog === "object" && !Array.isArray(catalog), `${locale}: expected a catalog object`);
|
||||
for (const [key, text] of Object.entries(catalog)) {
|
||||
assert.equal(typeof text, "string", `${locale}: non-string translation of ${key}`);
|
||||
assert.ok(text.trim(), `${locale}: empty translation of ${key}`);
|
||||
assert.ok(text.isWellFormed(), `${locale}: invalid Unicode in ${key}`);
|
||||
assert.ok(!/[\p{Cc}\ufffd]/u.test(text.replace(/[\t\n\r]/g, "")), `${locale}: malformed text in ${key}`);
|
||||
}
|
||||
return catalog;
|
||||
}
|
||||
|
||||
const source = await readCatalog("en");
|
||||
const locales = Object.keys(languages);
|
||||
for (const locale of locales) {
|
||||
const catalog = JSON.parse(await readFile(join(directory, `${locale}.json`), "utf8"));
|
||||
const catalog = locale === "en" ? source : await readCatalog(locale);
|
||||
assert.deepEqual(Object.keys(catalog).sort(), Object.keys(source).sort(), `${locale}: missing or extra strings`);
|
||||
for (const [key, text] of Object.entries(catalog)) {
|
||||
assert.equal(typeof text, "string", `${locale}: ${key}`);
|
||||
assert.ok(text.trim(), `${locale}: empty translation of ${key}`);
|
||||
assert.deepEqual(placeholders(text), placeholders(source[key]), `${locale}: changed placeholders in ${key}`);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,6 +155,60 @@ test("colony ship lists translate each upgrade without rewriting player names",
|
|||
assert.equal(t.translate(input), input);
|
||||
});
|
||||
|
||||
test("explicit message formatting reorders opaque values without interpreting them", () => {
|
||||
const t = createTranslator(catalogs, "nl");
|
||||
assert.equal(t.format("Build {p0} for {p1}", ["Draw {p1}", 0]), "Bouw voor 0: Draw {p1}");
|
||||
assert.equal(t.format("{p0} buys {p1}", ["Ore", "Draw"]), "Ore koopt Draw");
|
||||
assert.equal(t.format("Unknown {p0}", ["Round"]), "Unknown Round");
|
||||
t.setLocale("en");
|
||||
assert.equal(t.format("Build {p0} for {p1}", ["Ore", 10]), "Build Ore for 10");
|
||||
});
|
||||
|
||||
test("explicit formatting supports legacy placeholder offsets without guessing rendered values", () => {
|
||||
const t = createTranslator({ fr: { "{p23} (Era {p24})": "{p23} (Ère {p24})" } }, "fr");
|
||||
assert.equal(t.format("{p0} (Era {p1})", ["Water", "II"]), "Water (Ère II)");
|
||||
assert.equal(t.format("Unknown {p0}", ["Water (Era II)"]), "Unknown Water (Era II)");
|
||||
});
|
||||
|
||||
test("soft hyphens do not prevent resource labels from matching", () => {
|
||||
const t = createTranslator({ fr: { "New Chemicals": "Nouvelle chimie" } }, "fr");
|
||||
assert.equal(t.translate("New Chemi\u00adcals"), "Nouvelle chimie");
|
||||
});
|
||||
|
||||
test("staffing events cannot match generic factory labels in any language", async () => {
|
||||
const { languages } = await import("../packages/viewer/src/localization/runtime.js");
|
||||
for (const locale of Object.keys(languages)) {
|
||||
const catalog = JSON.parse(
|
||||
await readFile(new URL(`../packages/viewer/src/localization/${locale}.json`, import.meta.url), "utf8")
|
||||
);
|
||||
const t = createTranslator({ [locale]: catalog }, locale);
|
||||
for (const name of ["not2cool", "Heavy Equipment", "Alice mans 5 factories"]) {
|
||||
t.setNames([name]);
|
||||
for (const count of [0, 1, 9, 99]) {
|
||||
const key = count === 1 ? "{p0} mans {p1} factory" : "{p0} mans {p1} factories";
|
||||
const input = `${name} mans ${count} ${count === 1 ? "factory" : "factories"}`;
|
||||
const expected = catalog[key].replace(/\{p([01])\}/g, (_, i) => [name, count][i]);
|
||||
assert.equal(t.translate(input), expected, `${locale}: ${input}`);
|
||||
assert.equal(t.format(key, [name, count]), expected, `${locale}: explicit ${input}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("generic factory labels require a quantity instead of consuming arbitrary sentences", () => {
|
||||
const t = createTranslator({ fr: { "{p0} {p1} factories": "{p0} usines de {p1}", Water: "Eau" } }, "fr");
|
||||
assert.equal(t.translate("9 Water factories"), "9 usines de Eau");
|
||||
assert.equal(t.translate("Unknown statement about factories"), "Unknown statement about factories");
|
||||
});
|
||||
|
||||
test("nested translation cache entries do not poison standalone messages", () => {
|
||||
const t = createTranslator({ nl: { "Layer {p0}": "Laag {p0}", "Round {p0}": "Ronde {p0}" } }, "nl");
|
||||
t.translate("Layer ".repeat(6) + "Round 2");
|
||||
assert.equal(t.translate("Round 2"), "Ronde 2");
|
||||
t.translate("Layer ".repeat(5) + "Round 3");
|
||||
assert.equal(t.translate("Round 3"), "Ronde 3");
|
||||
});
|
||||
|
||||
test("Persian regional tags select Farsi", () => {
|
||||
assert.equal(resolveLocale("fa-IR"), "fa");
|
||||
assert.equal(resolveLocale("fa_IR"), "fa");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue