I have a react app that I have translated into several languages, and also contains some date formatting functionality. I would like to for example, allow the user to change between 12h and 24h time, however when switching to German, automatically change that option to 24h (as Germany typically doesn't use 12h time).
I still want this option to be able to be changed by the user, and I also don't want it to be reset to 24h every time the page reloads.
I'm using i18next
and i18next-browser-languageDetector
to detect the browser language, and currently I have an event bound to i18next
for when the language is manually changed by the user, in order to set these default settings per language, and this works perfectly:
i18n.on('languageChanged', lang => {
// Language specific options
if (lang === 'de') {
store.setTimeFormat('24h');
}
if (lang === 'en') {
store.setTimeFormat('12h');
}
});
Now, the only problem is that this function is not triggered when first loading the app, i.e. when the browser detector first detects the user's language.
How can I determine when i18next-browser-languageDetector
initially detects the language, that is, not loading from localstorage, but a query string of lng=de
for example would trigger it, so that I can set these settings?