2020-05-20 10:57:03 +00:00
|
|
|
import COUNTRIES_RESOURCES from 'i18n-iso-countries/langs/en.json';
|
2017-03-01 02:55:12 +00:00
|
|
|
import i18next from 'i18next';
|
|
|
|
import I18nextXHRBackend from 'i18next-xhr-backend';
|
2022-02-28 08:14:20 +00:00
|
|
|
import _ from 'lodash';
|
2017-03-01 02:55:12 +00:00
|
|
|
|
|
|
|
import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
|
|
|
|
import MAIN_RESOURCES from '../../../../lang/main.json';
|
2022-09-29 11:44:59 +00:00
|
|
|
import TRANSLATION_LANGUAGES_RESOURCES from '../../../../lang/translation-languages.json';
|
2017-03-01 02:55:12 +00:00
|
|
|
|
2021-12-07 10:04:33 +00:00
|
|
|
import { I18NEXT_INITIALIZED, LANGUAGE_CHANGED } from './actionTypes';
|
2017-03-01 02:55:12 +00:00
|
|
|
import languageDetector from './languageDetector';
|
|
|
|
|
2022-02-28 08:14:20 +00:00
|
|
|
/**
|
|
|
|
* Override certain country names.
|
|
|
|
*/
|
|
|
|
const COUNTRIES_RESOURCES_OVERRIDES = {
|
|
|
|
countries: {
|
|
|
|
TW: 'Taiwan'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merged country names.
|
|
|
|
*/
|
|
|
|
const COUNTRIES = _.merge({}, COUNTRIES_RESOURCES, COUNTRIES_RESOURCES_OVERRIDES);
|
|
|
|
|
2017-07-27 15:19:48 +00:00
|
|
|
/**
|
|
|
|
* The available/supported languages.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @type {Array<string>}
|
|
|
|
*/
|
2018-10-03 09:29:19 +00:00
|
|
|
export const LANGUAGES: Array<string> = Object.keys(LANGUAGES_RESOURCES);
|
2017-07-27 15:19:48 +00:00
|
|
|
|
2022-08-31 15:57:31 +00:00
|
|
|
/**
|
2022-09-29 11:44:59 +00:00
|
|
|
* The available/supported translation languages.
|
2022-08-31 15:57:31 +00:00
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @type {Array<string>}
|
|
|
|
*/
|
2022-09-29 11:44:59 +00:00
|
|
|
export const TRANSLATION_LANGUAGES: Array<string> = Object.keys(TRANSLATION_LANGUAGES_RESOURCES);
|
2022-08-31 15:57:31 +00:00
|
|
|
|
|
|
|
/**
|
2022-09-29 11:44:59 +00:00
|
|
|
* The available/supported translation languages head. (Languages displayed on the top ).
|
2022-08-31 15:57:31 +00:00
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @type {Array<string>}
|
|
|
|
*/
|
2022-09-29 11:44:59 +00:00
|
|
|
export const TRANSLATION_LANGUAGES_HEAD: Array<string> = [ 'en' ];
|
2022-08-31 15:57:31 +00:00
|
|
|
|
2017-07-27 15:19:48 +00:00
|
|
|
/**
|
|
|
|
* The default language.
|
|
|
|
*
|
2022-01-20 10:22:21 +00:00
|
|
|
* English is the default language.
|
2017-07-27 15:19:48 +00:00
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @type {string} The default language.
|
|
|
|
*/
|
2022-01-20 10:22:21 +00:00
|
|
|
export const DEFAULT_LANGUAGE = 'en';
|
2017-07-27 15:19:48 +00:00
|
|
|
|
2017-03-01 02:55:12 +00:00
|
|
|
/**
|
|
|
|
* The options to initialize i18next with.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
const options = {
|
2019-07-22 10:10:25 +00:00
|
|
|
backend: {
|
2019-07-22 14:56:59 +00:00
|
|
|
loadPath: 'lang/{{ns}}-{{lng}}.json'
|
2019-07-22 10:10:25 +00:00
|
|
|
},
|
|
|
|
defaultNS: 'main',
|
2017-03-01 02:55:12 +00:00
|
|
|
fallbackLng: DEFAULT_LANGUAGE,
|
2019-07-22 10:10:25 +00:00
|
|
|
interpolation: {
|
|
|
|
escapeValue: false // not needed for react as it escapes by default
|
|
|
|
},
|
|
|
|
load: 'languageOnly',
|
2022-09-29 11:44:59 +00:00
|
|
|
ns: [ 'main', 'languages', 'countries', 'translation-languages' ],
|
2019-07-22 10:10:25 +00:00
|
|
|
react: {
|
2021-12-07 10:04:33 +00:00
|
|
|
// re-render when a new resource bundle is added
|
|
|
|
bindI18nStore: 'added',
|
2019-07-22 10:10:25 +00:00
|
|
|
useSuspense: false
|
|
|
|
},
|
|
|
|
returnEmptyString: false,
|
|
|
|
returnNull: false,
|
2017-03-01 02:55:12 +00:00
|
|
|
|
|
|
|
// XXX i18next modifies the array lngWhitelist so make sure to clone
|
|
|
|
// LANGUAGES.
|
2019-07-22 10:10:25 +00:00
|
|
|
whitelist: LANGUAGES.slice()
|
2017-03-01 02:55:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
i18next
|
2017-12-22 09:25:36 +00:00
|
|
|
.use(navigator.product === 'ReactNative' ? {} : I18nextXHRBackend)
|
2022-08-01 07:14:54 +00:00
|
|
|
.use(languageDetector) // @ts-ignore
|
2017-03-01 02:55:12 +00:00
|
|
|
.init(options);
|
|
|
|
|
|
|
|
// Add default language which is preloaded from the source code.
|
2019-02-26 13:32:46 +00:00
|
|
|
i18next.addResourceBundle(
|
|
|
|
DEFAULT_LANGUAGE,
|
|
|
|
'countries',
|
2022-02-28 08:14:20 +00:00
|
|
|
COUNTRIES,
|
2019-02-26 13:32:46 +00:00
|
|
|
/* deep */ true,
|
|
|
|
/* overwrite */ true);
|
2017-03-01 02:55:12 +00:00
|
|
|
i18next.addResourceBundle(
|
2017-06-15 00:40:51 +00:00
|
|
|
DEFAULT_LANGUAGE,
|
2018-01-26 18:18:43 +00:00
|
|
|
'languages',
|
|
|
|
LANGUAGES_RESOURCES,
|
2017-06-15 00:40:51 +00:00
|
|
|
/* deep */ true,
|
|
|
|
/* overwrite */ true);
|
2022-09-29 11:44:59 +00:00
|
|
|
i18next.addResourceBundle(
|
|
|
|
DEFAULT_LANGUAGE,
|
|
|
|
'translation-languages',
|
|
|
|
TRANSLATION_LANGUAGES_RESOURCES,
|
|
|
|
/* deep */ true,
|
|
|
|
/* overwrite */ true);
|
2017-03-01 02:55:12 +00:00
|
|
|
i18next.addResourceBundle(
|
2017-06-15 00:40:51 +00:00
|
|
|
DEFAULT_LANGUAGE,
|
2018-01-26 18:18:43 +00:00
|
|
|
'main',
|
|
|
|
MAIN_RESOURCES,
|
2017-06-15 00:40:51 +00:00
|
|
|
/* deep */ true,
|
|
|
|
/* overwrite */ true);
|
2017-03-01 02:55:12 +00:00
|
|
|
|
2017-12-22 10:26:57 +00:00
|
|
|
// Add builtin languages.
|
2018-01-26 18:18:43 +00:00
|
|
|
// XXX: Note we are using require here, because we want the side-effects of the
|
|
|
|
// import, but imports can only be placed at the top, and it would be too early,
|
|
|
|
// since i18next is not yet initialized at that point.
|
2017-12-22 10:26:57 +00:00
|
|
|
require('./BuiltinLanguages');
|
|
|
|
|
2021-12-07 10:04:33 +00:00
|
|
|
// Label change through dynamic branding is available only for web
|
|
|
|
if (typeof APP !== 'undefined') {
|
|
|
|
i18next.on('initialized', () => {
|
|
|
|
APP.store.dispatch({ type: I18NEXT_INITIALIZED });
|
|
|
|
});
|
|
|
|
|
|
|
|
i18next.on('languageChanged', () => {
|
|
|
|
APP.store.dispatch({ type: LANGUAGE_CHANGED });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:55:12 +00:00
|
|
|
export default i18next;
|