-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
Copy pathpageTranslations.js
149 lines (144 loc) · 3.57 KB
/
pageTranslations.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
147
148
149
const webviews = require('webviews.js')
const statistics = require('js/statistics.js')
const settings = require('util/settings/settings.js')
const pageTranslations = {
apiURL: 'https://translate-api.minbrowser.org/translate',
translatePrivacyInfo: 'When you translate a page, the page contents are sent to Min\'s servers. We don\'t save your translations or use them to identify you.',
languages: [
{
name: 'English',
code: 'en'
},
{
name: 'Arabic',
code: 'ar'
},
{
name: 'Chinese',
code: 'zh'
},
{
name: 'Dutch',
code: 'nl'
},
{
name: 'French',
code: 'fr'
},
{
name: 'German',
code: 'de'
},
{
name: 'Hindi',
code: 'hi'
},
{
name: 'Indonesian',
code: 'id'
},
{
name: 'Irish',
code: 'ga'
},
{
name: 'Italian',
code: 'it'
},
{
name: 'Japanese',
code: 'ja'
},
{
name: 'Korean',
code: 'ko'
},
{
name: 'Polish',
code: 'pl'
},
{
name: 'Portuguese',
code: 'pt'
},
{
name: 'Russian',
code: 'ru'
},
{
name: 'Spanish',
code: 'es'
},
{
name: 'Turkish',
code: 'tr'
},
{
name: 'Ukranian',
code: 'uk'
},
{
name: 'Vietnamese',
code: 'vi'
}
],
getLanguageList: function () {
const userPrefs = navigator.languages.map(lang => lang.split('-')[0])
const topLangs = pageTranslations.languages.filter(lang => userPrefs.includes(lang.code))
// Translations to/from English are the highest quality in Libretranslate, so always show that near the top
if (!topLangs.some(lang => lang.code === 'en')) {
topLangs.push(pageTranslations.languages.find(lang => lang.code === 'en'))
}
const otherLangs = pageTranslations.languages.filter(lang => !userPrefs.includes(lang.code) && lang.code !== 'en')
return [topLangs, otherLangs]
},
translateInto (tabId, language) {
statistics.incrementValue('translatePage.' + language)
if (!settings.get('translatePrivacyPrompt')) {
const accepted = confirm(pageTranslations.translatePrivacyInfo)
if (accepted) {
settings.set('translatePrivacyPrompt', true)
} else {
return
}
}
webviews.callAsync(tabId, 'send', ['translate-page', language])
},
makeTranslationRequest: async function (tab, data) {
const requestOptions = {
method: 'POST',
body: JSON.stringify({
q: data[0].query,
source: 'auto',
target: data[0].lang
}),
headers: { 'Content-Type': 'application/json' }
}
fetch(pageTranslations.apiURL, requestOptions)
.then(res => res.json())
.then(function (result) {
console.log(result)
webviews.callAsync(tab, 'send', ['translation-response-' + data[0].requestId, {
response: result
}])
})
.catch(function (e) {
// retry once
setTimeout(function () {
console.warn('retrying translation request')
fetch(pageTranslations.apiURL, requestOptions)
.then(res => res.json())
.then(function (result) {
console.log('after retry', result)
webviews.callAsync(tab, 'send', ['translation-response-' + data[0].requestId, {
response: result
}])
})
}, 5000)
})
},
initialize: function () {
webviews.bindIPC('translation-request', this.makeTranslationRequest)
}
}
module.exports = pageTranslations