-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaiduTranslate.ts
49 lines (44 loc) · 1.44 KB
/
baiduTranslate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import cloud from '@lafjs/cloud'
import crypto from 'crypto';
function containsChinese(text) {
return /[\u4e00-\u9fa5]/.test(text);
}
async function translateToEnglish(prompt) {
if (!containsChinese(prompt)) {
return prompt;
}
const appid = process.env.BAIDU_APPID;
const appSecret = process.env.BAIDU_SECRET;
const salt = Math.random().toString().slice(2, 7);
const sign = crypto
.createHash('md5')
.update(appid + prompt + salt + appSecret)
.digest('hex');
const url = `https://fanyi-api.baidu.com/api/trans/vip/translate?from=zh&to=en&appid=${appid}&salt=${salt}&q=${encodeURIComponent(prompt)}&sign=${sign}`;
try {
const response = await fetch(url);
if (response.ok) {
const result = await response.json();
if (result.error_code) {
throw new Error(result.error_code + ' - ' + result.error_msg);
}
return result.trans_result[0].dst;
} else {
throw new Error(response.status + ' - ' + response.statusText);
}
} catch (error) {
console.warn('调用百度翻译失败:', error.message);
return prompt;
}
}
export default async function (ctx: FunctionContext) {
let { prompt } = ctx.body;
if (!prompt) {
return {};
}
if (!containsChinese(prompt)) {
return prompt;
}
const data = await translateToEnglish(prompt)
return { data }
}