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:
parent
bb2649d063
commit
7bd0943412
@ -19,7 +19,7 @@ RED.i18n = (function() {
|
||||
return {
|
||||
init: function(done) {
|
||||
i18n.init({
|
||||
resGetPath: 'locales/__ns__',
|
||||
resGetPath: 'locales/__ns__?lng=__lng__',
|
||||
dynamicLoad: false,
|
||||
load:'current',
|
||||
ns: {
|
||||
@ -38,6 +38,31 @@ RED.i18n = (function() {
|
||||
},
|
||||
loadCatalog: function(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();
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
@ -24,22 +24,7 @@
|
||||
url: 'nodes',
|
||||
success: function(data) {
|
||||
RED.nodes.setNodeList(data);
|
||||
var nsCount = 0;
|
||||
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();
|
||||
}
|
||||
RED.i18n.loadNodeCatalogs(loadNodes);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -147,6 +147,7 @@ function init(_server,_runtime) {
|
||||
|
||||
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,errorHandler);
|
||||
|
||||
adminApp.get('/locales/nodes',locales.getAllNodes,errorHandler);
|
||||
adminApp.get(/locales\/(.+)\/?$/,locales.get,errorHandler);
|
||||
|
||||
// Library
|
||||
|
@ -16,6 +16,7 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var i18n;
|
||||
var redNodes;
|
||||
|
||||
function determineLangFromHeaders(acceptedLanguages){
|
||||
var lang = i18n.defaultLang;
|
||||
@ -25,21 +26,35 @@ function determineLangFromHeaders(acceptedLanguages){
|
||||
}
|
||||
return lang;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(runtime) {
|
||||
i18n = runtime.i18n;
|
||||
redNodes = runtime.nodes;
|
||||
},
|
||||
get: function(req,res) {
|
||||
var namespace = req.params[0];
|
||||
var lngs = req.query.lng;
|
||||
namespace = namespace.replace(/\.json$/,"");
|
||||
var lang = determineLangFromHeaders(req.acceptsLanguages() || []);
|
||||
var lang = req.query.lng; //determineLangFromHeaders(req.acceptsLanguages() || []);
|
||||
var prevLang = i18n.i.lng();
|
||||
// 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);
|
||||
res.json(catalog||{});
|
||||
});
|
||||
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
|
||||
}
|
||||
|
@ -98,6 +98,7 @@ function init() {
|
||||
|
||||
function getCatalog(namespace,lang) {
|
||||
var result = null;
|
||||
lang = lang || defaultLang;
|
||||
if (resourceCache.hasOwnProperty(namespace)) {
|
||||
result = resourceCache[namespace][lang];
|
||||
if (!result) {
|
||||
@ -105,9 +106,6 @@ function getCatalog(namespace,lang) {
|
||||
if (langParts.length == 2) {
|
||||
result = resourceCache[namespace][langParts[0]];
|
||||
}
|
||||
if (!result) {
|
||||
return resourceCache[namespace][defaultLang];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
Loading…
Reference in New Issue
Block a user