mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
fixed i18n language fallback behaviour
This commit is contained in:
parent
9bd9c6a400
commit
dae1d6057e
59
packages/node_modules/@node-red/util/lib/i18n.js
vendored
59
packages/node_modules/@node-red/util/lib/i18n.js
vendored
@ -81,35 +81,58 @@ function mergeCatalog(fallback,catalog) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var MessageFileLoader = {
|
|
||||||
type: "backend",
|
function readFile(lng, ns) {
|
||||||
init: function(services, backendOptions, i18nextOptions) {},
|
return new Promise((resolve, reject) => {
|
||||||
read: function(lng, ns, callback) {
|
if (resourceCache[ns] && resourceCache[ns][lng]) {
|
||||||
if (resourceMap[ns]) {
|
resolve(resourceCache[ns][lng]);
|
||||||
var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file);
|
} else if (resourceMap[ns]) {
|
||||||
//console.log(file);
|
var file = path.join(resourceMap[ns].basedir, lng, resourceMap[ns].file);
|
||||||
fs.readFile(file,"utf8",function(err,content) {
|
fs.readFile(file, "utf8", function (err, content) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
resourceCache[ns] = resourceCache[ns]||{};
|
resourceCache[ns] = resourceCache[ns] || {};
|
||||||
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
|
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
|
||||||
//console.log(resourceCache[ns][lng]);
|
var baseLng = lng.split('-')[0];
|
||||||
if (lng !== defaultLang) {
|
if (baseLng !== lng && resourceCache[ns][baseLng]) {
|
||||||
mergeCatalog(resourceCache[ns][defaultLang],resourceCache[ns][lng]);
|
mergeCatalog(resourceCache[ns][baseLng], resourceCache[ns][lng]);
|
||||||
}
|
}
|
||||||
callback(null, resourceCache[ns][lng]);
|
if (lng !== defaultLang) {
|
||||||
} catch(e) {
|
mergeCatalog(resourceCache[ns][defaultLang], resourceCache[ns][lng]);
|
||||||
callback(e);
|
}
|
||||||
|
resolve(resourceCache[ns][lng]);
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} 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) {
|
||||||
|
// there are two languages to consider:
|
||||||
|
// 1) the actual language lng (e. g. 'en-US' or 'de-DE', 'fr-FR',...)
|
||||||
|
// 2) the base language (e. g. 'en', 'de', 'fr',...)
|
||||||
|
var baseLng = lng.split('-')[0];
|
||||||
|
// read base language (e. g. 'de'), then actual language (e. g. 'de-DE')
|
||||||
|
readFile(baseLng, ns).then(
|
||||||
|
baseData => {
|
||||||
|
// scenario 2: reading base language was successful -> read actual language -> if this fails, return base language data
|
||||||
|
readFile(lng, ns).then(data => callback(null, data), () => callback(null, baseData))
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
// scenario 2: reading base language failed -> read actual language -> if this fails too, return error
|
||||||
|
readFile(lng, ns).then(data => callback(null, data), err => callback(err))
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentLocale() {
|
function getCurrentLocale() {
|
||||||
|
Loading…
Reference in New Issue
Block a user