I use react-i18next
and i18next
to localize an app. I want to be able to change the language manually in the settings and persist that change.
The problem is i dont know how to use redux in the i18n
config file as redux is async, also i've checked the docs for some built in storage option in react-i18next
but without success.
here is the i18n
config file:
'use strict';
import i18n from 'i18next';
import {reactI18nextModule, initReactI18next} from 'react-i18next';
import DeviceInfo from 'react-native-device-info';
import translationFR from '../translation/fr/translation.json';
import translationEN from '../translation/en/translation.json';
import translationAR from '../translation/ar/translation.json';
async function getAppLang() {
const unsubscribe = store.subscribe(() => true);
const appLang = await store.getState().appLang;
await unsubscribe();
return appLang;
}
let locale = DeviceInfo.getDeviceLocale().substring(0, 2);
if (locale != 'fr' && locale != 'ar') {
locale = 'en';
}
// the translations
const resources = {
en: translationEN,
fr: translationFR,
ar: translationAR,
};
i18n
.use(reactI18nextModule) // passes i18n down to react-i18next
.init({
resources: resources,
lng: locale,
fallbackLng: ['en', 'fr', 'ar'],
keySeparator: false, // we do not use keys in form messages.welcome
interpolation: {
escapeValue: false, // react already safes from xss
},
});
export default i18n;
To change the language i use this function:
import i18n from 'config/i18n';
////
i18n.changeLanguage(langageName);
THANKS !