mailtrain/server/lib/translate.js
Tomas Bures dc7789c17b Extracted strings and fixes on localization support
Language chooser in the UI
2018-11-18 21:31:22 +01:00

64 lines
No EOL
1.3 KiB
JavaScript

'use strict';
const config = require('config');
const i18n = require("i18next");
const fs = require('fs');
const path = require('path');
const {convertToFake, langCodes} = require('../../shared/langs');
const resourcesCommon = {};
function loadLanguage(shortCode) {
resourcesCommon[shortCode] = {
common: JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'locales', shortCode, 'common.json')))
};
}
loadLanguage('en');
loadLanguage('es');
resourcesCommon.fake = convertToFake(resourcesCommon.en);
const resources = {};
for (const lng of config.enabledLanguages) {
const shortCode = langCodes[lng].shortCode;
resources[shortCode] = {
common: resourcesCommon[shortCode]
};
}
i18n
.init({
resources,
wait: true, // globally set to wait for loaded translations in translate hoc
fallbackLng: config.defaultLanguage,
defaultNS: 'common',
debug: false
})
function tLog(key, args) {
if (!args) {
args = {};
}
return JSON.stringify([key, args]);
}
function tUI(key, lang, args) {
if (!args) {
args = {};
}
return i18n.t(key, { ...args, defaultValue, lng: lang });
}
function tMark(key) {
return key;
}
module.exports.tLog = tLog;
module.exports.tUI = tUI;
module.exports.tMark = tMark;