Skip to content

Commit

Permalink
fix: deepl lang mapping
Browse files Browse the repository at this point in the history
fix: #997
  • Loading branch information
windingwind committed Dec 10, 2024
1 parent fec7b70 commit 4ef8ef4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/modules/services/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ async function deepl(url: string, data: Required<TranslateTask>) {
const [key, glossary_id]: string[] = data.secret.split("#");
let reqBody = `auth_key=${key}&text=${encodeURIComponent(
data.raw,
)}&source_lang=${data.langfrom
.split("-")[0]
.toUpperCase()}&target_lang=${data.langto.split("-")[0].toUpperCase()}`;
)}&source_lang=${mapLang(data.langfrom)}&target_lang=${mapLang(data.langto)}`;
if (glossary_id) {
reqBody += `&glossary_id=${glossary_id}`;
}
Expand All @@ -33,3 +31,20 @@ async function deepl(url: string, data: Required<TranslateTask>) {
}
data.result = xhr.response.translations[0].text;
}

function mapLang(lang: string) {
if (lang in LANG_MAP) {
return LANG_MAP[lang];
}
return lang.split("-")[0].toUpperCase();
}

const LANG_MAP = {
"pt-BR": "PT-BR",
"pt-PT": "PT-PT",
"zh-CN": "ZH-HANS",
"zh-HK": "ZH-HANT",
"zh-MO": "ZH-HANT",
"zh-SG": "ZH-HANS",
"zh-TW": "ZH-HANT",
} as Record<string, string | undefined>;
22 changes: 20 additions & 2 deletions src/modules/services/deeplx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default <TranslateTaskProcessor>async function (data) {
],
splitting: "newlines",
lang: {
source_lang_user_selected: data.langfrom.split("-")[0].toUpperCase(),
target_lang: data.langto.split("-")[0].toUpperCase(),
source_lang_user_selected: mapLang(data.langfrom),
target_lang: mapLang(data.langto),
},
timestamp: ts - (ts % ICounts) + ICounts,
commonJobParams: {
Expand Down Expand Up @@ -58,3 +58,21 @@ export default <TranslateTaskProcessor>async function (data) {
}
data.result = xhr.response.result.texts[0].text;
};

// Inherited from src/modules/services/deepl.ts
function mapLang(lang: string) {
if (lang in LANG_MAP) {
return LANG_MAP[lang];
}
return lang.split("-")[0].toUpperCase();
}

const LANG_MAP = {
"pt-BR": "PT-BR",
"pt-PT": "PT-PT",
"zh-CN": "ZH-HANS",
"zh-HK": "ZH-HANT",
"zh-MO": "ZH-HANT",
"zh-SG": "ZH-HANS",
"zh-TW": "ZH-HANT",
} as Record<string, string | undefined>;

0 comments on commit 4ef8ef4

Please sign in to comment.