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

Provide single endpoint to load all node message catalogs

Replaces potentially dozens of http requests with one or two.
This commit is contained in:
Nick O'Leary 2017-04-21 11:49:35 +01:00
parent bb2649d063
commit 7bd0943412
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 46 additions and 22 deletions

View File

@ -19,7 +19,7 @@ RED.i18n = (function() {
return { return {
init: function(done) { init: function(done) {
i18n.init({ i18n.init({
resGetPath: 'locales/__ns__', resGetPath: 'locales/__ns__?lng=__lng__',
dynamicLoad: false, dynamicLoad: false,
load:'current', load:'current',
ns: { ns: {
@ -38,6 +38,31 @@ RED.i18n = (function() {
}, },
loadCatalog: function(namespace,done) { loadCatalog: function(namespace,done) {
i18n.loadNamespace(namespace,done); i18n.loadNamespace(namespace,done);
},
loadNodeCatalogs: function(done) {
var languageList = i18n.functions.toLanguages(i18n.detectLanguage());
var toLoad = languageList.length;
languageList.forEach(function(lang) {
$.ajax({
headers: {
"Accept":"application/json"
},
cache: false,
url: 'locales/nodes?lng='+lang,
success: function(data) {
var namespaces = Object.keys(data);
namespaces.forEach(function(ns) {
i18n.addResourceBundle(lang,ns,data[ns]);
});
toLoad--;
if (toLoad === 0) {
done();
}
}
});
})
} }
} }
})(); })();

View File

@ -24,22 +24,7 @@
url: 'nodes', url: 'nodes',
success: function(data) { success: function(data) {
RED.nodes.setNodeList(data); RED.nodes.setNodeList(data);
var nsCount = 0; RED.i18n.loadNodeCatalogs(loadNodes);
for (var i=0;i<data.length;i++) {
var ns = data[i];
if (ns.module != "node-red") {
nsCount++;
RED.i18n.loadCatalog(ns.id, function() {
nsCount--;
if (nsCount === 0) {
loadNodes();
}
});
}
}
if (nsCount === 0) {
loadNodes();
}
} }
}); });
} }

View File

@ -147,6 +147,7 @@ function init(_server,_runtime) {
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,errorHandler); adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,errorHandler);
adminApp.get('/locales/nodes',locales.getAllNodes,errorHandler);
adminApp.get(/locales\/(.+)\/?$/,locales.get,errorHandler); adminApp.get(/locales\/(.+)\/?$/,locales.get,errorHandler);
// Library // Library

View File

@ -16,6 +16,7 @@
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var i18n; var i18n;
var redNodes;
function determineLangFromHeaders(acceptedLanguages){ function determineLangFromHeaders(acceptedLanguages){
var lang = i18n.defaultLang; var lang = i18n.defaultLang;
@ -25,21 +26,35 @@ function determineLangFromHeaders(acceptedLanguages){
} }
return lang; return lang;
} }
module.exports = { module.exports = {
init: function(runtime) { init: function(runtime) {
i18n = runtime.i18n; i18n = runtime.i18n;
redNodes = runtime.nodes;
}, },
get: function(req,res) { get: function(req,res) {
var namespace = req.params[0]; var namespace = req.params[0];
var lngs = req.query.lng;
namespace = namespace.replace(/\.json$/,""); namespace = namespace.replace(/\.json$/,"");
var lang = determineLangFromHeaders(req.acceptsLanguages() || []); var lang = req.query.lng; //determineLangFromHeaders(req.acceptsLanguages() || []);
var prevLang = i18n.i.lng(); var prevLang = i18n.i.lng();
// Trigger a load from disk of the language if it is not the default
i18n.i.setLng(lang, function(){ i18n.i.setLng(lang, function(){
var catalog = i18n.catalog(namespace,lang); var catalog = i18n.catalog(namespace,lang);
res.json(catalog||{}); res.json(catalog||{});
}); });
i18n.i.setLng(prevLang); i18n.i.setLng(prevLang);
},
getAllNodes: function(req,res) {
var lngs = req.query.lng;
var nodeList = redNodes.getNodeList();
var result = {};
nodeList.forEach(function(n) {
if (n.module !== "node-red") {
result[n.id] = i18n.catalog(n.id,lngs)||{};
}
});
res.json(result);
}, },
determineLangFromHeaders: determineLangFromHeaders determineLangFromHeaders: determineLangFromHeaders
} }

View File

@ -98,6 +98,7 @@ function init() {
function getCatalog(namespace,lang) { function getCatalog(namespace,lang) {
var result = null; var result = null;
lang = lang || defaultLang;
if (resourceCache.hasOwnProperty(namespace)) { if (resourceCache.hasOwnProperty(namespace)) {
result = resourceCache[namespace][lang]; result = resourceCache[namespace][lang];
if (!result) { if (!result) {
@ -105,9 +106,6 @@ function getCatalog(namespace,lang) {
if (langParts.length == 2) { if (langParts.length == 2) {
result = resourceCache[namespace][langParts[0]]; result = resourceCache[namespace][langParts[0]];
} }
if (!result) {
return resourceCache[namespace][defaultLang];
}
} }
} }
return result; return result;