2 The i18n module, loads the language files and initializes i18next
6 const i18n = require('i18next')
7 const extend = require('lodash/extend')
9 const LanguageDetector = require('i18next-browser-languagedetector')
12 const supported_languages = ['en', 'zh']
15 dev: { translations: require('./interface/i18n/desktop.en.i18n.json') },
18 // add supported languages
19 supported_languages.forEach((lang) => {
20 const desktopTranslations = require(`./interface/i18n/desktop.${lang}.i18n.json`)
21 const uiTranslations = require(`../src/locales/${lang}/translation.json`)
22 resources[lang] = { translations: extend(desktopTranslations, uiTranslations) }
26 * Given a language code, get best matched code from supported languages.
28 * > getBestMatchedLangCode('en-US')
30 * > getBestMatchedLangCode('zh-TW')
32 * > getBestMatchedLangCode('no-such-code')
35 i18n.getBestMatchedLangCode = (langCode) => {
36 const codeList = Object.keys(resources)
37 let bestMatchedCode = langCode
38 if (codeList.indexOf(langCode) === -1) {
39 if (codeList.indexOf(langCode.substr(0, 2)) > -1) {
40 bestMatchedCode = langCode.substr(0, 2)
42 bestMatchedCode = 'en'
45 return bestMatchedCode
48 i18n.use(LanguageDetector).init({
53 escapeValue: false, // not needed for react!!
60 defaultNS: 'translations',
63 // order and from where user language should be detected
64 order: ['localStorage', 'querystring', 'cookie', 'navigator', 'htmlTag', 'path', 'subdomain']