-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.js
31 lines (28 loc) · 1.04 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module.exports = function (RED) {
"use strict";
var translate = require('@vitalets/google-translate-api');
function GoogleTranslateNode(config) {
RED.nodes.createNode(this, config);
var node = this;
this.on('input', function (msg) {
msg.config = conf;
var conf = { to: config.to };
var phrase = msg.payload + '';
if (config.from === 'prog' && config.to === 'prog') {
conf = { from: msg.payload.from, to: msg.payload.to }
} else if (config.from === 'auto' && config.to === 'prog') {
conf = { to: msg.payload.to }
}
if (msg.payload.phrase) {
phrase = msg.payload.phrase;
}
translate(phrase + '', conf).then(function (res) {
msg.payload = res.text;
node.send(msg);
}).catch(function (err) {
node.error(err);
});
});
}
RED.nodes.registerType("google-translate", GoogleTranslateNode);
};