From b593fa4146c223b9c50dfe009eef245b1e42a5cf Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Sat, 2 Sep 2023 16:57:09 +0800 Subject: [PATCH] add deepl api --- src/apis/index.js | 35 +++++++++++++++++++++++++++++++++++ src/config/i18n.js | 8 ++++++++ src/config/index.js | 10 ++++++++++ src/libs/fetch.js | 7 +++++-- src/views/Options/Setting.js | 18 ++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/apis/index.js b/src/apis/index.js index 10fc1ef..81d36f3 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -3,6 +3,7 @@ import { fetchPolyfill } from "../libs/fetch"; import { OPT_TRANS_GOOGLE, OPT_TRANS_MICROSOFT, + OPT_TRANS_DEEPL, OPT_TRANS_OPENAI, URL_MICROSOFT_TRANS, OPT_LANGS_SPECIAL, @@ -95,6 +96,36 @@ const apiMicrosoftTranslate = (translator, text, to, from) => { }); }; +/** + * DeepL翻译 + * @param {*} text + * @param {*} to + * @param {*} from + * @returns + */ +const apiDeepLTranslate = (translator, text, to, from, setting) => { + const { deeplUrl, deeplKey } = setting; + const data = { + text: [text], + target_lang: to, + split_sentences: "0", + }; + if (from) { + data.source_lang = from; + } + return fetchPolyfill(deeplUrl, { + headers: { + "Content-type": "application/json", + }, + method: "POST", + body: JSON.stringify(data), + useCache: true, + usePool: true, + translator, + token: deeplKey, + }); +}; + /** * OpenAI 翻译 * @param {*} text @@ -160,6 +191,10 @@ export const apiTranslate = async ({ const res = await apiMicrosoftTranslate(translator, q, to, from); trText = res[0].translations[0].text; isSame = to === res[0].detectedLanguage.language; + } else if (translator === OPT_TRANS_DEEPL) { + const res = await apiDeepLTranslate(translator, q, to, from, setting); + trText = res.translations.map((item) => item.text).join(" "); + isSame = to === res.translations[0].detected_source_language; } else if (translator === OPT_TRANS_OPENAI) { const res = await apiOpenaiTranslate(translator, q, to, from, setting); trText = res?.choices?.[0].message.content; diff --git a/src/config/i18n.js b/src/config/i18n.js index 095d125..8eca27f 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -260,6 +260,14 @@ export const I18N = { zh: `请检查url地址是否正确或稍后再试。`, en: `Please check if the url address is correct or try again later.`, }, + deepl_api: { + zh: `DeepL 接口`, + en: `DeepL API`, + }, + deepl_key: { + zh: `DeepL 密钥`, + en: `DeepL Key`, + }, openai_api: { zh: `OpenAI 接口`, en: `OpenAI API`, diff --git a/src/config/index.js b/src/config/index.js index fc2c4ac..b3133bc 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -66,10 +66,12 @@ export const URL_MICROSOFT_TRANS = export const OPT_TRANS_GOOGLE = "Google"; export const OPT_TRANS_MICROSOFT = "Microsoft"; +export const OPT_TRANS_DEEPL = "DeepL"; export const OPT_TRANS_OPENAI = "OpenAI"; export const OPT_TRANS_ALL = [ OPT_TRANS_GOOGLE, OPT_TRANS_MICROSOFT, + OPT_TRANS_DEEPL, OPT_TRANS_OPENAI, ]; @@ -119,6 +121,12 @@ export const OPT_LANGS_SPECIAL = { ["zh-CN", "zh-Hans"], ["zh-TW", "zh-Hant"], ]), + [OPT_TRANS_DEEPL]: new Map([ + ...OPT_LANGS_FROM.map(([key]) => [key, key.toUpperCase()]), + ["auto", ""], + ["zh-CN", "ZH"], + ["zh-TW", "ZH"], + ]), [OPT_TRANS_OPENAI]: new Map( OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]]) ), @@ -200,6 +208,8 @@ export const DEFAULT_SETTING = { subrulesList: DEFAULT_SUBRULES_LIST, // 订阅列表 owSubrule: DEFAULT_OW_RULE, // 覆写订阅规则 googleUrl: "https://translate.googleapis.com/translate_a/single", // 谷歌翻译接口 + deeplUrl: "https://api-free.deepl.com/v2/translate", + deeplKey: "", openaiUrl: "https://api.openai.com/v1/chat/completions", openaiKey: "", openaiModel: "gpt-4", diff --git a/src/libs/fetch.js b/src/libs/fetch.js index 5f15f84..325864f 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -7,6 +7,7 @@ import { MSG_FETCH_CLEAR, CACHE_NAME, OPT_TRANS_MICROSOFT, + OPT_TRANS_DEEPL, OPT_TRANS_OPENAI, DEFAULT_FETCH_INTERVAL, DEFAULT_FETCH_LIMIT, @@ -67,9 +68,11 @@ const newCacheReq = async (request) => { */ const fetchApi = async ({ input, init = {}, translator, token }) => { if (translator === OPT_TRANS_MICROSOFT) { - init.headers["Authorization"] = `Bearer ${token}`; + init.headers["Authorization"] = `Bearer ${token}`; // Microsoft + } else if (translator === OPT_TRANS_DEEPL) { + init.headers["Authorization"] = `DeepL-Auth-Key ${token}`; // DeepL } else if (translator === OPT_TRANS_OPENAI) { - init.headers["Authorization"] = `Bearer ${token}`; // // OpenAI + init.headers["Authorization"] = `Bearer ${token}`; // OpenAI init.headers["api-key"] = token; // Azure OpenAI } diff --git a/src/views/Options/Setting.js b/src/views/Options/Setting.js index 96376c1..033e9a7 100644 --- a/src/views/Options/Setting.js +++ b/src/views/Options/Setting.js @@ -49,6 +49,8 @@ export default function Settings() { minLength, maxLength, openaiUrl, + deeplUrl = "", + deeplKey = "", openaiKey, openaiModel, openaiPrompt, @@ -144,6 +146,22 @@ export default function Settings() { } /> + + + +