fix i18n handling for ja-JP locale on Safari/MacOS

This commit is contained in:
Hiroyasu Nishiyama 2018-10-05 21:16:57 +09:00
parent 22ede79799
commit 14435c24ac
2 changed files with 37 additions and 1 deletions

View File

@ -21,6 +21,18 @@ var i18n = require("@node-red/util").i18n; // TODO: separate module
var runtimeAPI;
function loadResource(lang, namespace) {
var catalog = i18n.i.getResourceBundle(lang, namespace);
if (!catalog) {
var parts = lang.split("-");
if (parts.length == 2) {
var new_lang = parts[0];
return i18n.i.getResourceBundle(new_lang, namespace);
}
}
return catalog;
}
module.exports = {
init: function(_runtimeAPI) {
runtimeAPI = _runtimeAPI;
@ -33,7 +45,7 @@ module.exports = {
var prevLang = i18n.i.language;
// Trigger a load from disk of the language if it is not the default
i18n.i.changeLanguage(lang, function(){
var catalog = i18n.i.getResourceBundle(lang, namespace);
var catalog = loadResource(lang, namespace);
res.json(catalog||{});
});
i18n.i.changeLanguage(prevLang);

View File

@ -91,6 +91,30 @@ describe("api/editor/locales", function() {
done();
});
});
it('returns for locale defined only with primary tag ', function(done) {
var orig = i18n.i.getResourceBundle;
i18n.i.getResourceBundle = function (lang, ns) {
if (lang === "ja-JP") {
return undefined;
}
return orig(lang, ns);
};
request(app)
// returns `ja` instead of `ja-JP`
.get("/locales/message-catalog?lng=ja-JP")
.expect(200)
.end(function(err,res) {
i18n.i.getResourceBundle = orig;
if (err) {
return done(err);
}
res.body.should.have.property('namespace','message-catalog');
res.body.should.have.property('lang','ja');
done();
});
});
});
// describe('get all node resource catalogs',function() {