The locale JSON's importer did not accept HMR, so the update climbed to
main.tsx, whose re-run called createRoot on the live container again; the two
roots tore each other's DOM down ('removeChild ... not a child' by the dozen),
each a fatal toast evicting an old notification. Dev only.
78 lines
3.3 KiB
TypeScript
78 lines
3.3 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 (E5 S6). Collapse the active UI language to one of
|
|
// those for the `?lang=` param / query keys. Components read `i18n.language` off useTranslation (which
|
|
// re-renders on switch); this singleton reader is for non-component code (e.g. plexPlayerHooks).
|
|
export function plexLang(): "en" | "hu" {
|
|
return i18n.language?.startsWith("hu") ? "hu" : "en";
|
|
}
|
|
|
|
export default i18n;
|