Add Simplified Chinese viewer translations

This commit is contained in:
coyotte508 2026-10-20 14:15:10 +02:00
commit fb77b101f1
5 changed files with 1283 additions and 3 deletions

View file

@ -20,6 +20,7 @@ const locales = [
"da",
"pt-BR",
"ko",
"zh-CN",
"zh-TW",
"vi",
"it",

View file

@ -27,7 +27,9 @@ test("regional language preferences select supported catalogues", () => {
assert.equal(resolveLocale("nl-BE"), "nl");
assert.equal(resolveLocale("pt-PT"), "pt-BR");
assert.equal(resolveLocale("zh-Hant-TW"), "zh-TW");
assert.equal(resolveLocale("zh-CN"), "en");
for (const tag of ["zh", "zh-CN", "zh-SG", "zh-Hans", "zh-Hans-TW"])
assert.equal(resolveLocale(tag), "zh-CN");
assert.equal(resolveLocale("zh-Hant-CN"), "zh-TW");
assert.equal(resolveLocale(undefined), "en");
});
test("dynamic values are reordered while player names stay unchanged", () => {
@ -88,3 +90,20 @@ test("Persian regional tags select Farsi", () => {
assert.equal(resolveLocale("fa-IR"), "fa");
assert.equal(resolveLocale("fa_IR"), "fa");
});
test("Simplified Chinese preserves names and values in game logs", async () => {
const zh = JSON.parse(
await readFile(
new URL("../viewer/localization/zh-CN.json", import.meta.url),
"utf8",
),
);
const t = createTranslator({ "zh-CN": zh }, "zh-SG");
t.setNames(["Maya"]);
assert.equal(t.translate("Maya"), "Maya");
assert.equal(t.translate("north"), "北");
assert.equal(t.translate("Maya moves to C2."), "Maya 移动至 C2。");
assert.equal(t.translate("3 green nutrients"), "3 枚绿色营养块");
assert.equal(t.translate("Color-blind mode"), "色盲模式");
assert.equal(t.translate("Round"), "轮次");
});

51
test/chinese-browser.mjs Normal file
View file

@ -0,0 +1,51 @@
import { serveAssets } from "./asset-server.mjs";
import assert from "node:assert/strict";
import { chromium } from "playwright";
import { init, stripSecret } from "../engine/index.js";
const base = await serveAssets("dist");
const state = stripSecret(await init(3, [], {}, "chinese-browser"), 0);
const browser = await chromium.launch({
executablePath: process.env.CHROMIUM_EXECUTABLE,
});
try {
for (const width of [390, 1400]) {
const page = await browser.newPage({ viewport: { width, height: 950 } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.setContent(
'<!doctype html><meta name="viewport" content="width=device-width,initial-scale=1"><div id="app"></div>',
);
await page.addStyleTag({ url: base + "/viewer.css" });
await page.addScriptTag({ url: base + "/viewer.js" });
await page.evaluate((state) => {
window.e = primordialSoup.launch("#app");
e.emit("preferences", { locale: "zh-CN" });
e.emit("player", { index: 0 });
e.emit("state", state);
}, state);
await page.waitForFunction(() =>
document.body.innerText.includes("变形虫"),
);
const chinese = await page.locator("body").innerText();
assert.match(chinese, /基因|营养|漂流/);
await page.evaluate(() => e.emit("preferences", { locale: "en" }));
await page.waitForFunction(
() => !document.body.innerText.includes("变形虫"),
);
await page.evaluate(() => e.emit("preferences", { locale: "zh-Hans-SG" }));
await page.waitForFunction(() =>
document.body.innerText.includes("变形虫"),
);
assert.deepEqual(errors, []);
await page.screenshot({
path: `/tmp/soup-zh-${width}.png`,
fullPage: true,
});
console.log(
`Simplified Chinese cold load and live switching passed at ${width}px`,
);
await page.close();
}
} finally {
await browser.close();
}

View file

@ -10,6 +10,7 @@ export const languages = {
da: "Dansk",
"pt-BR": "Português (Brasil)",
ko: "한국어",
"zh-CN": "简体中文",
"zh-TW": "繁體中文",
vi: "Tiếng Việt",
it: "Italiano",
@ -31,8 +32,10 @@ export function resolveLocale(value) {
if (tag === "pt" || tag.startsWith("pt-")) {
return "pt-BR";
}
if (/^zh-(?:tw|hk|mo|hant)(?:-|$)/.test(tag)) {
return "zh-TW";
if (tag === "zh" || tag.startsWith("zh-")) {
if (/(?:^|-)hans(?:-|$)/.test(tag)) return "zh-CN";
if (/(?:^|-)hant(?:-|$)/.test(tag)) return "zh-TW";
return /^zh-(?:tw|hk|mo)(?:-|$)/.test(tag) ? "zh-TW" : "zh-CN";
}
return (
Object.keys(languages).find((locale) => locale === tag.split("-")[0]) ??

File diff suppressed because it is too large Load diff