Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not sort the google translate results #167

Merged
merged 1 commit into from Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
})
}
}