From d19c151669ad82390485146d24228b514b979b39 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 10 Sep 2023 18:22:39 +0300 Subject: [PATCH] Add DeepLX translation provider #1111 --- public/scripts/extensions/translate/index.js | 41 +++++++++++-- src/secrets.js | 1 + src/translate.js | 63 +++++++++++++++++++- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/public/scripts/extensions/translate/index.js b/public/scripts/extensions/translate/index.js index 37bf9e761..dd20a7b29 100644 --- a/public/scripts/extensions/translate/index.js +++ b/public/scripts/extensions/translate/index.js @@ -135,8 +135,8 @@ const languageCodes = { 'Zulu': 'zu', }; -const KEY_REQUIRED = ['deepl','libre']; -const LOCAL_URL = ['libre', 'oneringtranslator']; +const KEY_REQUIRED = ['deepl', 'libre']; +const LOCAL_URL = ['libre', 'oneringtranslator', 'deeplx']; function showKeysButton() { const providerRequiresKey = KEY_REQUIRED.includes(extension_settings.translate.provider); @@ -271,6 +271,27 @@ async function translateProviderDeepl(text, lang) { throw new Error(response.statusText); } +/** + * Translates text using the DeepLX API + * @param {string} text Text to translate + * @param {string} lang Target language code + * @returns {Promise} Translated text + */ +async function translateProviderDeepLX(text, lang) { + const response = await fetch('/api/translate/deeplx', { + method: 'POST', + headers: getRequestHeaders(), + body: JSON.stringify({ text: text, lang: lang }), + }); + + if (response.ok) { + const result = await response.text(); + return result; + } + + throw new Error(response.statusText); +} + /** * Translates text using the selected translation provider * @param {string} text Text to translate @@ -290,6 +311,8 @@ async function translate(text, lang) { return await translateProviderGoogle(text, lang); case 'deepl': return await translateProviderDeepl(text, lang); + case 'deeplx': + return await translateProviderDeepLX(text, lang); case 'oneringtranslator': return await translateProviderOneRing(text, lang); default: @@ -437,6 +460,7 @@ jQuery(() => { +