diff --git a/src/apis/trans.js b/src/apis/trans.js index 0ade766..6bed895 100644 --- a/src/apis/trans.js +++ b/src/apis/trans.js @@ -3,6 +3,7 @@ import { OPT_TRANS_GOOGLE, OPT_TRANS_GOOGLE_2, OPT_TRANS_MICROSOFT, + OPT_TRANS_AZUREAI, OPT_TRANS_DEEPL, OPT_TRANS_DEEPLFREE, OPT_TRANS_DEEPLX, @@ -179,6 +180,22 @@ const genMicrosoft = ({ texts, from, to, token }) => { return { url, body, headers }; }; +const genAzureAI = ({ texts, from, to, url, key, region }) => { + const params = queryString.stringify({ + from, + to, + }); + url = url.endsWith("&") ? `${url}${params}` : `${url}&${params}`; + const headers = { + "Content-type": "application/json", + "Ocp-Apim-Subscription-Key": key, + "Ocp-Apim-Subscription-Region": region, + }; + const body = texts.map((text) => ({ Text: text })); + + return { url, body, headers }; +}; + const genDeepl = ({ texts, from, to, url, key }) => { const body = { text: texts, @@ -546,6 +563,7 @@ const genReqFuncs = { [OPT_TRANS_GOOGLE]: genGoogle, [OPT_TRANS_GOOGLE_2]: genGoogle2, [OPT_TRANS_MICROSOFT]: genMicrosoft, + [OPT_TRANS_AZUREAI]: genAzureAI, [OPT_TRANS_DEEPL]: genDeepl, [OPT_TRANS_DEEPLFREE]: genDeeplFree, [OPT_TRANS_DEEPLX]: genDeeplX, @@ -733,6 +751,7 @@ export const parseTransRes = async ( case OPT_TRANS_GOOGLE_2: return res?.[0]?.map((_, i) => [res?.[0]?.[i], res?.[1]?.[i]]); case OPT_TRANS_MICROSOFT: + case OPT_TRANS_AZUREAI: return res?.map((item) => [ item.translations.map((item) => item.text).join(" "), item.detectedLanguage?.language, diff --git a/src/config/api.js b/src/config/api.js index 726af32..79090f0 100644 --- a/src/config/api.js +++ b/src/config/api.js @@ -28,6 +28,7 @@ export const OPT_TRANS_BUILTINAI = "BuiltinAI"; export const OPT_TRANS_GOOGLE = "Google"; export const OPT_TRANS_GOOGLE_2 = "Google2"; export const OPT_TRANS_MICROSOFT = "Microsoft"; +export const OPT_TRANS_AZUREAI = "AzureAI"; export const OPT_TRANS_DEEPL = "DeepL"; export const OPT_TRANS_DEEPLX = "DeepLX"; export const OPT_TRANS_DEEPLFREE = "DeepLFree"; @@ -50,6 +51,7 @@ export const OPT_ALL_TYPES = [ OPT_TRANS_GOOGLE, OPT_TRANS_GOOGLE_2, OPT_TRANS_MICROSOFT, + OPT_TRANS_AZUREAI, // OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, OPT_TRANS_VOLCENGINE, @@ -100,6 +102,7 @@ export const API_SPE_TYPES = { ]), // 支持多key mulkeys: new Set([ + OPT_TRANS_AZUREAI, OPT_TRANS_DEEPL, OPT_TRANS_OPENAI, OPT_TRANS_GEMINI, @@ -113,6 +116,7 @@ export const API_SPE_TYPES = { ]), // 支持批处理 batch: new Set([ + OPT_TRANS_AZUREAI, OPT_TRANS_GOOGLE_2, OPT_TRANS_MICROSOFT, OPT_TRANS_TENCENT, @@ -222,6 +226,12 @@ export const OPT_LANGS_TO_SPEC = { ["zh-CN", "zh-Hans"], ["zh-TW", "zh-Hant"], ]), + [OPT_TRANS_AZUREAI]: new Map([ + ...OPT_LANGS_SPEC_DEFAULT, + ["auto", ""], + ["zh-CN", "zh-Hans"], + ["zh-TW", "zh-Hant"], + ]), [OPT_TRANS_DEEPL]: new Map([ ...OPT_LANGS_SPEC_DEFAULT_UC, ["auto", ""], @@ -442,7 +452,8 @@ const defaultApi = { maxTokens: 20480, think: false, thinkIgnore: "qwen3,deepseek-r1", - isDisabled: false, // 是否不显示 + isDisabled: false, // 是否不显示, + region: "", // Azure 专用 }; const defaultApiOpts = { @@ -461,6 +472,11 @@ const defaultApiOpts = { ...defaultApi, useBatchFetch: true, }, + [OPT_TRANS_AZUREAI]: { + ...defaultApi, + url: "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0", + useBatchFetch: true, + }, [OPT_TRANS_BAIDU]: { ...defaultApi, }, diff --git a/src/views/Options/Apis.js b/src/views/Options/Apis.js index 96ed66e..f5b27f5 100644 --- a/src/views/Options/Apis.js +++ b/src/views/Options/Apis.js @@ -43,6 +43,7 @@ import { BUILTIN_STONES, BUILTIN_PLACEHOLDERS, BUILTIN_PLACETAGS, + OPT_TRANS_AZUREAI, } from "../../config"; function TestButton({ api }) { @@ -237,6 +238,7 @@ function ApiFields({ apiSlug, isUserApi, deleteApi }) { tone = "neutral", placeholder = BUILTIN_PLACEHOLDERS[0], placetag = BUILTIN_PLACETAGS[0], + region = "", // aiTerms = false, } = formData; @@ -283,6 +285,16 @@ function ApiFields({ apiSlug, isUserApi, deleteApi }) { )} + {apiType === OPT_TRANS_AZUREAI && ( + + )} + {API_SPE_TYPES.ai.has(apiType) && ( <>