Files
webspeak3/web/src/i18n.tsx
T
MoepchiandClaude Opus 5.5 eba1e1af92 Web: smaller review fixes (A10-A19, S10, S11, lint)
- A10: drop labels of unimplemented shortcuts, localize "Ctrl", never
  fire shortcuts (Ctrl+D!) while typing in a field
- A11: in-memory localStorage stand-in when storage is blocked instead
  of a blank page
- A12: invite link falls back to selecting the text without clipboard API
- A13: AEC3 script honors BASE_URL, failed loads are retried
- A14: shared downloadBlob helper, object URLs revoked with a delay
- A15: remaining hardcoded German/English strings moved to the locales
- A16: broken soundpack files show an error instead of failing silently
- A17: connect buttons no longer pass the click event as overrides
- A18: design base persisted from the resolved theme
- A19: no audio send() while the socket is still connecting
- S10: privilege keys are no longer persisted in localStorage
- S11: share links can carry the token in the fragment (#token=)
- oxlint: 0 warnings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
2026-09-27 19:20:47 +00:00

92 lines
3.0 KiB
TypeScript

/* eslint-disable react/only-export-components -- provider and its hooks belong together; only costs fast refresh */
import { createContext, useContext, useEffect, useState, type ReactNode } from "react";
import de from "./locales/de.json";
import en from "./locales/en.json";
import zhCN from "./locales/zh-CN.json";
import fa from "./locales/fa.json";
export type Lang = "de" | "en" | "zh-CN" | "fa";
const RTL_LANGS = new Set<Lang>(["fa"]);
export type LangPref = "auto" | Lang;
const LANGUAGE_KEY = "webspeak3:language";
// Keyed flat dictionary, one JSON file per language under locales/. Every
// user-facing string in the app should have an entry in every file - falls
// back to the English string (or the raw key) if a translation is missing,
// so a partial dictionary never blanks out the UI. Plain JSON (not inline
// TS objects) so translation tools like Weblate can read/write these files
// directly.
const translations: Record<Lang, Record<string, string>> = {
de,
en,
"zh-CN": zhCN,
fa,
};
function detectSystemLang(): Lang {
const nav = typeof navigator !== "undefined" ? navigator.language : "en";
const normalized = nav?.toLowerCase() ?? "en";
if (normalized.startsWith("zh")) return "zh-CN";
if (normalized.startsWith("fa")) return "fa";
return normalized.startsWith("de") ? "de" : "en";
}
export function resolveLang(pref: LangPref): Lang {
return pref === "auto" ? detectSystemLang() : pref;
}
export function loadLangPref(): LangPref {
const raw = localStorage.getItem(LANGUAGE_KEY);
return raw === "de" || raw === "en" || raw === "zh-CN" || raw === "fa" || raw === "auto" ? raw : "auto";
}
export function saveLangPref(pref: LangPref) {
localStorage.setItem(LANGUAGE_KEY, pref);
}
type TranslateFn = (key: string, vars?: Record<string, string>) => string;
function interpolate(template: string, vars?: Record<string, string>): string {
if (!vars) return template;
return template.replace(/\{(\w+)\}/g, (match, name) => vars[name] ?? match);
}
const LanguageContext = createContext<{ lang: Lang; langPref: LangPref; setLangPref: (p: LangPref) => void; t: TranslateFn }>({
lang: "en",
langPref: "auto",
setLangPref: () => {},
t: (key) => translations.en[key] ?? key,
});
export function LanguageProvider({ children }: { children: ReactNode }) {
const [langPref, setLangPrefState] = useState<LangPref>(() => loadLangPref());
const lang = resolveLang(langPref);
useEffect(() => {
saveLangPref(langPref);
}, [langPref]);
useEffect(() => {
document.documentElement.lang = lang;
document.documentElement.dir = RTL_LANGS.has(lang) ? "rtl" : "ltr";
}, [lang]);
const t: TranslateFn = (key, vars) =>
interpolate(translations[lang][key] ?? translations.en[key] ?? key, vars);
return (
<LanguageContext.Provider value={{ lang, langPref, setLangPref: setLangPrefState, t }}>
{children}
</LanguageContext.Provider>
);
}
export function useLanguage() {
return useContext(LanguageContext);
}
export function useT() {
return useContext(LanguageContext).t;
}