Set language defaults on initial load

This commit is contained in:
Ben Grant 2021-05-31 23:14:36 +10:00
parent 615f316be2
commit 2f23fcfd32
2 changed files with 17 additions and 5 deletions

View file

@ -16,6 +16,14 @@ import {
import locales from 'res/dayjs_locales';
// Language specific options
const setDefaults = (lang, store) => {
if (locales.hasOwnProperty(lang)) {
store.setWeekStart(locales[lang].weekStart);
store.setTimeFormat(locales[lang].timeFormat);
}
};
const Settings = () => {
const theme = useTheme();
const store = useSettingsStore();
@ -36,12 +44,13 @@ const Settings = () => {
}
}, [i18n.language, setLocale]);
if (!i18n.options.storedLang) {
setDefaults(i18n.language, store);
i18n.options.storedLang = i18n.language;
}
i18n.on('languageChanged', lang => {
// Language specific options
if (locales.hasOwnProperty(lang)) {
store.setWeekStart(locales[lang].weekStart);
store.setTimeFormat(locales[lang].timeFormat);
}
setDefaults(lang, store);
});
return (

View file

@ -5,6 +5,8 @@ import Backend from 'i18next-http-backend';
import locales from 'res/dayjs_locales';
const storedLang = localStorage.getItem('i18nextLng');
i18n
.use(LanguageDetector)
.use(Backend)
@ -21,6 +23,7 @@ i18n
backend: {
loadPath: '/i18n/{{lng}}/{{ns}}.json',
},
storedLang,
}).then(() => document.documentElement.setAttribute('lang', i18n.language));
export default i18n;