-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
39 lines (32 loc) · 1.33 KB
/
router.js
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
// router.js
import { getLanguageCode } from "./tool/iso.js";
import { deeplTranslate } from './lib/deepl.js';
import { googleTranslate } from "./lib/google.js";
import { microsoftTranslate } from "./lib/microsoft.js";
import { transmartTranslate } from "./lib/transmart.js";
import { youdaoTranslate } from "./lib/youdao.js";
import { geminiTranslate } from "./lib/gemini.js";
const translationFunctions = {
deepl: deeplTranslate,
google: googleTranslate,
microsoft: microsoftTranslate,
transmart: transmartTranslate,
youdao: youdaoTranslate,
gemini: geminiTranslate
};
async function Translate(model, text, form, to) {
const translateFunction = translationFunctions[model.toLowerCase()];
if (!translateFunction) {
throw new Error(`未找到名为 "${model}" 的翻译服务`);
}
const sourceLang = await getLanguageCode(model, form);
const targetLang = await getLanguageCode(model, to);
if (!sourceLang) {
return JSON.stringify({ "model": model, "error": `翻译失败,未找到源语言代码 ${form} 对应的标准代码` });
}
if (!targetLang) {
return JSON.stringify({ "model": model, "error": `翻译失败,未找到目标语言代码 ${to} 对应的标准代码` });
}
return await translateFunction(text, sourceLang, targetLang);
}
export { Translate };