国际化

If a node is packaged as a proper module, it can include a message catalog in order to provide translated content in the editor and runtime.

对于模块package.json中标识的每个节点,可以在节点的.js文件旁包含相应的消息目录和帮助文件。

给定一个标识为:

"name": "my-node-module",
"node-red": {
    "myNode": "myNode/my-node.js"
}

可能存在以下消息目录:

myNode/locales/__language__/my-node.json
myNode/locales/__language__/my-node.html

locales目录必须与节点的.js文件位于同一目录下。

路径中的__language__部分标识了对应文件提供的语言。默认情况下,Node-RED使用en-US

消息目录

消息目录是一个JSON文件,包含节点在编辑器或运行时日志中可能显示的任何文本片段。

例如:

{
    "myNode" : {
        "message1": "This is my first message",
        "message2": "This is my second message"
    }
}

该目录加载在节点特定的命名空间下。对于上面定义的节点,这个目录将在my-node-module/myNode命名空间下可用。

核心节点使用node-red命名空间。

帮助文本

The help file provides translated versions of the node’s help text that gets displayed within the Info sidebar tab of the editor.

使用国际化(i18n)消息

在运行时和编辑器中,系统为节点提供了从目录中查找消息的功能。这些功能已预先限定在节点自身的命名空间内,因此无需在消息标识符中包含命名空间。

运行时

节点的运行时部分可以使用RED._()函数访问消息。例如:

console.log(RED._("myNode.message1"));

状态消息

如果一个节点向编辑器发送状态消息,它应将状态的text设置为消息标识符。

this.status({fill:"green",shape:"dot",text:"myNode.status.ready"});

在核心node-red目录中有许多常用的状态消息。 可以通过在标识的消息中包含命名空间来使用这些消息:

this.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});

编辑器

Any HTML element provided in the node template can specify a data-i18n attribute to provide the message identify to use. For example:

<span data-i18n="myNode.label.foo"></span>

默认情况下,元素中的文本内容会被标识的消息替换。 也可以设置元素的属性,例如placeholder

<input type="text" data-i18n="[placeholder]myNode.placeholder.foo">

可以组合这些来指定多个替换项。例如,同时设置标题属性和显示的文本:

<a href="#" data-i18n="[title]myNode.label.linkTitle;myNode.label.linkText"></a>

As well as the data-i18n attribute for html elements, all node definition functions (for example, oneditprepare) can use this._() to retrieve messages.