forked from akai/readest
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 <chrox.huang@gmail.com>
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user