mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #2376 from tt92/master
I18n - language fallback behaviour
This commit is contained in:
commit
fc93e502b8
52
packages/node_modules/@node-red/util/lib/i18n.js
vendored
52
packages/node_modules/@node-red/util/lib/i18n.js
vendored
@ -81,35 +81,51 @@ function mergeCatalog(fallback,catalog) {
|
||||
}
|
||||
}
|
||||
|
||||
var MessageFileLoader = {
|
||||
type: "backend",
|
||||
init: function(services, backendOptions, i18nextOptions) {},
|
||||
read: function(lng, ns, callback) {
|
||||
if (resourceMap[ns]) {
|
||||
var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file);
|
||||
//console.log(file);
|
||||
fs.readFile(file,"utf8",function(err,content) {
|
||||
|
||||
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) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
reject(err);
|
||||
} else {
|
||||
try {
|
||||
resourceCache[ns] = resourceCache[ns]||{};
|
||||
resourceCache[ns] = resourceCache[ns] || {};
|
||||
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
|
||||
//console.log(resourceCache[ns][lng]);
|
||||
if (lng !== defaultLang) {
|
||||
mergeCatalog(resourceCache[ns][defaultLang],resourceCache[ns][lng]);
|
||||
var baseLng = lng.split('-')[0];
|
||||
if (baseLng !== lng && resourceCache[ns][baseLng]) {
|
||||
mergeCatalog(resourceCache[ns][baseLng], resourceCache[ns][lng]);
|
||||
}
|
||||
callback(null, resourceCache[ns][lng]);
|
||||
} catch(e) {
|
||||
callback(e);
|
||||
if (lng !== defaultLang) {
|
||||
mergeCatalog(resourceCache[ns][defaultLang], resourceCache[ns][lng]);
|
||||
}
|
||||
resolve(resourceCache[ns][lng]);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback(new Error("Unrecognised namespace"));
|
||||
}
|
||||
reject(new Error("Unrecognised namespace"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var MessageFileLoader = {
|
||||
type: "backend",
|
||||
init: function (services, backendOptions, i18nextOptions) { },
|
||||
read: function (lng, ns, callback) {
|
||||
readFile(lng, ns)
|
||||
.then(data => callback(null, data))
|
||||
.catch(() => {
|
||||
// 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));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getCurrentLocale() {
|
||||
|
Loading…
Reference in New Issue
Block a user