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);