-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.js
124 lines (117 loc) · 3.67 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
var keys = null;
fetch(chrome.runtime.getURL("keys.json"))
.then(result => result.json())
.then(result => keys = result)
.catch(err => {
console.log(err);
});
/**
* Provides Linguini's supported languages.
* @return {Object}
* Keys: "id"
* Values: "name", "nativeName", "direction"
*/
async function getLanguages() {
var microsoftLanguages = await getMicrosoftLanguages();
var googleLanguages = await getGoogleLanguages();
var languages = {};
for (var [id, language] of Object.entries(microsoftLanguages)) {
if (googleLanguages.includes(id)) {
languages[id] = {
name: language.name,
nativeName: language.nativeName,
direction: language.dir
}
}
}
return languages;
}
/**
* Provides Microsoft's supported languages.
* @return {Object}
* Keys: "id"
* Values: "name", "nativeName", "dir"
*/
async function getMicrosoftLanguages() {
var response = await fetch("https://api.cognitive.microsofttranslator.com/languages?api-version=3.0", {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
response = await response.json();
var languages = response.translation;
return languages;
}
/**
* Provides Microsoft's translation for a given text.
* @param {String} text
* The text to be translated.
* @param {String} targetLanguage
* The id of the language the text will be translated to.
* @return {Object}
* Values: "detected", "confidence", "text"
*/
async function microsoftTranslate(text, targetLanguage) {
var response = await fetch("https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=" + targetLanguage, {
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': keys.microsoft.key,
'Content-Type': 'application/json'
},
body: JSON.stringify([{
"text": text
}])
});
response = await response.json();
var translation = {
detected: response[0].detectedLanguage.language,
confidence: response[0].detectedLanguage.score,
text: response[0].translations[0].text
}
return translation;
}
/**
* Provides Google's supported languages.
* @return {Array}
* Values: "id"
*/
async function getGoogleLanguages() {
// var response = await fetch("https://translation.googleapis.com/language/translate/v2/languages?key=" + keys.google.key, {
// method: 'GET',
// headers: {
// 'Content-Type': 'application/json'
// }
// });
// response = await response.json();
var languages = [];
// response.data.languages.forEach(element => languages.push(element.language));
return languages;
}
/**
* Provides Google's translation for a given text.
* @param {String} text
* The text to be translated.
* @param {String} targetLanguage
* The id of the language the text will be translated to.
* @return {Object}
* Values: "detected", "text"
*/
async function googleTranslate(text, targetLanguage) {
var response = await fetch("https://translation.googleapis.com/language/translate/v2?key=" + keys.google.key, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"q": [text],
"target": targetLanguage
})
});
response = await response.json();
var translation = {
detected: response.data.translations[0].detectedSourceLanguage,
text: response.data.translations[0].translatedText
}
return translation;
}