From 11d3211839a8538e5e59af6b0e742afa9b28f646 Mon Sep 17 00:00:00 2001 From: berbant <33601955+berbant@users.noreply.github.com> Date: Wed, 31 Jan 2024 22:47:14 +0400 Subject: [PATCH] Update translate.js Added new translateProvider: Lingva Translate --- src/endpoints/translate.js | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/endpoints/translate.js b/src/endpoints/translate.js index ac196b648..a5ed3f8f1 100644 --- a/src/endpoints/translate.js +++ b/src/endpoints/translate.js @@ -102,6 +102,46 @@ router.post('/google', jsonParser, async (request, response) => { } }); +router.post('/lingva', jsonParser, async (request, response) => { + try { + const text = request.body.text; + const lang = request.body.lang; + + if (!text || !lang) { + return response.sendStatus(400); + } + + console.log('Input text: ' + text); + + const url = readSecret(SECRET_KEYS.LINGVA_URL) + "/auto/" + lang + "/" + encodeURIComponent(text); + + https.get(url, (resp) => { + let data = ''; + + resp.on('data', (chunk) => { + data += chunk; + }); + + resp.on('end', () => { + try { + const result = JSON.parse(data) + console.log('Translated text: ' + result.translation); + return response.send(result.translation); + } catch (error) { + console.log('Translation error', error); + return response.sendStatus(500); + } + }); + }).on('error', (err) => { + console.log('Translation error: ' + err.message); + return response.sendStatus(500); + }); + } catch (error) { + console.log('Translation error', error); + return response.sendStatus(500); + } +}); + router.post('/deepl', jsonParser, async (request, response) => { const key = readSecret(SECRET_KEYS.DEEPL);