Skip to content

Commit

Permalink
添加谷歌翻译(免费接口,请求不能太频繁,支持中英)
Browse files Browse the repository at this point in the history
  • Loading branch information
binw666 committed Mar 11, 2023
1 parent 0bd2e86 commit 86b882b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/interfaces/google.js
Original file line number Diff line number Diff line change
@@ -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 "请求过于频繁,请求失败!"
}

}
4 changes: 3 additions & 1 deletion src/interfaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
export const volcengine = _volcengine
export const google = _google

0 comments on commit 86b882b

Please sign in to comment.