From eaa47af26983afc19a105069338bd7e70ce71f89 Mon Sep 17 00:00:00 2001 From: Gabe Date: Thu, 26 Jun 2025 11:13:51 +0800 Subject: [PATCH] fix: revert old google translate api --- src/apis/index.js | 15 +++------------ src/apis/trans.js | 28 +++++++++++++++++++--------- src/config/index.js | 6 ++++-- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/apis/index.js b/src/apis/index.js index ce025d9..cb7416b 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -256,19 +256,10 @@ export const apiTranslate = async ({ ); switch (translator) { - case OPT_TRANS_GOOGLE: { - if (!res || !Array.isArray(res) || res.length < 2) { - console.error("Unexpected response structure:", res); - trText = "Error: Invalid response structure"; - isSame = false; - } else { - const translatedText = Array.isArray(res[0]) ? res[0].join(" ") : "Translation unavailable"; - const isTranslationComplete = to === (Array.isArray(res[1]) ? res[1][0] : undefined); - trText = translatedText; - isSame = isTranslationComplete; - } + case OPT_TRANS_GOOGLE: + trText = res.sentences.map((item) => item.trans).join(" "); + isSame = to === res.src; break; - } case OPT_TRANS_MICROSOFT: trText = res .map((item) => item.translations.map((item) => item.text).join(" ")) diff --git a/src/apis/trans.js b/src/apis/trans.js index 3109252..1bc9037 100644 --- a/src/apis/trans.js +++ b/src/apis/trans.js @@ -58,16 +58,26 @@ const keyPick = (translator, key = "", cacheMap) => { }; const genGoogle = ({ text, from, to, url, key }) => { - const body = JSON.stringify([[ [text], from, to ], "wt_lib"]); - const init = { - method: "POST", - headers: { - "Content-Type": "application/json+protobuf", - "X-Goog-API-Key": key, - }, - body, + const params = { + client: "gtx", + dt: "t", + dj: 1, + ie: "UTF-8", + sl: from, + tl: to, + q: text, }; - return [url, init]; + const input = `${url}?${queryString.stringify(params)}`; + const init = { + headers: { + "Content-type": "application/json", + }, + }; + if (key) { + init.headers.Authorization = `Bearer ${key}`; + } + + return [input, init]; }; const genMicrosoft = async ({ text, from, to }) => { diff --git a/src/config/index.js b/src/config/index.js index 19281d4..26e4ec1 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -87,7 +87,8 @@ export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth"; export const URL_MICROSOFT_LANGDETECT = "https://api-edge.cognitive.microsofttranslator.com/detect?api-version=3.0"; -export const URL_GOOGLE_TRAN = "https://translate-pa.googleapis.com/v1/translateHtml"; +export const URL_GOOGLE_TRAN = + "https://translate.googleapis.com/translate_a/single"; export const DEFAULT_GOOGLE_API_KEY = "AIzaSyATBXajvzQLTDHEQbcpq0Ihe0vWDHmO520"; @@ -551,7 +552,8 @@ const defaultOllamaApi = { export const DEFAULT_TRANS_APIS = { [OPT_TRANS_GOOGLE]: { url: URL_GOOGLE_TRAN, - key: DEFAULT_GOOGLE_API_KEY, + key: "", + // key: DEFAULT_GOOGLE_API_KEY, fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量 fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间 },