Files
DeepTutor/web/tests/app-shell-language-bootstrap.test.ts
Botong dd29ff5a0a fix(i18n): sync frontend output language and remove zh-only UI fallbacks
Align frontend model-output language propagation and eliminate Simplified-Chinese-only UI shortcuts across chat, settings, books, reading, memory, and workspace surfaces:

- Preserve Traditional Chinese (zh-tw/zh-hk/zh-mo/zh-hant) and native aliases in resolveResponseLanguage(), and introduce shared effectiveResponseLanguage() and subscribeResponseLanguage() helpers.
- Sync response_language with language in SettingsStore when both were aligned, while preserving intentional divergence and clearing auto-synced pending response_language if an external desktop menu overrides language.
- Preserve per-conversation replyLanguageOverride when regenerating without a replay snapshot and subscribe ChatWorkspace's default reply-language label to live response_language changes.
- Route Book Creator, Book Chat fallback, Memory evolution/update, and Reading extension actions through effectiveResponseLanguage() instead of the UI language, and preserve zh-Hant in translationTarget().
- Route remaining zh ? ... : t(...) branches, hardcoded Memory labels, and binary zh/en date formatting through i18next and normalizeLanguage() so Traditional Chinese and all non-Simplified locales render through catalogs without modifying locale JSON files.

Related to #2933
Related to #684
2026-11-29 02:13:55 -06:00

118 lines
4.0 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
LANGUAGE_STORAGE_KEY,
RESPONSE_LANGUAGE_STORAGE_KEY,
effectiveResponseLanguage,
hasStoredLanguage,
hasStoredResponseLanguage,
readStoredLanguage,
} from "../context/app-shell-storage";
/** Minimal localStorage stand-in — the helpers only need get/set. */
function withLocalStorage(entries: Record<string, string>, run: () => void) {
const store = new Map(Object.entries(entries));
const original = (globalThis as { window?: unknown }).window;
(globalThis as { window?: unknown }).window = {
localStorage: {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => void store.set(key, value),
},
dispatchEvent: () => true,
};
try {
run();
} finally {
(globalThis as { window?: unknown }).window = original;
}
}
test("an absent choice is distinguishable from an explicit English one", () => {
// readStoredLanguage normalizes both to "en", so the bootstrap cannot use it
// to decide whether the server-side preference may be adopted.
withLocalStorage({}, () => {
assert.equal(hasStoredLanguage(), false);
assert.equal(readStoredLanguage(), "en");
});
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "en" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "en");
});
});
test("a stored choice is reported for every supported language", () => {
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "zh" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "zh");
});
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "fr" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "fr");
});
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "uk" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "uk");
});
});
test("an unusable value still counts as a choice and normalizes to English", () => {
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "xx" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(readStoredLanguage(), "en");
});
});
test("server-side rendering reports no stored choice instead of throwing", () => {
const original = (globalThis as { window?: unknown }).window;
(globalThis as { window?: unknown }).window = undefined;
try {
assert.equal(hasStoredLanguage(), false);
assert.equal(readStoredLanguage(), "en");
} finally {
(globalThis as { window?: unknown }).window = original;
}
});
test("a browser with only the interface key can still adopt the response language", () => {
// The two keys were split after the interface language shipped. Gating the
// bootstrap on hasStoredLanguage alone meant a browser from before the split
// returned early forever and never picked up the account's model output
// language — one of the ways "I set Chinese" and "it answers in English"
// stayed true at the same time.
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "zh" }, () => {
assert.equal(hasStoredLanguage(), true);
assert.equal(hasStoredResponseLanguage(), false);
});
withLocalStorage(
{
[LANGUAGE_STORAGE_KEY]: "zh",
[RESPONSE_LANGUAGE_STORAGE_KEY]: "en",
},
() => {
assert.equal(hasStoredResponseLanguage(), true);
},
);
});
test("effectiveResponseLanguage prefers stored response language, then stored UI language, then fallback", () => {
withLocalStorage(
{
[LANGUAGE_STORAGE_KEY]: "en",
[RESPONSE_LANGUAGE_STORAGE_KEY]: "ja",
},
() => {
assert.equal(effectiveResponseLanguage("fr"), "ja");
},
);
withLocalStorage({ [LANGUAGE_STORAGE_KEY]: "de" }, () => {
assert.equal(effectiveResponseLanguage("fr"), "de");
});
withLocalStorage({}, () => {
assert.equal(effectiveResponseLanguage("zh-TW"), "zh-tw");
assert.equal(effectiveResponseLanguage("es-ES"), "es");
assert.equal(effectiveResponseLanguage(undefined), "en");
});
});