Further work on localization

This commit is contained in:
Tomas Bures 2018-12-15 15:15:48 +01:00
parent fa451fc8da
commit cb1fc5b28d
35 changed files with 430 additions and 2796 deletions

View file

@ -4,25 +4,24 @@ const config = require('config');
const i18n = require("i18next");
const fs = require('fs');
const path = require('path');
const {convertToFake, langCodes} = require('../../shared/langs');
const {convertToFake, getLang} = require('../../shared/langs');
const resourcesCommon = {};
function loadLanguage(shortCode) {
resourcesCommon[shortCode] = {
common: JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'locales', shortCode, 'common.json')))
function loadLanguage(longCode) {
resourcesCommon[longCode] = {
common: JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'locales', longCode, 'common.json')))
};
}
loadLanguage('en');
loadLanguage('es');
resourcesCommon.fake = convertToFake(resourcesCommon.en);
loadLanguage('en-US');
resourcesCommon['fk-FK'] = convertToFake(resourcesCommon['en-US']);
const resources = {};
for (const lng of config.enabledLanguages) {
const shortCode = langCodes[lng].shortCode;
resources[shortCode] = {
common: resourcesCommon[shortCode]
const langDesc = getLang(lng);
resources[langDesc.longCode] = {
common: resourcesCommon[langDesc.longCode]
};
}
@ -34,6 +33,9 @@ i18n
fallbackLng: config.defaultLanguage,
defaultNS: 'common',
whitelist: config.enabledLanguages,
load: 'currentOnly',
debug: false
})
@ -47,18 +49,29 @@ function tLog(key, args) {
return JSON.stringify([key, args]);
}
function tUI(key, lang, args) {
function tUI(key, locale, args) {
if (!args) {
args = {};
}
return i18n.t(key, { ...args, lng: lang });
return i18n.t(key, { ...args, lng: getLangCodeFromExpressLocale(locale) });
}
function tMark(key) {
return key;
}
function getLangCodeFromExpressLocale(locale) {
const longCode = locale.toString().replace('_', '-');
if (longCode in resources) {
return longCode;
} else {
return config.defaultLanguage
}
}
module.exports.tLog = tLog;
module.exports.tUI = tUI;
module.exports.tMark = tMark;
module.exports.tMark = tMark;
module.exports.getLangCodeFromExpressLocale = getLangCodeFromExpressLocale;