forked from Foundry376/Mailspring
-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.ts
273 lines (249 loc) · 7.64 KB
/
service.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import LRU from 'lru-cache';
import {
QuotedHTMLTransformer,
localized,
Actions,
MailspringAPIRequest,
RegExpUtils,
} from 'mailspring-exports';
export const TranslatePopupOptions = {
English: 'en',
Spanish: 'es',
Russian: 'ru',
Chinese: 'zh',
Arabic: 'ar',
French: 'fr',
German: 'de',
Italian: 'it',
Japanese: 'ja',
Portuguese: 'pt',
Hindi: 'hi',
Korean: 'ko',
};
export const AllLanguages = {
az: 'Azerbaijan',
ml: 'Malayalam',
sq: 'Albanian',
mt: 'Maltese',
am: 'Amharic',
mk: 'Macedonian',
en: 'English',
mi: 'Maori',
ar: 'Arabic',
mr: 'Marathi',
hy: 'Armenian',
mhr: 'Mari',
af: 'Afrikaans',
mn: 'Mongolian',
eu: 'Basque',
de: 'German',
ba: 'Bashkir',
ne: 'Nepali',
be: 'Belarusian',
no: 'Norwegian',
bn: 'Bengali',
pa: 'Punjabi',
my: 'Burmese',
pap: 'Papiamento',
bg: 'Bulgarian',
fa: 'Persian',
bs: 'Bosnian',
pl: 'Polish',
cy: 'Welsh',
pt: 'Portuguese',
hu: 'Hungarian',
ro: 'Romanian',
vi: 'Vietnamese',
ru: 'Russian',
ht: 'Haitian (Creole)',
ceb: 'Cebuano',
gl: 'Galician',
sr: 'Serbian',
nl: 'Dutch',
si: 'Sinhala',
mrj: 'Hill Mari',
sk: 'Slovakian',
el: 'Greek',
sl: 'Slovenian',
ka: 'Georgian',
sw: 'Swahili',
gu: 'Gujarati',
su: 'Sundanese',
da: 'Danish',
tg: 'Tajik',
he: 'Hebrew',
th: 'Thai',
yi: 'Yiddish',
tl: 'Tagalog',
id: 'Indonesian',
ta: 'Tamil',
ga: 'Irish',
tt: 'Tatar',
it: 'Italian',
te: 'Telugu',
is: 'Icelandic',
tr: 'Turkish',
es: 'Spanish',
udm: 'Udmurt',
kk: 'Kazakh',
uz: 'Uzbek',
kn: 'Kannada',
uk: 'Ukrainian',
ca: 'Catalan',
ur: 'Urdu',
ky: 'Kyrgyz',
fi: 'Finnish',
zh: 'Chinese',
fr: 'French',
ko: 'Korean',
hi: 'Hindi',
xh: 'Xhosa',
hr: 'Croatian',
km: 'Khmer',
cs: 'Czech',
lo: 'Laotian',
sv: 'Swedish',
la: 'Latin',
gd: 'Scottish',
lv: 'Latvian',
et: 'Estonian',
lt: 'Lithuanian',
eo: 'Esperanto',
lb: 'Luxembourgish',
jv: 'Javanese',
mg: 'Malagasy',
ja: 'Japanese',
ms: 'Malay',
};
export const TranslationsUsedLexicon = {
headerText: localized('All Translations Used'),
rechargeText: `${localized(
'You can translate up to %1$@ emails each %2$@ with Mailspring Basic.'
)} ${localized('Upgrade to Pro today!')}`,
iconUrl: 'mailspring://translation/assets/[email protected]',
};
function forEachTranslatableText(doc: Document, callback: (el: Node, text: string) => void) {
const textWalker = document.createTreeWalker(doc.body, NodeFilter.SHOW_TEXT);
const urlRegexp = RegExpUtils.urlRegex({ matchStartOfString: true, matchTailOfString: true });
const usaAddressRegexp = /, [A-Z]{2},? [A-Z]{2,3},? [\d]{5}/; // matches ", CA 94114"
while (textWalker.nextNode()) {
const node = textWalker.currentNode;
if (['SCRIPT', 'STYLE', 'LINK', 'META', 'TITLE'].includes(node.parentElement.tagName)) {
continue;
}
if (node.parentElement.closest('.notranslate')) {
continue; // the HTML explicitly requests this text not be translated
}
const text = node.textContent.replace(/\s{2,}/g, ' ');
if (!/[A-Za-z\p{L}]+/u.test(text)) {
continue; // there are no latin or unicode letters in the string
}
if (urlRegexp.test(text.trim())) {
continue; // purely a plaintext link
}
if (text.length < 250 && usaAddressRegexp.test(text)) {
continue; // looks very much like a US address. Don't awkwardly translate these
}
const closestWithFont = node.parentElement.closest('[style*=font]');
if (closestWithFont instanceof HTMLElement) {
const family = closestWithFont.style.fontFamily || closestWithFont.style.font || '';
if (
family.includes('monospace') ||
family.includes('Monaco') ||
family.includes('Lucida Console') ||
family.includes('Courier')
) {
continue; // the text is in a monospace font and is probably a meaningful "value / variable"
}
const size = Number((closestWithFont.style.fontSize || '').replace(/[A-Za-z]+/g, ''));
if (size > 0 && size < 12) {
continue; // the text is tiny or hidden, don't waste translation characters on footer stuff
}
}
if (text.length > 2) {
// we will translate this text. But trim out large blocks of whitespace that may
// exist in the raw textContent to avoid those counting as translation characters.
// NOTE: We use node.textContent here, not the trimmed version. The leading/trailing
// space are necessary for things like <span>this is a <a>link</a></span>
callback(node, text);
}
}
}
// We maintain a cache of blocks of text we've translated. Because the user may have a whole
// mailbox of very similar or templated emails, this can cut down on the amount of text we
// need to translate for a new email dramatically.
let translatedSnippetCache = new LRU<string, string>({ max: 1000 });
let translatedSnippetLang = '';
export async function translateMessageBody(
html: string,
targetLang?: string,
silent: boolean = false
): Promise<string | false> {
if (translatedSnippetLang !== targetLang) {
translatedSnippetCache.reset();
translatedSnippetLang = targetLang;
}
const replyHtml = QuotedHTMLTransformer.removeQuotedHTML(html);
// break the document down into text blocks to translate. We don't send the HTML
// because translation services bill by the character and it's unclear if a giant
// <style> tag counts.
const domParser = new DOMParser();
const doc = domParser.parseFromString(replyHtml, 'text/html');
const translationDoc = document.createElement('div');
// Iterate over the HTML document and pull out text blocks we need to translate via the API
// because LRU cache can change while the req is in flight
const skipped: { [text: string]: string } = {};
let totalChars = 0;
forEachTranslatableText(doc, (node, text) => {
const t = translatedSnippetCache.get(text);
if (t) {
skipped[text] = t;
return;
}
if (totalChars > 5000) {
return;
}
const b = document.createElement('b');
b.textContent = text;
totalChars += text.length + 7; // <b></b>;
translationDoc.appendChild(b);
});
let resultElements = [];
// Make the translation request
if (translationDoc.childElementCount > 0) {
const translationHTML = translationDoc.innerHTML.replace(/ /g, ' '); // nbsp char
let response = null;
try {
throw new Error('Not supported in this build :(');
} catch (error) {
Actions.closePopover();
if (!silent) {
const dialog = require('electron').remote.dialog;
dialog.showErrorBox(localized('Language Conversion Failed'), error.toString());
}
return false;
}
const resultDoc = domParser.parseFromString(response.result, 'text/html');
resultElements = Array.from(resultDoc.querySelectorAll('b'));
}
// Merge the results back in to the original document by traversing it in the same order
// and inserting the result back in. Note that we have saved our cache hits into `skipped`
// and read them from there to be 100% sure the translation indexes still align with the doc.
forEachTranslatableText(doc, (node, text) => {
if (skipped[text]) {
node.textContent = skipped[text];
} else {
const resultElement = resultElements.shift();
if (resultElement) {
const translated = resultElement.textContent;
node.textContent = translated;
translatedSnippetCache.set(text, translated);
} else {
// we may have hit the `totalChars` per-email translation limit.
// nothing more to do here!
}
}
});
// Put the quoted text back in!
return QuotedHTMLTransformer.appendQuotedHTML(doc.body.innerHTML, html);
}