Files
siftlode/frontend/src/i18n/index.ts
T
peter 4aaaaa7860 chore(comments): drop epic, sprint and review-round ids from the code
The code cited its plan instead of saying why: `E28 review J4`, `(R6 S3b)`, `S1 justified…`,
`deep-review round 5`. That plan lives in the private ops repo, so for every reader of this one the
citation was noise, and a sentence whose subject was the plan step ("round 2 showed…") hid its
lesson behind an id. Each one is gone and the "why" kept: a bare citation is deleted, a sentence
carrying a lesson is reworded around it ("a later attempt showed…").

Measured (`scripts/audit_metrics.py`, product code): 652 lines in 270 files carried an epic code by
the definition the next commit holds (507 / 241 by the old, two-digit-only one) -> 0. Across all
code, tests, scripts and configs: ~1100 epic-code lines plus ~320 sprint/round ids.

Behaviour unchanged, checked mechanically: every changed .py file has the same AST once docstrings
are blanked; css/sh/toml/yml/json files are identical once comments are stripped; the only
non-comment changes in TS/JS are test titles, one description string in a contract table, and five
ESLint messages, whose text is all the gates snapshot diff holds. Not touched: Markdown (docs and
the changelog keep their history), Alembic migrations, TV episode notation (`S2 · E5`), DESIGN.md
ids (`C1`, `B4`, `S3h`), tracker ids (`SEC-16`, `DX-17`).

One stale claim fixed on the way: test_hls_reaper's docstring said the player checkpoints "every
10 s whether or not it is playing", which plex/stream.py calls wrong; it now says what the code does.
2026-10-05 00:19:39 +02:00

80 lines
3.4 KiB
TypeScript

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import { LS } from "../lib/storage";
// Endonyms (shown as-is regardless of the active language — the convention for language pickers).
export const LANGUAGES = [
{ code: "en", label: "English" },
{ code: "hu", label: "Magyar" },
] as const;
export type LangCode = (typeof LANGUAGES)[number]["code"];
const SUPPORTED = LANGUAGES.map((l) => l.code) as readonly string[];
const LANG_KEY = LS.lang;
// Auto-load every locale file (locales/<lang>/<area>.json) and merge each <area> under one
// `translation` namespace, so components use t("area.key") and adding an area needs no wiring.
const mods = import.meta.glob("./locales/*/*.json", { eager: true }) as Record<
string,
{ default: Record<string, unknown> }
>;
const resources: Record<string, { translation: Record<string, unknown> }> = {};
for (const [path, mod] of Object.entries(mods)) {
const m = path.match(/\/locales\/([a-z]{2})\/([a-zA-Z0-9_]+)\.json$/);
if (!m) continue;
const [, lang, area] = m;
if (!lang || !area) continue; // both groups are required by the regex; satisfies the checker
(resources[lang] ??= { translation: {} }).translation[area] = mod.default;
}
export function isSupported(code: string | null | undefined): code is LangCode {
return !!code && SUPPORTED.includes(code);
}
// Initial language before the server preference is known: stored choice, else the browser
// language, else English. (Signed in, BootGate adopts the account's preference before the app renders.)
function detectInitialLang(): LangCode {
const stored = localStorage.getItem(LANG_KEY);
if (isSupported(stored)) return stored;
const nav = (navigator.language || "en").slice(0, 2).toLowerCase();
return isSupported(nav) ? nav : "en";
}
// Dev only: a locale edit re-runs THIS module (it accepts its own update, below) instead of climbing to
// main.tsx — a re-run main.tsx called createRoot on the live container a second time, and the two roots
// tore each other's DOM down ("removeChild … not a child" by the dozen, each one a fatal toast that evicted
// an old notification). On a re-run the i18next singleton is already up: swap the texts in and re-render.
if (i18n.isInitialized) {
for (const [lng, r] of Object.entries(resources))
i18n.addResourceBundle(lng, "translation", r.translation, true, true);
void i18n.changeLanguage(i18n.language);
} else {
const initial = detectInitialLang();
void i18n.use(initReactI18next).init({
resources,
lng: initial,
fallbackLng: "en",
interpolation: { escapeValue: false },
returnNull: false,
});
document.documentElement.lang = initial;
}
if (import.meta.hot) import.meta.hot.accept();
export function setLanguage(code: LangCode): void {
void i18n.changeLanguage(code);
localStorage.setItem(LANG_KEY, code);
document.documentElement.lang = code;
}
// The Plex overlay serves only EN or HU metadata. Collapse a UI language to one of those for
// the `?lang=` param / query keys. It takes the language instead of reading the singleton:
// under the React Compiler a component's `plexLang()` was a value with no reactive input, so it kept
// the language the component first rendered in — a switch left every Plex query on the old language.
// A component passes `i18n.language` from useTranslation; non-component code the singleton's.
export function plexLang(lang: string | undefined): "en" | "hu" {
return lang?.startsWith("hu") ? "hu" : "en";
}
export default i18n;