Skip to content
Permalink
Browse files
fix: always determine if the issue is in English according to the det…
…ected language by Google Translate.
  • Loading branch information
plainheart committed Feb 12, 2021
1 parent 687fa7c commit d3cb45ffd6ca21aa537e71efdec21f9d78eac155
Showing 2 changed files with 35 additions and 6 deletions.
@@ -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',
@@ -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
}

0 comments on commit d3cb45f

Please sign in to comment.