-
Notifications
You must be signed in to change notification settings - Fork 4
/
Translator.js
37 lines (35 loc) · 895 Bytes
/
Translator.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
32
33
34
35
36
37
var request = require("request");
var Translator = function(query,tl,callback) {
if (!query.language) {
console.trace();
return;
}
if (query.language == tl) {
callback(null,query);
return;
};
request({
url: "http://translate.google.com/translate_a/t?client=t&sl="+query.language+"&tl="+tl+"&hl=de&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=2&ssel=0&tsel=0&q=" + encodeURIComponent(query.text),
headers: {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
}
}, function(err,resp,body) {
if (err) {
callback("ERROR");
return;
}
body = body.replace(/[,]+/g,",");
var obj = {};
try {
obj = JSON.parse(body);
result = obj[0][0][0];
callback(null,{
text:result,
language: tl
});
} catch(e) {
callback("JSON ERROR",body);
}
})
}
module.exports = Translator;