Skip to content

Commit

Permalink
fix: 修复截图翻译换行失效问题, 优化特殊字符分隔
Browse files Browse the repository at this point in the history
  • Loading branch information
roojay520 committed Oct 12, 2020
1 parent 36f534e commit 9d368e5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# bobplugin-google-translate

> 这是一个 [Bob](https://ripperhe.gitee.io/bob/#/) 的一个 google 翻译插件
> 这是 [Bob](https://ripperhe.gitee.io/bob/#/) 的一个 google 翻译插件
## 特性

Expand Down
Binary file modified images/preview-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions src/libs/human-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var humanizeString = (input) => {
if (typeof _input !== 'string') {
throw new TypeError('Expected a string');
}

_input = decamelize(_input);
_input = decamelize(_input, '');
_input = _input
.replace(/[_-]+/g, ' ')
.replace(/\s{2,}/g, ' ')
.replace(/[]+/g, ' ')
.replace(/([_-])+/g, ' $1 ')
// https://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines#answer-3469155:~:text=Use%20a%20double%2Dnegative
.replace(/[^\S\r\n]{2,}/g, ' ')
.trim();
_input = `${_input.charAt(0).toUpperCase()}${_input.slice(1)}`;

Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Bob from './bob';
import _lang from './lang';
import GoogleTranslate from './google-translate';

var humanizeString = require('./libs/human-string');
var formatString = require('./libs/human-string');

export function supportLanguages(): Bob.supportLanguages {
return [..._lang];
Expand All @@ -11,7 +11,7 @@ export function supportLanguages(): Bob.supportLanguages {
// https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate
export function translate(query: Bob.TranslateQuery, completion: Bob.Completion) {
const { text = '', detectFrom, detectTo } = query;
const str = humanizeString(text);
const str = formatString(text);
const params = { from: detectFrom, to: detectTo, tld: Bob.getOption('tld'), cache: Bob.getOption('cache') };
GoogleTranslate._translate(str, params)
.then((result) => completion({ result }))
Expand Down

0 comments on commit 9d368e5

Please sign in to comment.