games_app/prediqo_web/src/shared/i18n/index.ts
2026-09-23 16:28:14 +02:00

45 lines
1.2 KiB
TypeScript

/**
* i18n bootstrap — English and French for the Tloto web app.
*/
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { en } from './locales/en';
import { fr } from './locales/fr';
export const LOCALE_STORAGE_KEY = 'prediqo.locale';
export const SUPPORTED_LOCALES = ['en', 'fr'] as const;
export type AppLocale = (typeof SUPPORTED_LOCALES)[number];
export function normalizeLocale(lng: string | undefined): AppLocale {
if (!lng) return 'en';
return lng.toLowerCase().startsWith('fr') ? 'fr' : 'en';
}
void i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: {
en: { translation: en },
fr: { translation: fr },
},
fallbackLng: 'en',
supportedLngs: [...SUPPORTED_LOCALES],
interpolation: { escapeValue: false },
detection: {
order: ['localStorage', 'navigator'],
lookupLocalStorage: LOCALE_STORAGE_KEY,
caches: ['localStorage'],
},
});
i18n.on('languageChanged', (lng) => {
document.documentElement.lang = normalizeLocale(lng);
});
document.documentElement.lang = normalizeLocale(i18n.language);
export default i18n;