Skip to content

Commit

Permalink
fix: 中文译文中的英文标点符号问题 #3中文译文中的括号总是英文的,复制过来后需要频繁改动,非常不方便。
Browse files Browse the repository at this point in the history
       希望能调整成中文。
  • Loading branch information
akl7777777 committed Feb 20, 2024
1 parent e2e5846 commit 6b38d89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bobplugin/dependOnService/src/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"identifier": "com.akl.bob-plugin-akl-youdao-free-translate",
"version": "0.2.0",
"version": "0.2.1",
"category": "translate",
"name": "Free 有道翻译",
"summary": "有道翻译免费免秘钥插件",
Expand Down
12 changes: 12 additions & 0 deletions bobplugin/dependOnService/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ function translate(query, completion) {
continue
}
translations.forEach(function (e) {
// 如果目标是中文,则需要将英文标点替换为中文标点
if (target_lang === 'zh-CHS') {
e = utils.replacePunctuation(e)
}
concatRs.push(e)
})
// concatRs.push('\n')
Expand Down Expand Up @@ -273,6 +277,10 @@ function translate(query, completion) {
return;
}
translations.forEach(function (e) {
// 如果目标是中文,则需要将英文标点替换为中文标点
if (target_lang === 'zh-CHS') {
e = utils.replacePunctuation(e)
}
concatRs.push(e)
})
}
Expand Down Expand Up @@ -318,6 +326,10 @@ function translate(query, completion) {
for (let j = 0; j < resp.data.translateResult[i].length; j++) {
rsParagraph += resp.data.translateResult[i][j].tgt
}
// 如果目标是中文,则需要将英文标点替换为中文标点
if (target_lang === 'zh-CHS') {
rsParagraph = utils.replacePunctuation(rsParagraph)
}
rs.push(rsParagraph)
}
}
Expand Down
15 changes: 15 additions & 0 deletions bobplugin/dependOnService/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,20 @@ var config = require('./config.js');
const langMap = new Map(config.supportedLanguages);
const langMapReverse = new Map(config.supportedLanguages.map(([standardLang, lang]) => [lang, standardLang]));

// 将英文标点替换为中文标点
function replacePunctuation(str) {
return str.replace(/(\w)'/g, '$1’').replace(/'/g, '’')
.replace(/(\w)"/g, '$1”').replace(/"/g, '”')
.replace(/(\w)'/g, '$1‘').replace(/'/g, '‘')
.replace(/(\w)'/g, '$1’').replace(/'/g, '’')
.replace(/(\w)!/g, '$1!').replace(/!/g, '!')
.replace(/(\w)\?/g, '$1?').replace(/\?/g, '?')
.replace(/(\w),/g, '$1,').replace(/,/g, ',')
.replace(/(\w)\./g, '$1。').replace(/\./g, '。')
.replace(/(\w);/g, '$1;').replace(/;/g, ';')
.replace(/(\w):/g, '$1:').replace(/:/g, ':');
}

exports.langMap = langMap;
exports.langMapReverse = langMapReverse;
exports.replacePunctuation = replacePunctuation;

0 comments on commit 6b38d89

Please sign in to comment.