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

Commit

Permalink
fix language detect
Browse files Browse the repository at this point in the history
  • Loading branch information
theowenyoung committed Nov 12, 2022
1 parent dadf36c commit 7407d76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
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.21",
"version": "0.0.22",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"commands": {
Expand Down
29 changes: 27 additions & 2 deletions src/contentScript/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ async function getNodesThatNeedToTranslate(root,ctx,options){
// check language
try{
const lang = node.getAttribute("lang");
if(lang && currentTargetLanguage.startsWith(lang)){
if(lang && checkIsSameLanguage(lang,currentTargetLanguage,ctx)){
continue;
}
}catch(e){
Expand Down Expand Up @@ -437,7 +437,7 @@ async function getNodesThatNeedToTranslate(root,ctx,options){
const nodeText = node.innerText;
if(nodeText && nodeText.trim().length>0){
const lang = await detectLanguage(nodeText);
if(lang && !currentTargetLanguage.startsWith(lang)){
if(lang && !checkIsSameLanguage(lang,currentTargetLanguage,ctx)){
// only translate the clearly language
newAllNodes.push(node);
}
Expand Down Expand Up @@ -683,3 +683,28 @@ addStyle()
})
})
}

function checkIsSameLanguage(lang,currentTargetLang,ctx){
const finalLang = twpLang.fixTLanguageCode(lang);
if(!finalLang){
return false;
}
if(finalLang === currentTargetLang){
return true;
}

// for api does not has the best detect for zh-CN and zh-TW
// we will treat zh-CN and zh-TW as same language
// we focus on the dual language display, so zh-TW -> zh-CN is not the first priority to fix,
// I think people will not use it to learn zh-TW to zh-CN
// only is show dual language, we will treat zh-CN and zh-TW as same language
if(ctx && ctx.twpConfig && ctx.twpConfig.get("isShowDualLanguage")==='yes'){
if(finalLang.startsWith("zh-") && currentTargetLang.startsWith('zh-')){
return true;
}else{
return false
}
}

return false
}
4 changes: 2 additions & 2 deletions src/lib/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5254,9 +5254,9 @@ const twpLang = (function () {
twpLang.fixTLanguageCode = function (langCode) {
if (typeof langCode !== "string") return;

if (langCode === "zh") {
if (langCode === "zh" || langCode==='zh-Hans') {
return "zh-CN";
} else if (langCode === "zh-Hant") {
} else if (langCode === "zh-Hant" || langCode === "zh-HK") {
return "zh-TW";
} else if (langCode === "iw") {
return "he";
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.21",
"version": "0.0.22",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"browser_specific_settings": {
Expand Down

0 comments on commit 7407d76

Please sign in to comment.