2015-04-15 21:59:39 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-04-15 21:59:39 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2018-08-22 11:00:03 +02:00
|
|
|
* @ignore
|
2015-04-15 21:59:39 +02:00
|
|
|
**/
|
2015-07-19 23:10:46 +02:00
|
|
|
|
2018-12-05 14:00:25 +01:00
|
|
|
/**
|
|
|
|
* Internationalization utilities
|
|
|
|
* @mixin @node-red/util_i18n
|
|
|
|
*/
|
2018-08-28 14:45:38 +02:00
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
var i18n = require("i18next");
|
2018-07-24 00:25:57 +02:00
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
var path = require("path");
|
|
|
|
var fs = require("fs");
|
|
|
|
|
2015-04-26 00:29:53 +02:00
|
|
|
var defaultLang = "en-US";
|
|
|
|
|
2015-11-22 00:12:39 +01:00
|
|
|
var resourceMap = {};
|
|
|
|
var resourceCache = {};
|
2018-04-19 12:23:08 +02:00
|
|
|
var initPromise;
|
2015-04-15 21:59:39 +02:00
|
|
|
|
2018-08-28 14:45:38 +02:00
|
|
|
/**
|
|
|
|
* Register multiple message catalogs with i18n.
|
2018-12-05 14:00:25 +01:00
|
|
|
* @memberof @node-red/util_i18n
|
2018-08-28 14:45:38 +02:00
|
|
|
*/
|
2017-01-06 00:33:19 +01:00
|
|
|
function registerMessageCatalogs(catalogs) {
|
|
|
|
var promises = catalogs.map(function(catalog) {
|
2020-11-30 15:38:48 +01:00
|
|
|
return registerMessageCatalog(catalog.namespace,catalog.dir,catalog.file).catch(err => {});
|
2017-01-06 00:33:19 +01:00
|
|
|
});
|
2020-11-30 15:38:48 +01:00
|
|
|
return Promise.all(promises);
|
2017-01-06 00:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:45:38 +02:00
|
|
|
/**
|
|
|
|
* Register a message catalog with i18n.
|
2018-12-05 14:00:25 +01:00
|
|
|
* @memberof @node-red/util_i18n
|
2018-08-28 14:45:38 +02:00
|
|
|
*/
|
2020-11-30 15:38:48 +01:00
|
|
|
async function registerMessageCatalog(namespace,dir,file) {
|
2018-04-19 12:23:08 +02:00
|
|
|
return initPromise.then(function() {
|
2018-07-30 00:47:19 +02:00
|
|
|
return new Promise((resolve,reject) => {
|
2019-04-25 16:23:08 +02:00
|
|
|
resourceMap[namespace] = { basedir:dir, file:file, lngs: []};
|
|
|
|
fs.readdir(dir,function(err, files) {
|
|
|
|
if (err) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
files.forEach(function(f) {
|
|
|
|
if (fs.existsSync(path.join(dir,f,file))) {
|
|
|
|
resourceMap[namespace].lngs.push(f);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
i18n.loadNamespaces(namespace,function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2015-04-15 21:59:39 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:56:45 +02:00
|
|
|
function mergeCatalog(fallback,catalog) {
|
|
|
|
for (var k in fallback) {
|
|
|
|
if (fallback.hasOwnProperty(k)) {
|
|
|
|
if (!catalog[k]) {
|
|
|
|
catalog[k] = fallback[k];
|
|
|
|
} else if (typeof fallback[k] === 'object') {
|
|
|
|
mergeCatalog(fallback[k],catalog[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 22:07:06 +01:00
|
|
|
|
|
|
|
function readFile(lng, ns) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (resourceCache[ns] && resourceCache[ns][lng]) {
|
|
|
|
resolve(resourceCache[ns][lng]);
|
|
|
|
} else if (resourceMap[ns]) {
|
|
|
|
var file = path.join(resourceMap[ns].basedir, lng, resourceMap[ns].file);
|
|
|
|
fs.readFile(file, "utf8", function (err, content) {
|
2015-04-15 21:59:39 +02:00
|
|
|
if (err) {
|
2019-11-09 22:07:06 +01:00
|
|
|
reject(err);
|
2015-04-15 21:59:39 +02:00
|
|
|
} else {
|
|
|
|
try {
|
2019-11-09 22:07:06 +01:00
|
|
|
resourceCache[ns] = resourceCache[ns] || {};
|
2015-04-26 00:29:53 +02:00
|
|
|
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
|
2019-11-09 22:07:06 +01:00
|
|
|
var baseLng = lng.split('-')[0];
|
|
|
|
if (baseLng !== lng && resourceCache[ns][baseLng]) {
|
|
|
|
mergeCatalog(resourceCache[ns][baseLng], resourceCache[ns][lng]);
|
|
|
|
}
|
2015-09-24 22:56:45 +02:00
|
|
|
if (lng !== defaultLang) {
|
2019-11-09 22:07:06 +01:00
|
|
|
mergeCatalog(resourceCache[ns][defaultLang], resourceCache[ns][lng]);
|
2015-09-24 22:56:45 +02:00
|
|
|
}
|
2019-11-09 22:07:06 +01:00
|
|
|
resolve(resourceCache[ns][lng]);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2019-11-09 22:07:06 +01:00
|
|
|
reject(new Error("Unrecognised namespace"));
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
2019-11-09 22:07:06 +01:00
|
|
|
});
|
|
|
|
}
|
2015-07-19 23:10:46 +02:00
|
|
|
|
2019-11-09 22:07:06 +01:00
|
|
|
var MessageFileLoader = {
|
|
|
|
type: "backend",
|
|
|
|
init: function (services, backendOptions, i18nextOptions) { },
|
|
|
|
read: function (lng, ns, callback) {
|
2019-11-18 20:45:48 +01:00
|
|
|
readFile(lng, ns)
|
|
|
|
.then(data => callback(null, data))
|
2019-11-21 11:42:40 +01:00
|
|
|
.catch(err => {
|
|
|
|
if (/-/.test(lng)) {
|
|
|
|
// if reading language file fails -> try reading base language (e. g. 'fr' instead of 'fr-FR' or 'de' for 'de-DE')
|
|
|
|
var baseLng = lng.split('-')[0];
|
|
|
|
readFile(baseLng, ns).then(baseData => callback(null, baseData)).catch(err => callback(err));
|
|
|
|
} else {
|
|
|
|
callback(err);
|
|
|
|
}
|
2019-11-09 22:07:06 +01:00
|
|
|
});
|
|
|
|
}
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 13:32:56 +02:00
|
|
|
function getCurrentLocale() {
|
2018-05-26 18:05:50 +02:00
|
|
|
var env = process.env;
|
|
|
|
for (var name of ['LC_ALL', 'LC_MESSAGES', 'LANG']) {
|
|
|
|
if (name in env) {
|
|
|
|
var val = env[name];
|
|
|
|
return val.substring(0, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-12-27 14:44:32 +01:00
|
|
|
function init(settings) {
|
2018-04-19 12:23:08 +02:00
|
|
|
if (!initPromise) {
|
2018-07-30 00:47:19 +02:00
|
|
|
// Keep this as a 'when' promise as top-level red.js uses 'otherwise'
|
|
|
|
// and embedded users of NR may have copied that.
|
2020-11-30 15:38:48 +01:00
|
|
|
initPromise = new Promise((resolve,reject) => {
|
2018-07-30 00:47:19 +02:00
|
|
|
i18n.use(MessageFileLoader);
|
2018-06-06 22:59:46 +02:00
|
|
|
var opt = {
|
2018-07-30 00:47:19 +02:00
|
|
|
// debug: true,
|
|
|
|
defaultNS: "runtime",
|
|
|
|
ns: [],
|
|
|
|
fallbackLng: defaultLang,
|
|
|
|
interpolation: {
|
|
|
|
unescapeSuffix: 'HTML',
|
|
|
|
escapeValue: false,
|
|
|
|
prefix: '__',
|
|
|
|
suffix: '__'
|
|
|
|
}
|
2018-06-06 22:59:46 +02:00
|
|
|
};
|
2020-12-27 12:59:12 +01:00
|
|
|
var lang = settings.lang || getCurrentLocale();
|
2018-06-06 22:59:46 +02:00
|
|
|
if (lang) {
|
|
|
|
opt.lng = lang;
|
|
|
|
}
|
2018-07-30 00:47:19 +02:00
|
|
|
i18n.init(opt ,function() {
|
2018-04-19 12:23:08 +02:00
|
|
|
resolve();
|
|
|
|
});
|
2015-04-15 21:59:39 +02:00
|
|
|
});
|
2018-04-19 12:23:08 +02:00
|
|
|
}
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:45:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a message catalog.
|
|
|
|
* @name catalog
|
|
|
|
* @function
|
2018-12-05 14:00:25 +01:00
|
|
|
* @memberof @node-red/util_i18n
|
2018-08-28 14:45:38 +02:00
|
|
|
*/
|
2015-04-26 00:29:53 +02:00
|
|
|
function getCatalog(namespace,lang) {
|
|
|
|
var result = null;
|
2017-04-21 12:49:35 +02:00
|
|
|
lang = lang || defaultLang;
|
2015-04-26 00:29:53 +02:00
|
|
|
if (resourceCache.hasOwnProperty(namespace)) {
|
|
|
|
result = resourceCache[namespace][lang];
|
|
|
|
if (!result) {
|
|
|
|
var langParts = lang.split("-");
|
|
|
|
if (langParts.length == 2) {
|
2015-09-24 22:56:45 +02:00
|
|
|
result = resourceCache[namespace][langParts[0]];
|
|
|
|
}
|
2015-04-26 00:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-25 16:23:08 +02:00
|
|
|
/**
|
|
|
|
* Gets a list of languages a given catalog is available in.
|
|
|
|
* @name availableLanguages
|
|
|
|
* @function
|
|
|
|
* @memberof @node-red/util_i18n
|
|
|
|
*/
|
|
|
|
function availableLanguages(namespace) {
|
|
|
|
if (resourceMap.hasOwnProperty(namespace)) {
|
|
|
|
return resourceMap[namespace].lngs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
var obj = module.exports = {
|
|
|
|
init: init,
|
2015-04-26 00:29:53 +02:00
|
|
|
registerMessageCatalog: registerMessageCatalog,
|
2017-01-06 00:33:19 +01:00
|
|
|
registerMessageCatalogs: registerMessageCatalogs,
|
2015-04-26 00:29:53 +02:00
|
|
|
catalog: getCatalog,
|
2019-04-25 16:23:08 +02:00
|
|
|
availableLanguages: availableLanguages,
|
2018-08-28 14:45:38 +02:00
|
|
|
/**
|
|
|
|
* The underlying i18n library for when direct access is really needed
|
|
|
|
*/
|
2015-05-28 21:17:31 +02:00
|
|
|
i: i18n,
|
2018-08-28 14:45:38 +02:00
|
|
|
/**
|
|
|
|
* The default language of the runtime
|
|
|
|
*/
|
2016-11-14 20:10:02 +01:00
|
|
|
defaultLang: defaultLang
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:45:38 +02:00
|
|
|
/**
|
|
|
|
* Perform a message catalog lookup.
|
|
|
|
* @name _
|
|
|
|
* @function
|
2018-12-05 14:00:25 +01:00
|
|
|
* @memberof @node-red/util_i18n
|
2018-08-28 14:45:38 +02:00
|
|
|
*/
|
2015-04-15 21:59:39 +02:00
|
|
|
obj['_'] = function() {
|
|
|
|
//var opts = {};
|
|
|
|
//if (def) {
|
|
|
|
// opts.defaultValue = def;
|
|
|
|
//}
|
2015-04-26 00:29:53 +02:00
|
|
|
//console.log(arguments);
|
2018-07-24 00:25:57 +02:00
|
|
|
var res = i18n.t.apply(i18n,arguments);
|
|
|
|
return res;
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|