I import an object that i have on a constant file in my functional component, but it doesn't get translated according to the selected language.
I guess it's cause of timing, probably the language hasn't been loaded yet.
I cannot change the labels into string for several reason, so i need to understand how to update the i18n according to the language change.
so which is the best way to handle this?
is there a way to wait till the languages is loaded?
export const myTypes: FormSelectOptions = [
{ value: 'alpha', label: i18next.t('alpha.string') },
{ value: 'beta', label: i18next.t('beta.string') },
];
my i18n config
import Backend from 'i18next-xhr-backend';
import i18next from 'i18next';
i18next
.use(Backend)
.use(initReactI18next)
.init({
backend: {
loadPath: './translations/{{lng}}.json',
addPath: './translations/{{lng}}/{{ns}}.missing.json',
crossDomain: true,
},
resources: {
en: {
main: require('./translations/en.json'), // eslint-disable-line
},
it: {
main: require('./translations/it.json'), // eslint-disable-line
},
},
lng: 'it',
defaultNS: 'main',
saveMissing: false,
saveMissingTo: 'all',
fallbackNS: process.env.NODE_ENV === 'development' ? false : 'main',
fallbackLng: process.env.NODE_ENV === 'development' ? false : 'en',
debug: process.env.NODE_ENV === 'development',
});
export default i18next;
```