Files
zeekay 3aaf80ad3f Update content for Parsis Foundation
- Transform from personal site to foundation/NGO site
- Focus on education, research, AI, cryptography, open governance
- Update all translations and navigation
- Update package.json metadata
2026-02-04 18:35:04 -08:00

52 lines
1.3 KiB
TypeScript

'use client';
import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
import { Locale, getTranslation, isRTL, TranslationKeys } from './index';
interface I18nContextType {
locale: Locale;
setLocale: (locale: Locale) => void;
t: TranslationKeys;
isRTL: boolean;
}
const I18nContext = createContext<I18nContextType | undefined>(undefined);
export function I18nProvider({ children }: { children: ReactNode }) {
const [locale, setLocaleState] = useState<Locale>('en');
useEffect(() => {
const saved = localStorage.getItem('locale') as Locale | null;
if (saved && (saved === 'en' || saved === 'fa')) {
setLocaleState(saved);
}
}, []);
useEffect(() => {
document.documentElement.lang = locale;
document.documentElement.dir = isRTL(locale) ? 'rtl' : 'ltr';
}, [locale]);
const setLocale = (newLocale: Locale) => {
setLocaleState(newLocale);
localStorage.setItem('locale', newLocale);
};
const value: I18nContextType = {
locale,
setLocale,
t: getTranslation(locale),
isRTL: isRTL(locale),
};
return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
}
export function useI18n() {
const context = useContext(I18nContext);
if (!context) {
throw new Error('useI18n must be used within I18nProvider');
}
return context;
}