Spaces:
Paused
Paused
File size: 1,406 Bytes
b7d4394 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import { createContext } from "preact";
import { useContext, useState, useCallback } from "preact/hooks";
import { translations, type LangCode, type TranslationKey } from "./translations";
import type { ComponentChildren } from "preact";
interface I18nContextValue {
lang: LangCode;
toggleLang: () => void;
t: (key: TranslationKey) => string;
}
const I18nContext = createContext<I18nContextValue>(null!);
function getInitialLang(): LangCode {
try {
const saved = localStorage.getItem("codex-proxy-lang");
if (saved === "en" || saved === "zh") return saved;
} catch {}
return navigator.language.startsWith("zh") ? "zh" : "en";
}
export function I18nProvider({ children }: { children: ComponentChildren }) {
const [lang, setLang] = useState<LangCode>(getInitialLang);
const toggleLang = useCallback(() => {
setLang((prev) => {
const next = prev === "en" ? "zh" : "en";
localStorage.setItem("codex-proxy-lang", next);
return next;
});
}, []);
const t = useCallback(
(key: TranslationKey): string => {
return translations[lang][key] ?? translations.en[key] ?? key;
},
[lang]
);
return (
<I18nContext.Provider value={{ lang, toggleLang, t }}>
{children}
</I18nContext.Provider>
);
}
export function useT() {
return useContext(I18nContext).t;
}
export function useI18n() {
return useContext(I18nContext);
}
|