-
-
Notifications
You must be signed in to change notification settings - Fork 918
Expand file tree
/
Copy pathTranslator.js
More file actions
184 lines (160 loc) · 4.97 KB
/
Copy pathTranslator.js
File metadata and controls
184 lines (160 loc) · 4.97 KB
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
const kuroshiroPath = "https://cdn.jsdelivr.net/npm/kuroshiro@1.2.0/dist/kuroshiro.min.js";
const kuromojiPath = "https://cdn.jsdelivr.net/npm/kuroshiro-analyzer-kuromoji@1.1.0/dist/kuroshiro-analyzer-kuromoji.min.js";
const aromanize = "https://cdn.jsdelivr.net/npm/aromanize@0.1.5/aromanize.min.js";
const openCCPath = "https://cdn.jsdelivr.net/npm/opencc-js@1.0.5/dist/umd/full.min.js";
const pinyinProPath = "https://cdn.jsdelivr.net/npm/pinyin-pro@3.28.1/dist/index.min.js";
const dictPath = "https:/cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict";
class Translator {
constructor(lang, isUsingNetease = false) {
this.finished = {
ja: false,
ko: false,
zh: false,
};
this.isUsingNetease = isUsingNetease;
this.applyKuromojiFix();
this.injectExternals(lang);
this.createTranslator(lang);
}
includeExternal(url, forceInclude = false) {
if ((forceInclude || CONFIG.visual.translate || this.isUsingNetease) && !document.querySelector(`script[src="${url}"]`)) {
const script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", url);
document.head.appendChild(script);
}
}
isUsingRomanization() {
return CONFIG.visual["romanization"] && CONFIG.visual["romanization"] !== "none";
}
injectExternals(lang) {
switch (lang?.slice(0, 2)) {
case "ja":
this.includeExternal(kuromojiPath);
this.includeExternal(kuroshiroPath);
break;
case "ko":
this.includeExternal(aromanize);
break;
case "zh":
this.includeExternal(openCCPath);
this.includeExternal(pinyinProPath, this.isUsingRomanization());
break;
}
}
async awaitFinished(language) {
return new Promise((resolve) => {
const interval = setInterval(() => {
this.injectExternals(language);
this.createTranslator(language);
const lan = language.slice(0, 2);
if (this.finished[lan]) {
clearInterval(interval);
resolve();
}
}, 100);
});
}
/**
* Fix an issue with kuromoji when loading dict from external urls
* Adapted from: https://github.com/mobilusoss/textlint-browser-runner/pull/7
*/
applyKuromojiFix() {
if (typeof XMLHttpRequest.prototype.realOpen !== "undefined") return;
XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url, bool) {
if (url.indexOf(dictPath.replace("https://", "https:/")) === 0) {
this.realOpen(method, url.replace("https:/", "https://"), bool);
} else {
this.realOpen(method, url, bool);
}
};
}
async createTranslator(lang) {
switch (lang.slice(0, 2)) {
case "ja":
if (this.kuroshiro) return;
if (typeof Kuroshiro === "undefined" || typeof KuromojiAnalyzer === "undefined") {
await Translator.#sleep(50);
return this.createTranslator(lang);
}
this.kuroshiro = new Kuroshiro.default();
this.kuroshiro.init(new KuromojiAnalyzer({ dictPath })).then(
function () {
this.finished.ja = true;
}.bind(this)
);
break;
case "ko":
if (this.Aromanize) return;
if (typeof Aromanize === "undefined") {
await Translator.#sleep(50);
return this.createTranslator(lang);
}
this.Aromanize = Aromanize;
this.finished.ko = true;
break;
case "zh": {
const needsOpenCC = CONFIG.visual.translate || this.isUsingNetease;
const needsPinyin = this.isUsingRomanization();
if (needsOpenCC && typeof OpenCC === "undefined") {
await Translator.#sleep(50);
return this.createTranslator(lang);
}
if (needsPinyin && typeof pinyinPro === "undefined") {
await Translator.#sleep(50);
return this.createTranslator(lang);
}
if (needsOpenCC && !this.OpenCC) this.OpenCC = OpenCC;
if (needsPinyin && !this.pinyinPro) this.pinyinPro = pinyinPro;
this.finished.zh = true;
break;
}
}
}
async romajifyText(text, target = "romaji", mode = "spaced") {
if (!this.finished.ja) {
await Translator.#sleep(100);
return this.romajifyText(text, target, mode);
}
return this.kuroshiro.convert(text, {
to: target,
mode: mode,
});
}
async convertToRomaja(text, target) {
if (!this.finished.ko) {
await Translator.#sleep(100);
return this.convertToRomaja(text, target);
}
if (target === "hangul") return text;
return Aromanize.hangulToLatin(text, "rr-translit");
}
async convertChinese(text, from, target) {
if (!this.finished.zh) {
await Translator.#sleep(100);
return this.convertChinese(text, from, target);
}
const converter = this.OpenCC.Converter({
from: from,
to: target,
});
return converter(text);
}
async convertToPinyin(text) {
if (!this.finished.zh || !this.pinyinPro) {
await Translator.#sleep(100);
return this.convertToPinyin(text);
}
return this.pinyinPro.pinyin(text, { toneType: "symbol", nonZh: "consecutive" });
}
/**
* Async wrapper of `setTimeout`.
*
* @param {number} ms
* @returns {Promise<void>}
*/
static async #sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}