/
language.js
279 lines (213 loc) · 10.2 KB
/
language.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
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
274
275
276
277
278
279
/*
This file is part of Talkie -- text-to-speech browser extension button.
<https://github.com/joelpurra/talkie>
Copyright (c) 2016, 2017 Joel Purra <https://joelpurra.com/>
Talkie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Talkie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Talkie. If not, see <https://www.gnu.org/licenses/>.
*/
import {
log,
logError,
} from "../shared/log";
import {
promiseTry,
} from "../shared/promise";
import {
isUndefinedOrNullOrEmptyOrWhitespace,
shallowCopy,
} from "../shared/basic";
import {
executeLogToPage,
} from "../shared/execute";
import {
messagesLocale,
} from "../shared/configuration";
const noTextSelectedMessage = {
text: browser.i18n.getMessage("noTextSelectedMessage"),
effectiveLanguage: messagesLocale,
};
const noVoiceForLanguageDetectedMessage = {
text: browser.i18n.getMessage("noVoiceForLanguageDetectedMessage"),
effectiveLanguage: browser.i18n.getMessage("noVoiceForLanguageDetectedMessageLanguage"),
};
export const detectPageLanguage = () => promiseTry(
() => {
// https://developer.browser.com/extensions/tabs#method-detectLanguage
return browser.tabs.detectLanguage()
.then((language) => {
log("detectPageLanguage", "Browser detected primary page language", language);
// The language fallback value is "und", so treat it as no language.
if (!language || typeof language !== "string" || language === "und") {
return null;
}
return language;
})
.catch((error) => {
// https://github.com/joelpurra/talkie/issues/3
// NOTE: It seems the Vivaldi browser doesn't (yet/always) support detectLanguage.
// As this is not critical, just log the error and resolve with null.
logError("detectPageLanguage", error);
return null;
});
}
);
const detectTextLanguage = (text) => promiseTry(
() => {
if (!("detectLanguage" in browser.i18n)) {
// NOTE: text-based language detection is only used as a fallback.
log("detectTextLanguage", "Browser does not support detecting text language");
return null;
}
// https://developer.browser.com/extensions/i18n#method-detectLanguage
return browser.i18n.detectLanguage(text)
.then((result) => {
// The language fallback value is "und", so treat it as no language.
if (
!result.isReliable
|| !result.languages
|| !(result.languages.length > 0)
|| typeof result.languages[0].language !== "string"
|| !(result.languages[0].language.trim().length > 0)
|| result.languages[0].language === "und"
) {
// NOTE: text-based language detection is only used as a fallback.
log("detectTextLanguage", "Browser did not detect reliable text language", result);
return null;
}
const primaryDetectedTextLanguage = result.languages[0].language;
log("detectTextLanguage", "Browser detected reliable text language", result, primaryDetectedTextLanguage);
return primaryDetectedTextLanguage;
});
}
);
const getSelectionsWithValidText = (selections) => promiseTry(
() => {
const isNonNullObject = (selection) => !!selection && typeof selection === "object";
const hasValidText = (selection) => !isUndefinedOrNullOrEmptyOrWhitespace(selection.text);
const trimText = (selection) => {
const copy = shallowCopy(selection);
copy.text = copy.text.trim();
return copy;
};
const selectionsWithValidText = selections
.filter(isNonNullObject)
.filter(hasValidText)
.map(trimText)
.filter(hasValidText);
return selectionsWithValidText;
}
);
const detectAndAddLanguageForSelections = (selectionsWithValidText) => promiseTry(
() => Promise.all(
selectionsWithValidText.map(
(selection) => {
const copy = shallowCopy(selection);
return detectTextLanguage(copy.text)
.then((detectedTextLanguage) => {
copy.detectedTextLanguage = detectedTextLanguage;
return copy;
});
})
)
);
const isKnownVoiceLanguage = (allVoices, elementLanguage) => allVoices.some((voice) => voice.lang.startsWith(elementLanguage));
// https://www.iso.org/obp/ui/#iso:std:iso:639:-1:ed-1:v1:en
// https://en.wikipedia.org/wiki/ISO_639-1
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
// http://xml.coverpages.org/iso639a.html
// NOTE: discovered because Twitter seems to still use "iw".
const iso639Dash1Aliases1988To2002 = {
"in": "id",
"iw": "he",
"ji": "yi",
};
const mapIso639Aliases = (language) => {
return iso639Dash1Aliases1988To2002[language] || language;
};
const isValidString = (str) => !isUndefinedOrNullOrEmptyOrWhitespace(str);
const cleanupLanguagesArray = (allVoices, languages) => {
const copy = (languages || [])
.filter(isValidString)
.map(mapIso639Aliases)
.filter(isKnownVoiceLanguage.bind(null, allVoices));
return copy;
};
const getSelectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage = (allVoices, detectedPageLanguage, selectionsWithValidTextAndDetectedLanguage) => promiseTry(
() => {
const cleanupParentElementsLanguages = (selection) => {
const copy = shallowCopy(selection);
copy.parentElementsLanguages = cleanupLanguagesArray(allVoices, copy.parentElementsLanguages);
return copy;
};
const getMoreSpecificLanguagesWithPrefix = (prefix) => {
return (language) => language.startsWith(prefix) && language.length > prefix.length;
};
const setEffectiveLanguage = (selection) => {
const copy = shallowCopy(selection);
const detectedLanguages = [
copy.detectedTextLanguage,
copy.parentElementsLanguages[0] || null,
copy.htmlTagLanguage,
detectedPageLanguage,
];
log("setEffectiveLanguage", "detectedLanguages", detectedLanguages);
const cleanedLanguages = cleanupLanguagesArray(allVoices, detectedLanguages);
log("setEffectiveLanguage", "cleanedLanguages", cleanedLanguages);
const primaryLanguagePrefix = cleanedLanguages[0] || null;
log("setEffectiveLanguage", "primaryLanguagePrefix", primaryLanguagePrefix);
// NOTE: if there is a more specific language with the same prefix among the detected languages, prefer it.
const cleanedLanguagesWithPrimaryPrefix = cleanedLanguages.filter(getMoreSpecificLanguagesWithPrefix(primaryLanguagePrefix));
log("setEffectiveLanguage", "cleanedLanguagesWithPrimaryPrefix", cleanedLanguagesWithPrimaryPrefix);
const effectiveLanguage = cleanedLanguagesWithPrimaryPrefix[0] || cleanedLanguages[0] || null;
log("setEffectiveLanguage", "effectiveLanguage", effectiveLanguage);
copy.effectiveLanguage = effectiveLanguage;
executeLogToPage("Language", "Selected text language:", copy.detectedTextLanguage);
executeLogToPage("Language", "Selected text element language:", copy.parentElementsLanguages[0] || null);
executeLogToPage("Language", "HTML tag language:", copy.htmlTagLanguage);
executeLogToPage("Language", "Detected page language:", detectedPageLanguage);
executeLogToPage("Language", "Effective language:", copy.effectiveLanguage);
return copy;
};
const selectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage = selectionsWithValidTextAndDetectedLanguage
.map(cleanupParentElementsLanguages)
.map(setEffectiveLanguage);
return selectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage;
}
);
const useFallbackMessageIfNoLanguageDetected = (selectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage) => promiseTry(
() => {
const fallbackMessageForNoLanguageDetected = (selection) => {
if (selection.effectiveLanguage === null) {
return noVoiceForLanguageDetectedMessage;
}
return selection;
};
const mapResults = (selection) => {
return {
text: selection.text,
effectiveLanguage: selection.effectiveLanguage,
};
};
const results = selectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage
.map(fallbackMessageForNoLanguageDetected)
.map(mapResults);
if (results.length === 0) {
log("Empty filtered selections");
results.push(noTextSelectedMessage);
}
return results;
}
);
export const cleanupSelections = (allVoices, detectedPageLanguage, selections) => Promise.resolve()
.then(() => getSelectionsWithValidText(selections))
.then((selectionsWithValidText) => detectAndAddLanguageForSelections(selectionsWithValidText))
.then((selectionsWithValidTextAndDetectedLanguage) => getSelectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage(allVoices, detectedPageLanguage, selectionsWithValidTextAndDetectedLanguage))
.then((selectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage) => useFallbackMessageIfNoLanguageDetected(selectionsWithValidTextAndDetectedLanguageAndEffectiveLanguage));