Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat: translate the issue automatically if the language is not English.
- Loading branch information
Showing
9 changed files
with
154 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,3 +1,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
indent_type = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,53 @@ | ||
const googleTranslate = require('@vitalets/google-translate-api'); | ||
const franc = require('franc'); | ||
|
||
/** | ||
* To translate the raw sentence to English | ||
* @param {string} rawContent sentence to be translated | ||
* @return {object} { translated: string, lang: string } | ||
*/ | ||
async function translate(rawContent) { | ||
if (!rawContent || !(rawContent = rawContent.trim())) { | ||
return; | ||
} | ||
try { | ||
const res = await googleTranslate( | ||
rawContent, | ||
{ | ||
to: 'en', | ||
tld: 'cn' | ||
} | ||
); | ||
return { | ||
translated: res.text, | ||
lang: res.from.language.iso | ||
}; | ||
} | ||
catch (e) { | ||
console.error('failed to translate', e); | ||
} | ||
} | ||
|
||
/** | ||
* To detect the languages of specified text | ||
* @param {string} text | ||
* @param {boolean} detectAll whether to return all detected languages | ||
* @return {string | Array<[string, number]>} | ||
*/ | ||
function detectLanguage(text, detectAll) { | ||
if (!text || !(text = text.trim())) { | ||
return; | ||
} | ||
return detectAll ? franc.all(text) : franc(text); | ||
} | ||
|
||
function detectEnglish(text) { | ||
const lang = detectLanguage(text, true); | ||
return lang[0][0] === 'eng' && (!lang[1] || lang[1][1] < 0.9); | ||
} | ||
|
||
module.exports = { | ||
translate, | ||
detectLanguage, | ||
detectEnglish | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,14 @@ | ||
function removeCodeAndComment(body) { | ||
return body | ||
.replace(/<!--[\w\W\r\n]*?-->/gmi, '') | ||
.replace(/`{3}(.|\n)*`{3}/gmi, ''); | ||
} | ||
|
||
function replaceAll(str, search, replacement) { | ||
return str.replace(new RegExp(search, 'g'), replacement); | ||
} | ||
|
||
module.exports = { | ||
removeCodeAndComment, | ||
replaceAll | ||
} |