From e949476d2786b021188887fe2d8147d95d3b4d2e Mon Sep 17 00:00:00 2001 From: bfcs <52602045+bfcs@users.noreply.github.com> Date: Sun, 1 Mar 2026 13:03:52 +0800 Subject: [PATCH] fix(translation): resolve DeepL translation failure with auto-detection (#3412) * fix(translation): correctly handle DeepL source_lang 'AUTO' by omitting it * refactor: backward compatibility --------- Co-authored-by: Huang Xin --- apps/readest-app/src/pages/api/deepl/translate.ts | 11 ++++++++++- .../src/services/translators/providers/deepl.ts | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/readest-app/src/pages/api/deepl/translate.ts b/apps/readest-app/src/pages/api/deepl/translate.ts index 45a726f7..c3a39ed4 100644 --- a/apps/readest-app/src/pages/api/deepl/translate.ts +++ b/apps/readest-app/src/pages/api/deepl/translate.ts @@ -183,12 +183,21 @@ async function callDeepLAPI( // TODO: this should be processed in the client, but for now, we need to do it here // please remove this when most clients are updated const input = text.replaceAll('\n', '').trim(); - const requestBody = { + + const requestBody: { + text: string | string[]; + target_lang: string; + source_lang?: string; + } = { text: isV2Api ? [input] : input, source_lang: isV2Api ? sourceLang : (LANG_V2_V1_MAP[sourceLang] ?? sourceLang), target_lang: isV2Api ? targetLang : (LANG_V2_V1_MAP[targetLang] ?? targetLang), }; + if (isV2Api && requestBody.source_lang?.toUpperCase() === 'AUTO') { + delete requestBody.source_lang; + } + const response = await fetch(apiUrl, { method: 'POST', headers: { diff --git a/apps/readest-app/src/services/translators/providers/deepl.ts b/apps/readest-app/src/services/translators/providers/deepl.ts index a2710821..3d3ec337 100644 --- a/apps/readest-app/src/services/translators/providers/deepl.ts +++ b/apps/readest-app/src/services/translators/providers/deepl.ts @@ -36,9 +36,10 @@ export const deeplProvider: TranslationProvider = { throw new Error('Authentication token is required for DeepL translation'); } + const normalizedSourceLang = normalizeToShortLang(sourceLang).toUpperCase(); const body = JSON.stringify({ text: text, - source_lang: normalizeToShortLang(sourceLang).toUpperCase(), + ...(normalizedSourceLang !== 'AUTO' ? { source_lang: normalizedSourceLang } : {}), target_lang: normalizeToShortLang(targetLang).toUpperCase(), use_cache: useCache, });