Skip to content
Permalink
Browse files
fix: issue translator may be not working in some cases.
  • Loading branch information
plainheart committed Feb 9, 2021
1 parent 5f5dbe4 commit 8c47adee57a0234fb2009c6ceb5fc68ac20fa0a0
Showing 3 changed files with 18 additions and 12 deletions.
@@ -195,20 +195,24 @@ function closeIssue(context) {
return closeIssue;
}

async function commentIssue (context, commentText, needTranslate) {
async function commentIssue(context, commentText, needTranslate) {
// create comment
await context.octokit.issues.createComment(context.issue({
body: commentText
}));

logger.info('issue needs translation: ' + needTranslate)

// translate the issue if needed
if (needTranslate) {
let { title, body } = context.payload.issue;
title = removeCodeAndComment(title);
body = removeCodeAndComment(body);
const startTime = Date.now();

const { title, body } = context.payload.issue;
const filteredTitle = removeCodeAndComment(title);
const filteredBody = removeCodeAndComment(body);

const isEnTitle = translator.detectEnglish(title);
const isEnBody = translator.detectEnglish(body);
const isEnTitle = translator.detectEnglish(filteredTitle);
const isEnBody = translator.detectEnglish(filteredBody);

let translatedTitle;
let translatedBody;
@@ -222,18 +226,19 @@ async function commentIssue (context, commentText, needTranslate) {
translatedBody = res && res.translated;
}

if (!isEnBody && translatedBody && body !== translatedBody) {
if (!isEnTitle || !isEnBody) {
const translateTip = replaceAll(
text.ISSUE_COMMENT_TRANSLATE_TIP,
'AT_ISSUE_AUTHOR',
'@' + context.payload.issue.user.login
);
const translateComment = `${translateTip}\n<details><summary><b>TRANSLATED</b></summary><br>${!isEnTitle && translatedTitle && title !== translatedTitle ? '\n\n**TITLE**\n\n' + translatedTitle : ''}\n\n**BODY**\n\n${translatedBody}\n</details>`;
const translateComment = `${translateTip}\n<details><summary><b>TRANSLATED</b></summary><br>${!isEnTitle && translatedTitle && title !== translatedTitle ? '\n\n**TITLE**\n\n' + translatedTitle : ''}${!isEnBody && translatedBody && body !== translatedBody ? '\n\n**BODY**\n\n' + translatedBody : ''}\n</details>`;
await context.octokit.issues.createComment(
context.issue({
body: translateComment
})
);
logger.info(`issue translated - ${Date.now() - startTime}ms`)
}
}
}
@@ -15,7 +15,7 @@ async function translate(rawContent) {
rawContent,
{
to: 'en',
tld: 'cn'
// tld: 'cn'
}
);
return {
@@ -1,8 +1,9 @@
function removeCodeAndComment(body) {
return body
.replace(/<!--[\w\W\r\n]*?-->/gmi, '')
.replace(/`{3}(.|\n)*`{3}/gmi, '')
.replace(/#.*\n/g, '');
.replace(/<!--[\w\W\s]*?-->/gmi, '')
.replace(/`{3}(.|\n)*`{3}/gmi, '')
.replace(/#.*\s?/g, '')
.replace(/-{3}\s?/g, '');
}

function replaceAll(str, search, replacement) {

0 comments on commit 8c47ade

Please sign in to comment.