diff --git a/bobplugin/dependOnService/src/info.json b/bobplugin/dependOnService/src/info.json index ebeb7bc..0bf889e 100644 --- a/bobplugin/dependOnService/src/info.json +++ b/bobplugin/dependOnService/src/info.json @@ -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": "有道翻译免费免秘钥插件", diff --git a/bobplugin/dependOnService/src/main.js b/bobplugin/dependOnService/src/main.js index 8147fb9..5d0ba30 100644 --- a/bobplugin/dependOnService/src/main.js +++ b/bobplugin/dependOnService/src/main.js @@ -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') @@ -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) }) } @@ -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) } } diff --git a/bobplugin/dependOnService/src/utils.js b/bobplugin/dependOnService/src/utils.js index f0e7e4f..2fb833e 100644 --- a/bobplugin/dependOnService/src/utils.js +++ b/bobplugin/dependOnService/src/utils.js @@ -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;