1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/runtime/i18n.js

156 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2015-04-15 21:59:39 +02: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.
**/
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 when = require("when");
var path = require("path");
var fs = require("fs");
2015-04-26 00:29:53 +02:00
var defaultLang = "en-US";
var resourceMap = {};
var resourceCache = {};
2015-04-15 21:59:39 +02:00
2017-01-06 00:33:19 +01:00
function registerMessageCatalogs(catalogs) {
var promises = catalogs.map(function(catalog) {
return registerMessageCatalog(catalog.namespace,catalog.dir,catalog.file);
});
return when.settle(promises);
}
2015-04-26 00:29:53 +02:00
function registerMessageCatalog(namespace,dir,file) {
2015-04-15 21:59:39 +02:00
return when.promise(function(resolve,reject) {
2015-04-26 00:29:53 +02:00
resourceMap[namespace] = { basedir:dir, file:file};
2018-07-24 00:25:57 +02:00
i18n.loadNamespaces(namespace,function() {
2015-04-15 21:59:39 +02:00
resolve();
});
});
}
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]);
}
}
}
}
2015-04-15 21:59:39 +02:00
var MessageFileLoader = {
2018-07-24 00:25:57 +02:00
type: "backend",
init: function(services, backendOptions, i18nextOptions) {},
read: function(lng, ns, callback) {
2015-04-15 21:59:39 +02:00
if (resourceMap[ns]) {
2015-04-26 00:29:53 +02:00
var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file);
2015-05-06 23:14:00 +02:00
//console.log(file);
2015-04-15 21:59:39 +02:00
fs.readFile(file,"utf8",function(err,content) {
if (err) {
callback(err);
} else {
try {
2015-04-26 00:29:53 +02:00
resourceCache[ns] = resourceCache[ns]||{};
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
2015-05-06 23:14:00 +02:00
//console.log(resourceCache[ns][lng]);
if (lng !== defaultLang) {
mergeCatalog(resourceCache[ns][defaultLang],resourceCache[ns][lng]);
}
2015-04-26 00:29:53 +02:00
callback(null, resourceCache[ns][lng]);
2015-04-15 21:59:39 +02:00
} catch(e) {
callback(e);
}
}
});
} else {
callback(new Error("Unrecognised namespace"));
}
}
2015-04-15 21:59:39 +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;
}
2015-04-15 21:59:39 +02:00
function init() {
return when.promise(function(resolve,reject) {
2018-07-24 00:25:57 +02:00
i18n.use(MessageFileLoader);
2018-05-26 18:05:50 +02:00
var opt = {
2018-07-24 00:25:57 +02:00
// debug: true,
defaultNS: "runtime",
ns: [],
fallbackLng: defaultLang,
interpolation: {
unescapeSuffix: 'HTML',
escapeValue: false,
prefix: '__',
suffix: '__'
}
2018-05-26 18:05:50 +02:00
};
var lang = getCurrentLocale();
2018-05-26 18:05:50 +02:00
if (lang) {
opt.lng = lang;
}
i18n.init(opt ,function() {
resolve();
2015-04-15 21:59:39 +02:00
});
});
}
2015-04-26 00:29:53 +02:00
function getCatalog(namespace,lang) {
var result = null;
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) {
result = resourceCache[namespace][langParts[0]];
}
2015-04-26 00:29:53 +02:00
}
}
return result;
}
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,
2015-05-28 21:17:31 +02:00
i: i18n,
defaultLang: defaultLang
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
}