Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
support language detect, now all paragraph will be detected the langa…
Browse files Browse the repository at this point in the history
…uage first, only its not the target language, itll be translate.
  • Loading branch information
theowenyoung committed Nov 12, 2022
1 parent 1bbc645 commit 31e84d2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse(tabToMimeType[tabs[0].id])
})
return true
}else if(request.action ==='detectLanguage'){
chrome.i18n.detectLanguage(request.text, function(result){
if(result.languages.length > 0){
sendResponse(result.languages[0].language);
}else{
sendResponse(undefined);
}
});
return true
}
})

Expand Down
10 changes: 10 additions & 0 deletions src/background/chrome_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse(tabToMimeType[tabs[0].id])
})
return true
}else if(request.action ==='detectLanguage'){
chrome.i18n.detectLanguage(request.text, function(result){
if(result.languages.length > 0){
sendResponse(result.languages[0].language);
}else{
sendResponse(undefined);
}
});
return true
}

})

function updateTranslateSelectedContextMenu() {
Expand Down
2 changes: 1 addition & 1 deletion src/chrome_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"default_locale": "en",
"name": "Immersive Translate",
"description": "Let's experience immersive web translation, with bilingual simultaneous display and translation of only the important content.",
"version": "0.0.20.1",
"version": "0.0.21",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"commands": {
Expand Down
37 changes: 34 additions & 3 deletions src/contentScript/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const enhanceOriginalDisplayValueAttributeName = "data-translationoriginaldispla
const enhanceHtmlTagsInlineIgnore = ['BR', 'CODE', 'KBD', 'WBR'] // and input if type is submit or button, and pre depending on settings
const enhanceHtmlTagsNoTranslate = ['TITLE', 'SCRIPT', 'STYLE', 'TEXTAREA', 'SVG', 'svg'] //TODO verificar porque 'svg' é com letras minúsculas
const blockElements = [
'H1', 'H2', 'H3', 'H4', 'H5', 'H6','TABLE', 'LI', 'P',
'H1', 'H2', 'H3', 'H4', 'H5', 'H6','TABLE', 'OL',"UL", 'P',
];
if (twpConfig.get('translateTag_pre') !== 'yes') {
blockElements.push('PRE')
Expand Down Expand Up @@ -328,7 +328,7 @@ function isDuplicatedChild(array,child){
}
return false;
}
function getNodesThatNeedToTranslate(root,ctx,options){
async function getNodesThatNeedToTranslate(root,ctx,options){
options = options || {};
const pageSpecialConfig = getPageSpecialConfig(ctx);
const twpConfig = ctx.twpConfig
Expand Down Expand Up @@ -429,6 +429,23 @@ function getNodesThatNeedToTranslate(root,ctx,options){
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
})


// check node language is target language, if yes, remove it

let newAllNodes = [];
for(const node of allNodes){
const nodeText = node.innerText;
if(nodeText && nodeText.trim().length>0){
const lang = await detectLanguage(nodeText);
if(lang && !currentTargetLanguage.startsWith(lang)){
// only translate the clearly language
newAllNodes.push(node);
}
}
}

allNodes = newAllNodes;

if(!isShowDualLanguage){
return allNodes;
}
Expand Down Expand Up @@ -457,7 +474,8 @@ function getNodesThatNeedToTranslate(root,ctx,options){
copyNode.style.paddingRight = "8px";
}else{
// if not li element
if(copyNode.nodeName.toLowerCase() !== "li"){
const copiedNodeName = copyNode.nodeName.toLowerCase();
if(!['p','ul','ol','li'].includes(copiedNodeName)){
copyNode.style.paddingBottom = "8px";
}
}
Expand Down Expand Up @@ -652,3 +670,16 @@ function addStyle(){
}

addStyle()


function detectLanguage(text) {
// send message to background
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
action: "detectLanguage",
text: text
}, response => {
resolve(response)
})
})
}
14 changes: 7 additions & 7 deletions src/contentScript/pageTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ Promise.all([twpConfig.onReady(), getTabUrl()])

let nodesToRestore = []

function translateNewNodes() {
async function translateNewNodes() {
try {
newNodes.forEach(nn => {
if (removedNodes.indexOf(nn) != -1) return;
for(const nn of newNodes) {
if (removedNodes.indexOf(nn) != -1) continue;

// let newPiecesToTranslate = getPiecesToTranslate(nn)
let newPiecesToTranslate = getNodesThatNeedToTranslate(nn,ctx).reduce((acc, node) => {
let newPiecesToTranslate = (await getNodesThatNeedToTranslate(nn,ctx)).reduce((acc, node) => {
return acc.concat(getPiecesToTranslate(node))
}, [])

Expand All @@ -292,7 +292,7 @@ Promise.all([twpConfig.onReady(), getTabUrl()])
piecesToTranslate.push(newPiecesToTranslate[i])
}
}
})
}
} catch (e) {
console.error(e)
} finally {
Expand Down Expand Up @@ -829,7 +829,7 @@ Promise.all([twpConfig.onReady(), getTabUrl()])
pageLanguageStateObservers.push(callback)
}

pageTranslator.translatePage = function (targetLanguage) {
pageTranslator.translatePage = async function (targetLanguage) {
fooCount++
pageTranslator.restorePage()
showOriginal.enable()
Expand All @@ -843,7 +843,7 @@ Promise.all([twpConfig.onReady(), getTabUrl()])
// piecesToTranslate = getPiecesToTranslate()
try{

piecesToTranslate = getNodesThatNeedToTranslate(document.body,ctx).reduce((acc, node) => {
piecesToTranslate = (await getNodesThatNeedToTranslate(document.body,ctx)).reduce((acc, node) => {
return acc.concat(getPiecesToTranslate(node))
}, [])
}catch(e){
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"default_locale": "en",
"name": "Immersive Translate",
"description": "Let's experience immersive web translation, with bilingual simultaneous display and translation of only the important content.",
"version": "0.0.20.1",
"version": "0.0.21",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"browser_specific_settings": {
Expand Down

0 comments on commit 31e84d2

Please sign in to comment.