From d3cb45ffd6ca21aa537e71efdec21f9d78eac155 Mon Sep 17 00:00:00 2001 From: plainheart Date: Fri, 12 Feb 2021 13:48:17 +0800 Subject: [PATCH] fix: always determine if the issue is in English according to the detected language by Google Translate. --- index.js | 23 ++++++++++++++++++----- src/translator.js | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index d0196a4..e8b98c7 100644 --- a/index.js +++ b/index.js @@ -211,22 +211,35 @@ async function commentIssue(context, commentText, needTranslate) { const filteredTitle = removeCodeAndComment(title); const filteredBody = removeCodeAndComment(body); - const isEnTitle = translator.detectEnglish(filteredTitle); - const isEnBody = translator.detectEnglish(filteredBody); + let isEnTitle = translator.detectEnglish(filteredTitle); + let isEnBody = translator.detectEnglish(filteredBody); let translatedTitle; let translatedBody; + // if the franc has detected it's English, so no need to translate it. if (!isEnTitle) { const res = await translator.translate(title); - translatedTitle = res && res.translated; + if (res) { + // determine if it's English according to the detected language by Google Translate + isEnTitle = res.lang === 'en'; + translatedTitle = !isEnTitle && res.translated; + } } if (!isEnBody) { const res = await translator.translate(body); - translatedBody = res && res.translated; + if (res) { + isEnBody = res.lang === 'en'; + translatedBody = !isEnBody && res.translated; + } } - if ((!isEnTitle || !isEnBody) && (translatedTitle || translatedBody)) { + if ((!isEnTitle || !isEnBody) + && ( + (translatedTitle && translatedTitle !== title) + || (translatedBody && translatedBody !== body) + ) + ) { const translateTip = replaceAll( text.ISSUE_COMMENT_TRANSLATE_TIP, 'AT_ISSUE_AUTHOR', diff --git a/src/translator.js b/src/translator.js index 75f89fa..422d8ef 100644 --- a/src/translator.js +++ b/src/translator.js @@ -41,14 +41,30 @@ function detectLanguage(text, detectAll) { return detectAll ? franc.all(text) : franc(text); } +/** + * To detect English by franc + * + * FIXME: Not accurate enough + */ function detectEnglish(text) { const lang = detectLanguage(text, true); return lang[0][0] === 'eng' && (!lang[1] || (lang[1][0] === 'sco' && lang[1][1] > 0.9) || lang[1][1] < 0.9); } +/** + * To detect English by Google Translate + * + * FIXME: Accurate enough but it requires network requests. + */ +async function detectEnglishByGoogle(text) { + const res = await translate(text); + return res && res.lang === 'en'; +} + module.exports = { translate, detectLanguage, - detectEnglish + detectEnglish, + detectEnglishByGoogle }