import React, { createContext, useContext, useState, ReactNode } from "react"; import { translations, Language, TranslationModule, TranslationKey } from "@/translations"; type LanguageContextType = { language: Language; setLanguage: (language: Language) => void; t: (key: string, module?: M) => string; }; // ❗ Create the context with `undefined` to enforce provider usage const LanguageContext = createContext(undefined); // ✅ Stable custom hook export const useLanguage = () => { const context = useContext(LanguageContext); if (!context) { throw new Error("useLanguage must be used within a LanguageProvider"); } return context; }; export const LanguageProvider = ({ children }: { children: ReactNode }) => { const [language, setLanguage] = useState("en"); const t = (key: string, module?: M): string => { if (module) { const translatedValue = translations[language][module][key as TranslationKey]; return typeof translatedValue === "string" ? translatedValue : key; } for (const mod in translations[language]) { const moduleKey = mod as TranslationModule; const translatedValue = translations[language][moduleKey][key as any]; if (translatedValue && typeof translatedValue === "string") { return translatedValue; } } return key; }; return ( {children} ); };