-
Notifications
You must be signed in to change notification settings - Fork 65
/
translate.js
146 lines (132 loc) · 4.6 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var querystring = require('querystring');
var got = require('got');
var token = require('./token');
var languages = require('./languages');
function translate(text, opts) {
opts = opts || {};
return token.get(text, opts).then(function (token) {
var url = opts.domain + '/translate_a/single';
var data = {
client: opts.client || 't',
sl: opts.from,
tl: opts.to,
hl: opts.from,
dt: ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'],
ie: 'UTF-8',
oe: 'UTF-8',
otf: 1,
ssel: 0,
tsel: 0,
kc: 7,
q: text
};
data[token.name] = token.value;
return url + '?' + querystring.stringify(data);
}).then(function (url) {
return got(url, {agent: opts.agent}).then(function (res) {
var body = JSON.parse(res.body);
var result = {
from: {
language: {
didYouMean: false,
iso: ''
},
corrected: {
corrected: false,
didYouMean: false,
value: ''
},
text: {
array: [],
value: '',
phonetic: ''
}
},
to: {
text: {
array: [],
value: '',
phonetic: ''
},
translations: [],
definitions: []
}
};
// Language detected
if (body[2] === body[8][0][0]) {
result.from.language.iso = body[2];
} else {
result.from.language.didYouMean = true;
result.from.language.iso = body[8][0][0];
}
// Corrected
var corrected = body[7] || [];
if (corrected[0]) {
var str = corrected[0];
str = str.replace(/<b><i>/g, '[');
str = str.replace(/<\/i><\/b>/g, ']');
result.from.corrected.value = str;
if (corrected[5] === true) {
result.from.corrected.corrected = true;
} else {
result.from.corrected.didYouMean = true;
}
}
// Result & Standard
var indexOfPhonetic = 0;
body[0].forEach(function (obj) {
if (obj[0]) {
result.to.text.array.push(obj[0]);
result.to.text.value += obj[0];
result.from.text.array.push(obj[1]);
result.from.text.value += obj[1];
indexOfPhonetic++;
}
});
var phonetic = body[0][indexOfPhonetic] || [];
if (phonetic[3]) {
result.from.text.phonetic = phonetic[3];
}
if (phonetic[2]) {
result.to.text.phonetic = phonetic[2];
}
// Definitions
if (body[12]) {
body[12].forEach(function (obj) {
var partsOfSpeech = obj[0];
obj[1].forEach(function (obj) {
result.to.definitions.push({
partsOfSpeech: partsOfSpeech || '',
value: obj[0] || '',
example: obj[2] || ''
});
});
});
}
// Translation Of
if (body[1]) {
body[1].forEach(function (obj) {
var partsOfSpeech = obj[0];
obj[2].forEach(function (obj) {
result.to.translations.push({
partsOfSpeech: partsOfSpeech || '',
value: obj[0] || '',
synonyms: obj[1] || [],
frequency: obj[3] || 0.0000
});
});
});
}
return result;
}).catch(function (err) {
err.message += `\nUrl: ${url}`;
if (err.statusCode !== undefined && err.statusCode !== 200) {
err.code = 'BAD_REQUEST';
} else {
err.code = 'BAD_NETWORK';
}
throw err;
});
});
}
module.exports.translate = translate;