import { createContext, useContext, useState, type ReactNode } from "react"; import type { Lang, Translations } from "./translations"; import { translations } from "./translations"; interface LanguageContextValue { lang: Lang; setLang: (l: Lang) => void; t: Translations; } const LanguageContext = createContext(null); export function LanguageProvider({ children }: { children: ReactNode }) { const [lang, setLang] = useState(() => { try { return (localStorage.getItem("lang") as Lang) || "zh"; } catch { return "zh"; } }); const handleSetLang = (l: Lang) => { setLang(l); try { localStorage.setItem("lang", l); } catch {} }; return ( {children} ); } export function useLang() { const ctx = useContext(LanguageContext); if (!ctx) throw new Error("useLang must be used within LanguageProvider"); return ctx; }