From 86b882ba81320fb5ab7f6b29a2cded57a69058f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=8C?= <1907058805@qq.com> Date: Sat, 11 Mar 2023 13:16:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B0=B7=E6=AD=8C=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=EF=BC=88=E5=85=8D=E8=B4=B9=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=B8=8D=E8=83=BD=E5=A4=AA=E9=A2=91=E7=B9=81?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E4=B8=AD=E8=8B=B1=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interfaces/google.js | 46 ++++++++++++++++++++++++++++++++++++++++ src/interfaces/index.js | 4 +++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/interfaces/google.js diff --git a/src/interfaces/google.js b/src/interfaces/google.js new file mode 100644 index 0000000000..13bde00d9d --- /dev/null +++ b/src/interfaces/google.js @@ -0,0 +1,46 @@ +import { fetch } from '@tauri-apps/api/http'; +import { get } from '../global/config'; + +// 此接口只支持英汉互译 +export const info = { + name: "谷歌翻译(免费)", + supportLanguage: { + "zh-cn": "zh_CN", + "en": "en" + }, + needs: { + "google_proxy": "谷歌翻译镜像地址(http(s)://translate.xxx.xxx)" + } +} + +export async function translate(text, from, to) { + const { supportLanguage } = info; + if (!(from in supportLanguage) || !(to in supportLanguage)) { + return '该接口只支持中英互译' + } + + const domain = get('google_proxy', "https://translate.google.com"); + + let res = await fetch(`${domain}/translate_a/single`, { + method: 'GET', + timeout: 5, + query: { + "client": "at", + "sl": from, + "tl": to, + "dt":"t", + "q":text + } + }); + if (res.status == 200) { + let result = "" + for (let r of res.data[0]) { + result = result + r[0] + } + return result + } + else { + return "请求过于频繁,请求失败!" + } + +} diff --git a/src/interfaces/index.js b/src/interfaces/index.js index 12682a277d..218d2e0e0d 100644 --- a/src/interfaces/index.js +++ b/src/interfaces/index.js @@ -7,6 +7,7 @@ import * as _openai_summary from './openai_summary' import * as _openai_code from './openai_code' import * as _tencent from './tencent' import * as _volcengine from './volcengine' +import * as _google from './google' export const baidu = _baidu export const caiyun = _caiyun @@ -16,4 +17,5 @@ export const openai_polish = _openai_polish export const openai_summary = _openai_summary export const openai_code = _openai_code export const tencent = _tencent -export const volcengine = _volcengine \ No newline at end of file +export const volcengine = _volcengine +export const google = _google \ No newline at end of file