Skip to content

Commit 55a9c34

Browse files
committed
feat:添加百度翻译
1 parent 857ddb7 commit 55a9c34

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

electron/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ export default {
1111
url: isDev ? "http://127.0.0.1:3030" : "https://tool.hellowmonkey.cc",
1212
youdaoAppId: "4942fc478d27c774",
1313
youdaoKey: "qLDdY5g9qUNQN8WV1WBSVOGYjRfug0mq",
14+
baiduAppId: "20221003001368543",
15+
baiduKey: "tVV9MC_Jdtse2oO3CAEU",
1416
scheme: "tool-box",
1517
};

electron/main/developer.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function truncate(q: string) {
88
return q.substring(0, 10) + len + q.substring(len - 10, len);
99
}
1010

11+
// 有道翻译
1112
export interface IYoudao {
1213
errorCode: string;
1314
query: string;
@@ -17,7 +18,6 @@ export interface IYoudao {
1718
};
1819
web?: { key: string; value: string[] }[];
1920
}
20-
2121
export function youdaoTranslate(q = "") {
2222
const appKey = config.youdaoAppId;
2323
const now = Date.now();
@@ -59,3 +59,55 @@ export function youdaoTranslate(q = "") {
5959
}
6060
});
6161
}
62+
63+
// 百度翻译
64+
export interface IBaidu {
65+
from: string;
66+
to: string;
67+
trans_result: {
68+
src: string;
69+
dst: string;
70+
}[];
71+
error_msg?: string;
72+
}
73+
export function baiduTranslate(q = "") {
74+
const appid = config.baiduAppId;
75+
const salt = Date.now();
76+
const from = "zh";
77+
const to = "en";
78+
const sign = createHash("MD5")
79+
.update(appid + q + salt + config.baiduKey)
80+
.digest("hex");
81+
82+
return ajax<IBaidu>("https://api.fanyi.baidu.com/api/trans/vip/translate", {
83+
q,
84+
appid,
85+
salt,
86+
from,
87+
to,
88+
sign,
89+
}).then(data => {
90+
if (data.error_msg) {
91+
return Promise.reject(new Error(data.error_msg));
92+
} else {
93+
return data.trans_result.map(v => v.dst);
94+
}
95+
});
96+
}
97+
98+
// 翻译
99+
export async function translate(words: string) {
100+
const arr: string[] = [];
101+
try {
102+
await Promise.any([
103+
youdaoTranslate(words).then(data => {
104+
arr.push(...data);
105+
}),
106+
baiduTranslate(words).then(data => {
107+
arr.push(...data);
108+
}),
109+
]);
110+
} finally {
111+
return arr;
112+
}
113+
}

electron/main/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, SaveDialogOptions, Tray, Notification, shell, dialog } from "electron";
1+
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, SaveDialogOptions, Tray, Notification, shell } from "electron";
22
import { join, resolve } from "path";
33
import { writeJSONSync, readJSONSync, existsSync, mkdirSync } from "fs-extra";
44
import chokidar from "chokidar";
55
import { compressImage, pngToIco } from "./image";
66
import { openDirectory, saveDialog, saveBase64File, selectDirectory, writeFile } from "./file";
77
import { getFilePath, notification } from "./helper";
8-
import { youdaoTranslate } from "./developer";
8+
import { translate } from "./developer";
99
import config from "../config";
1010
import defaultUserConfig from "../data/config.json";
1111

@@ -295,4 +295,4 @@ ipcMain.handle("get-config", () => userConfig);
295295
ipcMain.handle("open-url", (e, url: string) => shell.openExternal(url));
296296

297297
// 翻译
298-
ipcMain.handle("youdao-translate", (e, words: string) => youdaoTranslate(words));
298+
ipcMain.handle("translate", (e, words: string) => translate(words));

electron/preload/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ contextBridge.exposeInMainWorld("electronAPI", {
2929
// 打开链接
3030
openUrl: (...args: any[]) => ipcRenderer.invoke("open-url", ...args),
3131
// 翻译
32-
youdaoTranslate: (...args: any[]) => ipcRenderer.invoke("youdao-translate", ...args),
32+
translate: (...args: any[]) => ipcRenderer.invoke("translate", ...args),
3333
});

src/env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ declare let electronAPI: {
2525
setConfig: (data: unknown) => Promise<void>;
2626
getConfig: () => Promise<{ keyboard: string; openAtLogin: boolean; compressDirs: string[]; compressNotify: boolean }>;
2727
openUrl: (url: string) => Promise<void>;
28-
youdaoTranslate: (words: string) => Promise<string[]>;
28+
translate: (words: string) => Promise<string[]>;
2929
};
3030

3131
declare module "qrcode-decoder";

src/page/developer/translate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default defineComponent({
2525
data.value.map(v =>
2626
v
2727
.split("")
28-
.map(val => val.toLocaleLowerCase().replace(/[\.\,\?\!\~\/]/g, ""))
28+
.map(val => val.toLocaleLowerCase().replace(/[\.\,\?\!\~\/\-]/g, ""))
2929
.join("")
3030
)
3131
),
@@ -51,7 +51,7 @@ export default defineComponent({
5151
}
5252
loading.value = true;
5353
electronAPI
54-
.youdaoTranslate(form.words)
54+
.translate(form.words)
5555
.then(list => {
5656
data.value = list;
5757
if (!dataList.value.length) {

0 commit comments

Comments
 (0)