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

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-extra": "5.0.0",
"fs.notify": "0.0.4", "fs.notify": "0.0.4",
"hash-sum": "1.0.2", "hash-sum": "1.0.2",
"i18next": "1.10.6", "i18next": "^11.4.0",
"is-utf8": "0.2.1", "is-utf8": "0.2.1",
"js-yaml": "3.12.0", "js-yaml": "3.12.0",
"json-stringify-safe": "5.0.1", "json-stringify-safe": "5.0.1",

View File

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

View File

@ -15,6 +15,7 @@
**/ **/
var i18n = require("i18next"); var i18n = require("i18next");
var when = require("when"); var when = require("when");
var path = require("path"); var path = require("path");
var fs = require("fs"); var fs = require("fs");
@ -34,7 +35,7 @@ function registerMessageCatalogs(catalogs) {
function registerMessageCatalog(namespace,dir,file) { function registerMessageCatalog(namespace,dir,file) {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
resourceMap[namespace] = { basedir:dir, file:file}; resourceMap[namespace] = { basedir:dir, file:file};
i18n.loadNamespace(namespace,function() { i18n.loadNamespaces(namespace,function() {
resolve(); resolve();
}); });
}); });
@ -53,7 +54,9 @@ function mergeCatalog(fallback,catalog) {
} }
var MessageFileLoader = { var MessageFileLoader = {
fetchOne: function(lng, ns, callback) { type: "backend",
init: function(services, backendOptions, i18nextOptions) {},
read: function(lng, ns, callback) {
if (resourceMap[ns]) { if (resourceMap[ns]) {
var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file); var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file);
//console.log(file); //console.log(file);
@ -94,13 +97,18 @@ function getCurrentLocale() {
function init() { function init() {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
i18n.backend(MessageFileLoader); i18n.use(MessageFileLoader);
var opt = { var opt = {
ns: { // debug: true,
namespaces: [], defaultNS: "runtime",
defaultNs: "runtime" ns: [],
}, fallbackLng: defaultLang,
fallbackLng: [defaultLang] interpolation: {
unescapeSuffix: 'HTML',
escapeValue: false,
prefix: '__',
suffix: '__'
}
}; };
var lang = getCurrentLocale(); var lang = getCurrentLocale();
if (lang) { if (lang) {
@ -142,5 +150,6 @@ obj['_'] = function() {
// opts.defaultValue = def; // opts.defaultValue = def;
//} //}
//console.log(arguments); //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({ locales.init({
i18n: { i18n: {
i: { i: {
lng: function() { return 'en-US'}, language: function() { return 'en-US'},
setLng: function(lang,callback) { changeLanguage: function(lang,callback) {
if (callback) { if (callback) {
callback(); callback();
} }
}
}, },
catalog: function(namespace, lang) { getResourceBundle: function(lang, namespace) {
return {namespace:namespace, lang:lang}; return {namespace:namespace, lang:lang};
} }
},
} }
}); });
app = express(); app = express();
@ -81,7 +81,8 @@ describe("api/editor/locales", function() {
// bit of a mess of internal workings // bit of a mess of internal workings
locales.init({ locales.init({
i18n: { i18n: {
catalog: function(namespace, lang) { i:{
getResourceBundle: function(lang, namespace) {
return { return {
"node-red": "should not return", "node-red": "should not return",
"test-module-a-id": "test-module-a-catalog", "test-module-a-id": "test-module-a-catalog",
@ -89,6 +90,7 @@ describe("api/editor/locales", function() {
"test-module-c-id": "test-module-c-catalog" "test-module-c-id": "test-module-c-catalog"
}[namespace] }[namespace]
} }
}
}, },
nodes: { nodes: {
getNodeList: function() { getNodeList: function() {