From 7e26a81d0be85e404733a0dfa35837e83376cb49 Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Wed, 6 Oct 2021 04:13:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=AD=E6=96=87=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E6=97=A0=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/info.json | 2 +- src/translate.ts | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2a65036..2b2f9de 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/info.json b/src/info.json index 1c3cda2..ef38e42 100644 --- a/src/info.json +++ b/src/info.json @@ -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", diff --git a/src/translate.ts b/src/translate.ts index 49a3ce2..c956d8e 100644 --- a/src/translate.ts +++ b/src/translate.ts @@ -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; @@ -20,7 +23,11 @@ var resultCache = new Bob.CacheResult('translate-result'); */ async function _translate(text: string, options: QueryOption = {}): Promise { 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; @@ -35,9 +42,9 @@ async function _translate(text: string, options: QueryOption = {}): Promise([\s\S]*)<\/div>