Disable the pageAction functionality is the browser's language is not supported (#219)

This commit is contained in:
Andre Natal 2022-04-06 17:00:53 -07:00 коммит произвёл GitHub
Родитель 87aa5ce4d7
Коммит b48bd55c9e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 48 добавлений и 19 удалений

Просмотреть файл

@ -75,11 +75,21 @@ const messageListener = function(message, sender) {
case "detectPageLanguage":
if (!modelFastText) break;
languageDetection = Object.assign(new LanguageDetection(), message.languageDetection);
/*
* if we don't support this browser's language, we nede to hide the
* page action and bail right away
*/
if (!languageDetection.isBrowserSupported()) {
browser.pageAction.hide(sender.tab.id);
break;
}
/*
* call the cld experiment to detect the language of the snippet
* extracted from the page
*/
languageDetection = Object.assign(new LanguageDetection(), message.languageDetection);
languageDetection.pageLanguage = modelFastText
.predict(languageDetection.wordsToDetect.trim().replace(/(\r\n|\n|\r)/gm, ""), 1, 0.0)
.get(0)[1]
@ -335,24 +345,33 @@ browser.pageAction.onClicked.addListener(tab => {
* if the user clicks the pageAction, we summon the infobar, and for that we
* need to let the infobar api know that this is on demand-request, which
* doesn't have a language detected, so for that reason we set the language
* parameter as 'userrequest', in order to override the preferences
* parameter as 'userrequest', in order to override the preferences.
*/
browser.experiments.translationbar.show(
tab.id,
"userrequest",
languageDetection.navigatorLanguage,
{
displayStatisticsMessage: browser.i18n.getMessage("displayStatisticsMessage"),
outboundTranslationsMessage: browser.i18n.getMessage("outboundTranslationsMessage"),
qualityEstimationMessage: browser.i18n.getMessage("qualityEstimationMessage"),
surveyMessage: browser.i18n.getMessage("surveyMessage"),
languageDefaultOption: browser.i18n.getMessage("languageDefaultOption")
},
true,
{
outboundtranslations: await browser.storage.local.get("outboundtranslations-check"),
qualityestimations: await browser.storage.local.get("qualityestimations-check")
}
);
if (languageDetection.isBrowserSupported()) {
browser.experiments.translationbar.show(
tab.id,
"userrequest",
languageDetection.navigatorLanguage,
{
displayStatisticsMessage: browser.i18n.getMessage("displayStatisticsMessage"),
outboundTranslationsMessage: browser.i18n.getMessage("outboundTranslationsMessage"),
qualityEstimationMessage: browser.i18n.getMessage("qualityEstimationMessage"),
surveyMessage: browser.i18n.getMessage("surveyMessage"),
languageDefaultOption: browser.i18n.getMessage("languageDefaultOption")
},
true,
{
outboundtranslations: await browser.storage.local.get("outboundtranslations-check"),
qualityestimations: await browser.storage.local.get("qualityestimations-check")
}
);
} else {
/*
* if we don't support this browser's language, we nede to hide the
* page action and bail right away
*/
browser.pageAction.hide(tab.id);
}
});
});

Просмотреть файл

@ -45,4 +45,13 @@ class LanguageDetection {
isLangMismatch() {
return !this.navigatorLanguage.includes(this.pageLanguage);
}
isBrowserSupported() {
const languageSet = new Set()
for (const languagePair of Object.keys(modelRegistry)) {
const secondLang = languagePair.substring(2, 4);
languageSet.add(secondLang);
}
return languageSet.has(this.navigatorLanguage);
}
}

Просмотреть файл

@ -25,6 +25,7 @@
"background": {
"persistent": true,
"scripts": [
"model/modelRegistry.js",
"model/static/errorReporting/sentry.js",
"settings.js",
"controller/translation/bergamotTranslatorVersion.js",