2020-07-06 11:36:45 +02:00
|
|
|
import i18n from 'i18next';
|
|
|
|
import { initReactI18next } from 'react-i18next';
|
|
|
|
import LanguageDetector from 'i18next-browser-languagedetector';
|
|
|
|
import Backend from 'i18next-http-backend';
|
|
|
|
|
|
|
|
const fallbackLng = ['en-US'];
|
|
|
|
const availableLanguages = ['en-US', 'es-419'];
|
|
|
|
|
2020-08-27 17:00:04 +02:00
|
|
|
export function languageKeyToLabel(lang) {
|
|
|
|
const languageMap = {
|
|
|
|
'en-US': 'English',
|
|
|
|
'es-419': 'Español'
|
|
|
|
};
|
|
|
|
return languageMap[lang];
|
|
|
|
}
|
|
|
|
|
2020-07-06 11:36:45 +02:00
|
|
|
const options = {
|
2020-07-20 20:35:34 +02:00
|
|
|
loadPath: '/locales/{{lng}}/translations.json',
|
2020-07-06 11:36:45 +02:00
|
|
|
requestOptions: { // used for fetch, can also be a function (payload) => ({ method: 'GET' })
|
|
|
|
mode: 'no-cors'
|
|
|
|
},
|
|
|
|
allowMultiLoading: false, // set loadPath: '/locales/resources.json?lng={{lng}}&ns={{ns}}' to adapt to multiLoading
|
|
|
|
};
|
|
|
|
|
|
|
|
i18n
|
|
|
|
.use(initReactI18next) // pass the i18n instance to react-i18next.
|
2020-07-31 15:20:42 +02:00
|
|
|
// .use(LanguageDetector)// to detect the language from currentBrowser
|
2020-07-06 11:36:45 +02:00
|
|
|
.use(Backend) // to fetch the data from server
|
|
|
|
.init({
|
|
|
|
lng: 'en-US',
|
|
|
|
fallbackLng, // if user computer language is not on the list of available languages, than we will be using the fallback language specified earlier
|
|
|
|
debug: false,
|
|
|
|
backend: options,
|
|
|
|
getAsync: false,
|
|
|
|
initImmediate: false,
|
|
|
|
useSuspense: true,
|
|
|
|
whitelist: availableLanguages,
|
|
|
|
interpolation: {
|
|
|
|
escapeValue: false, // react already safes from xss
|
|
|
|
},
|
|
|
|
saveMissing: false, // if a key is not found AND this flag is set to true, i18next will call the handler missingKeyHandler
|
|
|
|
missingKeyHandler: false // function(lng, ns, key, fallbackValue) { } custom logic about how to handle the missing keys
|
|
|
|
});
|
|
|
|
|
|
|
|
export default i18n;
|