File size: 1,014 Bytes
831f7e7 |
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 |
import { createContext, useState, useEffect, ReactNode } from 'react';
import { Language } from '@/i18n/translations';
interface LanguageContextType {
language: Language;
setLanguage: (lang: Language) => void;
}
export const LanguageContext = createContext<LanguageContextType>({
language: 'en',
setLanguage: () => {},
});
interface LanguageProviderProps {
children: ReactNode;
}
export const LanguageProvider = ({ children }: LanguageProviderProps) => {
const [language, setLanguage] = useState<Language>('en');
useEffect(() => {
const savedLang = localStorage.getItem('language') as Language;
if (savedLang && ['en', 'fr', 'de', 'it', 'es'].includes(savedLang)) {
setLanguage(savedLang);
}
}, []);
const handleSetLanguage = (lang: Language) => {
setLanguage(lang);
localStorage.setItem('language', lang);
};
return (
<LanguageContext.Provider value={{ language, setLanguage: handleSetLanguage }}>
{children}
</LanguageContext.Provider>
);
}; |