Add i18n function to editor plugins when they are registered

Adds a `_` function to the plugin definition object that will automatically
prepend the plugin's module namespace to any call. This saves the plugin
from having to prepend its namespace all of the time.
This commit is contained in:
Nick O'Leary 2021-01-20 15:35:44 +00:00
parent 9f71dbb006
commit 9e179170ee
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 21 additions and 1 deletions

View File

@ -8,6 +8,23 @@ RED.plugins = (function() {
pluginsByType[definition.type] = pluginsByType[definition.type] || [];
pluginsByType[definition.type].push(definition);
}
if (RED._loadingModule) {
definition.module = RED._loadingModule;
definition["_"] = function() {
var args = Array.prototype.slice.call(arguments);
var originalKey = args[0];
if (!/:/.test(args[0])) {
args[0] = definition.module+":"+args[0];
}
var result = RED._.apply(null,args);
if (result === args[0]) {
return originalKey;
}
return result;
}
} else {
definition["_"] = RED["_"]
}
if (definition.onadd && typeof definition.onadd === 'function') {
definition.onadd();
}

View File

@ -67,11 +67,11 @@ var RED = (function() {
var moduleId;
if (moduleIdMatch) {
moduleId = moduleIdMatch[1];
RED._loadingModule = moduleId;
} else {
moduleId = "unknown";
}
try {
var hasDeferred = false;
var nodeConfigEls = $("<div>"+config+"</div>");
var scripts = nodeConfigEls.find("script");
@ -85,6 +85,7 @@ var RED = (function() {
scriptCount--;
if (scriptCount === 0) {
$(targetContainer).append(nodeConfigEls);
delete RED._loadingModule;
done()
}
}
@ -108,6 +109,7 @@ var RED = (function() {
})
if (!hasDeferred) {
$(targetContainer).append(nodeConfigEls);
delete RED._loadingModule;
done();
}
} catch(err) {
@ -116,6 +118,7 @@ var RED = (function() {
timeout: 10000
});
console.log("["+moduleId+"] "+err.toString());
delete RED._loadingModule;
done();
}
}