2 The i18n module, loads the language files and initializes i18next
6 const fs = require('fs')
7 const i18n = require('i18next')
9 let i18nConf = fs.readFileSync(`${__dirname}/interface/project-tap.i18n`)
10 i18nConf = JSON.parse(i18nConf)
13 dev: { translation: require('./interface/i18n/desktop.en.i18n.json') },
16 // add supported languages
17 i18nConf.supported_languages.forEach((lang) => {
18 resources[lang] = { translation: require(`./interface/i18n/desktop.${lang}.i18n.json`) }
22 * Given a language code, get best matched code from supported languages.
24 * > getBestMatchedLangCode('en-US')
26 * > getBestMatchedLangCode('zh-TW')
28 * > getBestMatchedLangCode('no-such-code')
31 i18n.getBestMatchedLangCode = (langCode) => {
32 const codeList = Object.keys(resources)
33 let bestMatchedCode = langCode
34 if (codeList.indexOf(langCode) === -1) {
35 if (codeList.indexOf(langCode.substr(0, 2)) > -1) {
36 bestMatchedCode = langCode.substr(0, 2)
38 bestMatchedCode = 'en'
41 return bestMatchedCode
45 lng: global.language || 'en',
48 interpolation: { prefix: '__', suffix: '__' },