4

I'm trying to understand existing code of the Switch node in node-red to deal with and make my own node correctly.

I'm stuck with these lines :

var operators = [
                {v:"eq",t:"=="},
                {v:"neq",t:"!="},
                {v:"lt",t:"<"},
                {v:"lte",t:"<="},
                {v:"gt",t:">"},
                {v:"gte",t:">="},
                {v:"btwn",t:this._("switch.rules.btwn")},
                {v:"cont",t:this._("switch.rules.cont")},
                {v:"regex",t:this._("switch.rules.regex")},
                {v:"true",t:this._("switch.rules.true")},
                {v:"false",t:this._("switch.rules.false")},
                {v:"null",t:this._("switch.rules.null")},
                {v:"nnull",t:this._("switch.rules.nnull")},
                {v:"else",t:this._("switch.rules.else")}
            ];

Especially with the this._("switch.rules.smthg"). How it will work ? Sometime in the code, i will see this call, but i'm not able to find where is it stored, and so make my own, like this._(myawesomenode.myawesomesection.myawesomepropertie)

EDIT

Thanks to your comments, i've seen it's for internationalisation.

Suppposing i have this catalog :

{
    "and": {
        "list": {
            "key": "THE DATA I WANT"
        }    
    }
}

How can i have my data ? I've tried something like this._(and.list.key) without result.

N Frb
  • 107
  • 1
  • 9
  • This really should have been a separate question, but have you followed the instructions at the start of the documentation? Your node needs to have a package.json file with a correct node-red entry – hardillb Oct 19 '16 at 12:29
  • yes, for a "mynode.js" file, i have the "mynode.json" in my locales/en-US directory – N Frb Oct 19 '16 at 12:36
  • Ask a new question, include a full directory listing of your node, and a sample of the code you are trying (with enough context to see what it's actually doing) and a proper description of the error you are getting. – hardillb Oct 19 '16 at 12:41

2 Answers2

3

This is the function to pull in the translated version of a label.

"switch.rules.btwn" is the key to look up the version of the label in the right language for the user.

See the Internationalisation section of the Node-RED documentation for more details.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • For now, 've tried to implement it, by creating locales/en-US/catalog.json next to the service.js, who describe my node. I've edited my post, and so, to access it, how can i do ? – N Frb Oct 19 '16 at 11:57
0

I don't know node-red, but this looks like there is some method defined on the this object with the name/identifier _, which is called with a string parameter.

You can do this like this._ = function (arg) {...}

You might find the definition in the constructor for the this object (whatever that is in that context)

Matthias247
  • 9,836
  • 1
  • 20
  • 29