Update i18next in runtime

This commit is contained in:
Nick O'Leary 2018-07-23 23:25:57 +01:00
parent c36870c23e
commit db77be5d72
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 38 additions and 27 deletions

View File

@ -49,7 +49,7 @@
"fs-extra": "5.0.0",
"fs.notify": "0.0.4",
"hash-sum": "1.0.2",
"i18next": "1.10.6",
"i18next": "^11.4.0",
"is-utf8": "0.2.1",
"js-yaml": "3.12.0",
"json-stringify-safe": "5.0.1",

View File

@ -29,13 +29,13 @@ module.exports = {
var lngs = req.query.lng;
namespace = namespace.replace(/\.json$/,"");
var lang = req.query.lng; //apiUtil.determineLangFromHeaders(req.acceptsLanguages() || []);
var prevLang = i18n.i.lng();
var prevLang = i18n.i.language;
// Trigger a load from disk of the language if it is not the default
i18n.i.setLng(lang, function(){
var catalog = i18n.catalog(namespace,lang);
i18n.i.changeLanguage(lang, function(){
var catalog = i18n.i.getResourceBundle(lang, namespace);
res.json(catalog||{});
});
i18n.i.setLng(prevLang);
i18n.i.changeLanguage(prevLang);
},
getAllNodes: function(req,res) {
@ -44,7 +44,7 @@ module.exports = {
var result = {};
nodeList.forEach(function(n) {
if (n.module !== "node-red") {
result[n.id] = i18n.catalog(n.id,lngs)||{};
result[n.id] = i18n.i.getResourceBundle(lngs, n.id)||{};
}
});
res.json(result);

View File

@ -15,6 +15,7 @@
**/
var i18n = require("i18next");
var when = require("when");
var path = require("path");
var fs = require("fs");
@ -34,7 +35,7 @@ function registerMessageCatalogs(catalogs) {
function registerMessageCatalog(namespace,dir,file) {
return when.promise(function(resolve,reject) {
resourceMap[namespace] = { basedir:dir, file:file};
i18n.loadNamespace(namespace,function() {
i18n.loadNamespaces(namespace,function() {
resolve();
});
});
@ -53,7 +54,9 @@ function mergeCatalog(fallback,catalog) {
}
var MessageFileLoader = {
fetchOne: function(lng, ns, callback) {
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);
@ -94,13 +97,18 @@ function getCurrentLocale() {
function init() {
return when.promise(function(resolve,reject) {
i18n.backend(MessageFileLoader);
i18n.use(MessageFileLoader);
var opt = {
ns: {
namespaces: [],
defaultNs: "runtime"
},
fallbackLng: [defaultLang]
// debug: true,
defaultNS: "runtime",
ns: [],
fallbackLng: defaultLang,
interpolation: {
unescapeSuffix: 'HTML',
escapeValue: false,
prefix: '__',
suffix: '__'
}
};
var lang = getCurrentLocale();
if (lang) {
@ -142,5 +150,6 @@ obj['_'] = function() {
// opts.defaultValue = def;
//}
//console.log(arguments);
return i18n.t.apply(null,arguments);
var res = i18n.t.apply(i18n,arguments);
return res;
}

View File

@ -33,16 +33,16 @@ describe("api/editor/locales", function() {
locales.init({
i18n: {
i: {
lng: function() { return 'en-US'},
setLng: function(lang,callback) {
language: function() { return 'en-US'},
changeLanguage: function(lang,callback) {
if (callback) {
callback();
}
},
getResourceBundle: function(lang, namespace) {
return {namespace:namespace, lang:lang};
}
},
catalog: function(namespace, lang) {
return {namespace:namespace, lang:lang};
}
}
});
app = express();
@ -81,13 +81,15 @@ describe("api/editor/locales", function() {
// bit of a mess of internal workings
locales.init({
i18n: {
catalog: function(namespace, lang) {
return {
"node-red": "should not return",
"test-module-a-id": "test-module-a-catalog",
"test-module-b-id": "test-module-b-catalog",
"test-module-c-id": "test-module-c-catalog"
}[namespace]
i:{
getResourceBundle: function(lang, namespace) {
return {
"node-red": "should not return",
"test-module-a-id": "test-module-a-catalog",
"test-module-b-id": "test-module-b-catalog",
"test-module-c-id": "test-module-c-catalog"
}[namespace]
}
}
},
nodes: {