-
Notifications
You must be signed in to change notification settings - Fork 0
/
language-translate.js
57 lines (44 loc) · 1.91 KB
/
language-translate.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var https = require("https");
const request = require('request'); //npm install request --save
const mysql = require('mysql'); //npm install mysql --save
const nodemailer = require('nodemailer'); //npm install nodemailer --save
const translate = require('google-translate-api');
self.addEventListener('message', function(e) {
self.postMessage('now in from worker: ', self.name);
console.log('worker: '+JSON.stringify(e.data,null,2));
let langAsync = async () => {
//let's translate
if(e.data.sentences.sentences == null){
//terminating things... ON ERROR.
self.postMessage({terminatingWorker: "Worker initiate failed."});
console.log('terminating worker...');
self.close();
}else{
var translations = [];
let sentences = e.data.sentences.sentences;
for (var i = 0; i < sentences.length; i++) {
let sentence = sentences[i];
let res = await translate(sentence, {from: 'en', to: e.data.sentences.language})
/*.then(res => {
})*/
.catch(err => {
//terminating things... ON ERROR.
self.postMessage({terminatingWorker: "translation failed: "+err});
console.log('terminating worker...');
self.close();
});
//use res Here.
translations.push(res);
}
//success ENd
//send value back to global(parent) last feerate.
self.postMessage({
translations: translations,
workerIsFinished: true
});
console.log('terminating worker...');
self.close();
}
}
langAsync();
}, false);