jiti-meet/react/features/base/i18n/i18next.js

105 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-01-26 18:18:43 +00:00
// @flow
declare var APP: Object;
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';
import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
import MAIN_RESOURCES from '../../../../lang/main.json';
import { I18NEXT_INITIALIZED, LANGUAGE_CHANGED } from './actionTypes';
2017-03-01 02:55:12 +00:00
import languageDetector from './languageDetector';
/**
* 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);
/**
* The default language.
*
* English is the default language.
*
* @public
* @type {string} The default language.
*/
export const DEFAULT_LANGUAGE = 'en';
2017-03-01 02:55:12 +00:00
/**
* The options to initialize i18next with.
*
* @type {Object}
*/
const options = {
backend: {
loadPath: 'lang/{{ns}}-{{lng}}.json'
},
defaultNS: 'main',
2017-03-01 02:55:12 +00:00
fallbackLng: DEFAULT_LANGUAGE,
interpolation: {
escapeValue: false // not needed for react as it escapes by default
},
load: 'languageOnly',
ns: [ 'main', 'languages', 'countries' ],
react: {
// re-render when a new resource bundle is added
bindI18nStore: 'added',
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.
whitelist: LANGUAGES.slice()
2017-03-01 02:55:12 +00:00
};
i18next
.use(navigator.product === 'ReactNative' ? {} : I18nextXHRBackend)
2017-03-01 02:55:12 +00:00
.use(languageDetector)
.init(options);
// Add default language which is preloaded from the source code.
i18next.addResourceBundle(
DEFAULT_LANGUAGE,
'countries',
COUNTRIES_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
'languages',
LANGUAGES_RESOURCES,
2017-06-15 00:40:51 +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
'main',
MAIN_RESOURCES,
2017-06-15 00:40:51 +00:00
/* deep */ true,
/* overwrite */ true);
2017-03-01 02:55:12 +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.
require('./BuiltinLanguages');
// 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;