-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTranslator.js
38 lines (35 loc) · 1.05 KB
/
StringTranslator.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
38
import v4 from "uuid/dist/v4.js"
import request from "request";
class StringTranslator {
//Constructor
constructor(tradString, sLang, tLang) {
this.key = "4b709d313c17489bbe2cc41dc664e0ae";
this.host = "https://api.cognitive.microsofttranslator.com";
this.options = {
method: 'POST',
baseUrl: this.host,
url: 'translate',
qs: {
'api-version': '3.0',
'to': [sLang, tLang]
},
headers: {
'Ocp-Apim-Subscription-Key': this.key,
"Ocp-Apim-Subscription-Region": "canadacentral",
'Content-type': 'application/json',
'X-ClientTraceId': v4().toString()
},
body: [{
'text': tradString
}],
json: true,
};
}
//translate() function
translate(cb){
request(this.options, function(err, res, body){
cb(body[0]['translations'][1]['text']);
});
}
}
export {StringTranslator}