/
BergamotTranslator.ts
82 lines (69 loc) · 2.35 KB
/
BergamotTranslator.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
import browser from 'webextension-polyfill';
import { BatchTranslator } from '../../../../thirdparty/bergamot/src/frontend/BatchTranslator';
import { detectLanguage, getMessage } from '../../language';
import { TranslatorBackingWithCache } from './TranslatorBackingWithCache';
export class BergamotTranslator {
static translatorName = getMessage('common_offlineTranslator', 'Bergamot');
static isRequiredKey = () => false;
private translator;
constructor() {
const workerUrl = browser.runtime.getURL(
'thirdparty/bergamot/translator.worker.js',
);
const backing = new TranslatorBackingWithCache({
workerUrl,
// This error handler do nothing, but it's required to `translate` method correct handle exceptions
// With no error handler here we cannot to catch an error when worker does not exists
onerror(err) {
console.warn(
'BergamotTranslator: error in TranslatorBackingWithCache',
err,
);
},
});
this.translator = new BatchTranslator(backing, {
onerror(err) {
console.warn('BergamotTranslator: error in BatchTranslator', err);
},
});
}
translate = async (text: string, from: string, to: string) => {
if (from === 'auto') {
const langs = BergamotTranslator.getSupportedLanguages();
const detectedLanguage = await detectLanguage(text);
if (detectedLanguage !== null && langs.includes(detectedLanguage)) {
from = detectedLanguage;
} else {
// Use most popular content language or first of list
const defaultLang =
to !== 'en' ? 'en' : langs.find((lang) => lang !== to);
from = defaultLang ?? 'en';
}
}
if (from === to) return text;
const response = this.translator.translate({
from,
to,
text,
html: false,
priority: 0,
});
return response.then((response) => response.target.text);
};
translateBatch = (texts: string[], from: string, to: string) =>
Promise.all(texts.map((text) => this.translate(text, from, to)));
getLengthLimit = () => 5000;
getRequestsTimeout = () => 100;
checkLimitExceeding = (text: string) => {
const textLength = !Array.isArray(text)
? text.length
: text.reduce((len, text) => len + text.length, 0);
return textLength - this.getLengthLimit();
};
static isSupportedAutoFrom = () => true;
// prettier-ignore
static getSupportedLanguages = () => [
"fr", "en", "it", "pt", "ru", "cs",
"de", "es", "et", "bg", "uk"
];
}