mirror of
https://github.com/parsis-foundation/parsis.foundation.git
synced 2026-10-30 20:47:22 +00:00
- 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
52 lines
1.3 KiB
TypeScript
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;
|
|
}
|