"use client"; import { createContext, useContext, useEffect, useMemo, useState } from "react"; import { formatMessage, getInitialLocaleFromNavigator, Locale, LOCALE_STORAGE_KEY, normalizeLocale, } from "@/lib/i18n"; interface I18nContextValue { locale: Locale; setLocale: (locale: Locale) => void; toggleLocale: () => void; t: (key: string, params?: Record) => string; } const I18nContext = createContext(null); export function I18nProvider({ children }: { children: React.ReactNode }) { const [locale, setLocale] = useState(() => { if (typeof window === "undefined") { return "zh-CN"; } const stored = window.localStorage.getItem(LOCALE_STORAGE_KEY); return stored ? normalizeLocale(stored) : getInitialLocaleFromNavigator(); }); useEffect(() => { if (typeof window === "undefined") return; window.localStorage.setItem(LOCALE_STORAGE_KEY, locale); document.documentElement.lang = locale; }, [locale]); const value = useMemo( () => ({ locale, setLocale, t: (key, params) => formatMessage(locale, key, params), toggleLocale: () => { setLocale((current) => (current === "zh-CN" ? "en-US" : "zh-CN")); }, }), [locale], ); return {children}; } export function useI18n() { const context = useContext(I18nContext); if (!context) { throw new Error("useI18n must be used within I18nProvider"); } return context; }