-
Notifications
You must be signed in to change notification settings - Fork 0
Design: i18n
We are using the i18next library for NLS support in both the runtime and editor.
| Component | Namespace | Location |
|---|---|---|
| Runtime | runtime |
node-red/locales/__lang__/runtime.json |
| Editor | editor |
node-red/locales/__lang__/editor.json |
| Core nodes | node-red |
node-red/nodes/core/locales/__lang__/messages.json |
A node module provides message catalogs on a per-node-set basis. Given a node-set identified in package.json as:
"node-red": {
"myNode": "lib/my-node.js"
}
The following message catalogs may exist:
lib/locales/__lang__/my-node.json
lib/locales/__lang__/my-node.html
NB: the locales directory is relative to the node's .js file.
The .json file is the message catalog for both runtime and editor.
The .html file contains the localized node help content. The help content in the top-level .html file may identify what language it provides using a data-lang attribute: (en-US assumed if not set).
<script type="text/x-red" data-help-name="foo" data-lang="en-US">
<p>A foo node</p>
</script>
The catalog for a node-set is added under the namespace <module>/<set>.
All of this is subject to change - need to figure out how best for the editor to express what language it wants the catalogs in
A node's localized help will automatically get included with the existing methods to retrieve the node's editor configuration.
For message catalogs, a new api end-point is added:
/locales/__namespace__
For example:
/locales/node-red
will load the catalog for all core nodes.
/locales/node-red-contrib-twilio/twilio
will load the catalog for the node-red-contrib-twilio/twilio node set.
var i18n = require("./path/to/i18n.js");
// i18n._ is provided as a wrapper to i18next.t
// Retrieve the value of the message `runtime.version`
console.log(i18n._("runtime.version"));
// Retrieve the value of the message `example.insert" with a named insert
// "example.insert" : "I have a value of __myValue__"
console.log(i18n._("example.insert",{myValue: 123});
Nodes can use RED._() to retrieve messages. The function they are provided is pre-scoped to the node's own namespace so they don't have to worry about providing it with each message.
RED._() is provided for core editor code.
Any html element can set a data-i18n attribute, as documented here. NB: $(element).i18n() must be called after element is added to the DOM in order for these attributes to be parsed.
As well as the data-i18n attribute for html elements, all node definition functions (oneditprepare etc) can use this._() to retrieve messages, pre-scoped to the node's namespace.