From 9e179170ee082da3d3bb30085916e0dfebbaae78 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 20 Jan 2021 15:35:44 +0000 Subject: [PATCH] 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. --- .../@node-red/editor-client/src/js/plugins.js | 17 +++++++++++++++++ .../@node-red/editor-client/src/js/red.js | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/plugins.js b/packages/node_modules/@node-red/editor-client/src/js/plugins.js index 25d6acf8a..e6f41517d 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/plugins.js +++ b/packages/node_modules/@node-red/editor-client/src/js/plugins.js @@ -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(); } diff --git a/packages/node_modules/@node-red/editor-client/src/js/red.js b/packages/node_modules/@node-red/editor-client/src/js/red.js index 229147b10..a2bcb0e7d 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/red.js +++ b/packages/node_modules/@node-red/editor-client/src/js/red.js @@ -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 = $("
"+config+"
"); 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(); } }