Skip to content

Commit

Permalink
修复中文翻译无效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkPHP authored and ThinkPHP committed Oct 5, 2021
1 parent 48eb105 commit 7e26a81
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bobplugin-google-translate",
"version": "1.0.1",
"version": "1.0.2",
"description": "Google 翻译插件,无需申请 API 秘钥",
"homepage": "https://github.com/tingv/bobplugin-google-translate",
"repository": "https://github.com/tingv/bobplugin-google-translate.git",
Expand Down
2 changes: 1 addition & 1 deletion src/info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"identifier": "com.tingv.bobplugin.googletranslate",
"category": "translate",
"version": "1.0.1",
"version": "1.0.2",
"name": "Google 翻译",
"summary": "Google 翻译插件,无需申请 API 秘钥",
"author": "TingV",
Expand Down
19 changes: 13 additions & 6 deletions src/translate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import querystring from 'querystring';
import * as Bob from '@bob-plug/core';
import { userAgent } from './util';
import { standardToNoStandard } from './lang';

var CryptoJS = require("crypto-js");

interface QueryOption {
to?: Bob.Language;
Expand All @@ -20,7 +23,11 @@ var resultCache = new Bob.CacheResult('translate-result');
*/
async function _translate(text: string, options: QueryOption = {}): Promise<Bob.TranslateResult> {
const { from = 'auto', to = 'auto', cache = 'disable', tld = 'com', timeout = 10000 } = options;
const cacheKey = `${text}${from}${to}`;

const sourceLanguage = standardToNoStandard(from);
const targetLanguage = standardToNoStandard(to);

const cacheKey = CryptoJS.MD5(`${text}${from}${to}`);
if (cache === 'enable') {
const _cacheData = resultCache.get(cacheKey);
if (_cacheData) return _cacheData;
Expand All @@ -35,9 +42,9 @@ async function _translate(text: string, options: QueryOption = {}): Promise<Bob.

// 查询参数
const data = {
sl: from,
tl: to,
hl: to,
sl: sourceLanguage,
tl: targetLanguage,
hl: targetLanguage,
q: text,
};

Expand All @@ -58,12 +65,12 @@ async function _translate(text: string, options: QueryOption = {}): Promise<Bob.
if (!Bob.util.isString(html)) throw Bob.util.error('api', '接口返回数据类型出错', res);
if (html.indexOf('"result-container"') == -1 ) throw Bob.util.error('api', '接口返回数据不存在', res);

const matchResults = html.match(/"result-container">([\s\S]*)<\/div><div class="links-container"/) // 提取结果
const matchResults = html.match(/"result-container">([\s\S]*)<\/div><div class="links-container"/); // 提取结果

if (!matchResults) throw Bob.util.error('api', '接口返回数据出错', res);
if(typeof matchResults[1] === "undefined") throw Bob.util.error('api', '接口返回数据异常', res);

const translateResult = matchResults[1].split("\n") // 最终结果
const translateResult = matchResults[1].split("\n"); // 最终结果

result.toParagraphs = translateResult;
result.fromParagraphs = [];
Expand Down

0 comments on commit 7e26a81

Please sign in to comment.