Skip to content

Commit

Permalink
fix: update date-fns locale when language changes
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed May 30, 2024
1 parent c208d35 commit 41c7af7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/ui/src/services/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { initReactI18next } from 'react-i18next';

export let dateFnsLocale = undefined;

async function setDateFnsLocale(lng: string) {
dateFnsLocale = await import(`date-fns/locale/${lng}/index.js`).catch((e) => console.error(e));
}

export async function initI18n({ lng, basePath }: { lng: string; basePath: string }) {
const i18nextInstance = i18n
.use(initReactI18next) // passes i18n down to react-i18next
Expand All @@ -16,7 +20,10 @@ export async function initI18n({ lng, basePath }: { lng: string; basePath: strin
i18nextInstance.use(new HMRPlugin({ webpack: { client: true } }));
(window as any).testI18n = (lng = 'cimode') => i18nextInstance.changeLanguage(lng);
}
dateFnsLocale = await import(`date-fns/locale/${lng}/index.js`).catch((e) => console.error(e));

i18nextInstance.on('languageChanged', (lng) => setDateFnsLocale(lng));
await setDateFnsLocale(lng);

return i18nextInstance.init({
lng,
fallbackLng: 'en-US',
Expand Down

0 comments on commit 41c7af7

Please sign in to comment.