Skip to content

Commit

Permalink
fix: do not sort the google translate results
Browse files Browse the repository at this point in the history
fix #163
  • Loading branch information
azu committed May 22, 2021
1 parent f3a4956 commit 747682b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 111 deletions.
22 changes: 6 additions & 16 deletions background/translationService.js
Expand Up @@ -228,7 +228,7 @@ var translationService = {}
http.open("POST", translationServiceURL + tk)
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
http.responseType = "json"
http.send(requests[idx].requestBody)
http.send(requests[idx].requestBody)
} else {
http.open("GET", translationServiceURL + requests[idx].requestBody)
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Expand Down Expand Up @@ -435,33 +435,23 @@ var translationService = {}
}

result = sentences.length > 0 ? sentences.join(" ") : result
// Should not sort the <a i={number}> of Google Translate result
// Instead of it, join the texts without sorting
// https://github.com/FilipePS/Traduzir-paginas-web/issues/163
let resultArray = result.match(/\<a\si\=[0-9]+\>[^\<\>]*(?=\<\/a\>)/g)

let indexes
if (resultArray && resultArray.length > 0) {
indexes = resultArray.map(value => parseInt(value.match(/[0-9]+(?=\>)/g))).filter(value => !isNaN(value))
resultArray = resultArray.map(value => {
var resultStartAtIndex = value.indexOf('>')
return value.slice(resultStartAtIndex + 1)
})
} else {
resultArray = [result]
indexes = [0]
}

resultArray = resultArray.map(value => value.replace(/\<\/b\>/g, ""))
resultArray = resultArray.map(value => unescapeHTML(value))

const finalResulArray = []
for (const j in indexes) {
if (finalResulArray[indexes[j]]) {
finalResulArray[indexes[j]] += " " + resultArray[j]
} else {
finalResulArray[indexes[j]] = resultArray[j]
}
}

resultArray3d.push(finalResulArray)
resultArray3d.push(resultArray)
}

//return fixResultArray(resultArray3d, fixIndexesMap)
Expand Down Expand Up @@ -669,4 +659,4 @@ var translationService = {}
return true
}
})
}
}

0 comments on commit 747682b

Please sign in to comment.