Skip to content

Commit

Permalink
Corrigido: Idioma não é detectado no Opera e Kiwi. Fix #784, Fix #425,
Browse files Browse the repository at this point in the history
…Fix #261, #779
  • Loading branch information
FilipePS committed Feb 22, 2024
1 parent a28f9e8 commit 664dd1c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,22 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
return;
}
try {
chrome.tabs.detectLanguage(sender.tab.id, (result) =>
sendResponse(result)
);
if (
(platformInfo.isMobile.any && !platformInfo.isFirefox) ||
(platformInfo.isDesktop.any && platformInfo.isOpera)
) {
chrome.tabs.sendMessage(
sender.tab.id,
{ action: "detectLanguageUsingTextContent" },
{ frameId: 0 },
(result) => sendResponse(result)
);
} else {
chrome.tabs.detectLanguage(sender.tab.id, (result) => {
checkedLastError();
sendResponse(result);
});
}
} catch (e) {
console.error(e);
sendResponse("und");
Expand Down
24 changes: 24 additions & 0 deletions src/contentScript/pageTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,8 @@ Promise.all([twpConfig.onReady(), getTabHostName()]).then(function (_) {
}
};

let textContentLanguageDetectionPromise = null;

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "translatePage") {
if (request.targetLanguage === "original") {
Expand Down Expand Up @@ -1299,6 +1301,28 @@ Promise.all([twpConfig.onReady(), getTabHostName()]).then(function (_) {
pageTranslator.restorePage();
} else if (request.action === "currentTargetLanguage") {
sendResponse(currentTargetLanguage);
} else if (request.action === "detectLanguageUsingTextContent") {
if (textContentLanguageDetectionPromise) {
textContentLanguageDetectionPromise.then((result) =>
sendResponse(result)
);
} else {
textContentLanguageDetectionPromise = new Promise((resolve) => {
chrome.i18n.detectLanguage(
document.body.textContent.trim().slice(0, 1000),
(result) => {
// checkedLastError();
if (result && result.isReliable && result.languages && result.languages.length > 0) {
resolve(result.languages[0].language);
sendResponse(result.languages[0].language);
} else {
sendResponse("und");
}
}
);
});
}
return true;
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/lib/platformInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ twpConfig.onReady(function () {

platformInfo.isDesktop = {
any: !platformInfo.isMobile.any,
Firefox: typeof browser !== "undefined",
};

platformInfo.isFirefox = typeof browser !== "undefined";
platformInfo.isOpera = userAgent.match(/OPR/i);
});
1 change: 1 addition & 0 deletions src/options/release-notes/en.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ <h4>10.0 - February 20, 2024</h4>
<li>Some texts are not localized in the preferred language for the UI</li>
<li>Translate selected text panel appears off-screen on Android</li>
<li>Extension icon does not change color when the page is translated</li>
<li>Page language is not detected in Opera and Kiwi</li>
</ul>
</li>
<li>Updated some language names</li>
Expand Down

0 comments on commit 664dd1c

Please sign in to comment.