Dynamic import of dayjs locale

This commit is contained in:
Ben Grant 2021-05-24 21:48:03 +10:00
parent a1677094f7
commit 6ed9c1e226
2 changed files with 10 additions and 12 deletions

View file

@ -90,19 +90,18 @@ const CalendarField = ({
}; };
useEffect(() => { useEffect(() => {
let lang = i18n.language;
if (Array.from(supportedLocales).includes(i18n.language)) { if (Array.from(supportedLocales).includes(i18n.language)) {
require(`dayjs/locale/${i18n.language}`); import(`dayjs/locale/${i18n.language}.js`).then(() => {
dayjs.locale(i18n.language); dayjs.locale(i18n.language);
lang = i18n.language; if (weekStart !== dayjs.Ls[i18n.language].weekStart) {
dayjs.updateLocale(i18n.language, { weekStart });
}
});
} else { } else {
// Fallback // Fallback
lang = 'en'; if (weekStart !== dayjs.Ls.en.weekStart) {
dayjs.updateLocale('en', { weekStart });
} }
if (weekStart !== dayjs.Ls[lang].weekStart) {
dayjs.updateLocale(lang, {
weekStart,
});
} }
setDates(calculateMonth(month, year, weekStart)); setDates(calculateMonth(month, year, weekStart));
}, [weekStart, month, year, i18n.language]); }, [weekStart, month, year, i18n.language]);

View file

@ -77,8 +77,7 @@ const Event = (props) => {
useEffect(() => { useEffect(() => {
if (Array.from(supportedLocales).includes(i18n.language)) { if (Array.from(supportedLocales).includes(i18n.language)) {
require(`dayjs/locale/${i18n.language}`); import(`dayjs/locale/${i18n.language}.js`).then(() => dayjs.locale(i18n.language));
dayjs.locale(i18n.language);
} }
}, [i18n.language]); }, [i18n.language]);