import { createContext, useContext, useState, useCallback, type ReactNode } from 'react'; import { translations, type Lang, type TranslationKey } from './translations'; interface I18nContextType { lang: Lang; setLang: (lang: Lang) => void; t: (key: TranslationKey) => string; } const I18nContext = createContext(null); export function I18nProvider({ children }: { children: ReactNode }) { const [lang, setLang] = useState(() => { const saved = localStorage.getItem('proserve-lang'); return (saved === 'en' || saved === 'fr') ? saved : 'fr'; }); const changeLang = useCallback((newLang: Lang) => { setLang(newLang); localStorage.setItem('proserve-lang', newLang); }, []); const t = useCallback((key: TranslationKey): string => { return translations[key]?.[lang] || key; }, [lang]); return ( {children} ); } export function useI18n() { const context = useContext(I18nContext); if (!context) throw new Error('useI18n must be used within I18nProvider'); return context; }