Replace Google Translate API package
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | "csrf-csrf": "^2.2.3", |
| 25 | 25 | "express": "^4.21.0", |
| 26 | 26 | "form-data": "^4.0.0", |
| 27 | 27 | "google-translate-api-browserx": "^310.07.1", |
| 28 | 28 | "helmet": "^7.1.0", |
| 29 | 29 | "html-entities": "^2.5.2", |
| 30 | 30 | "iconv-lite": "^0.6.3", |
| @@ -3689,11 +3689,18 @@ | ||
| 3689 | 3689 | "url": "https://github.com/sponsors/sindresorhus" |
| 3690 | 3690 | } |
| 3691 | 3691 | }, |
| 3692 | 3692 | "node_modules/google-translate-api-browserx": { |
| 3693 | 3693 | "version": "310.07.1", |
| 3694 | 3694 | "resolved": "https://registry.npmjs.org/google-translate-api-browserx/-/google-translate-api-browserx-310.07.1.tgz", |
| 3695 | 3695 | "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4lOdZDS6jRWzn1woOk62aOKQ5OyVaJSA+CXXpQeyc6CktOWxo36IWfstOjwG/xaynGVAzeBezzJG6qn8MLeqOq3SmW0AdkvnGl3Z2Sbpmk1A+jc2WwrBiRjqaY2A==", |
| 3696 | 3696 | "license": "MIT", |
| 3697 | + "engines": { | |
| 3698 | + "node": ">=14.0.0" | |
| 3699 | + }, | |
| 3700 | + "funding": { | |
| 3701 | + "type": "github", | |
| 3702 | + "url": "https://github.com/sponsors/AidanWelch" | |
| 3703 | + } | |
| 3697 | 3704 | }, |
| 3698 | 3705 | "node_modules/gopd": { |
| 3699 | 3706 | "version": "1.0.1", |
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | "csrf-csrf": "^2.2.3", |
| 15 | 15 | "express": "^4.21.0", |
| 16 | 16 | "form-data": "^4.0.0", |
| 17 | 17 | "google-translate-api-browserx": "^310.07.1", |
| 18 | 18 | "helmet": "^7.1.0", |
| 19 | 19 | "html-entities": "^2.5.2", |
| 20 | 20 | "iconv-lite": "^0.6.3", |
| @@ -1,14 +1,9 @@ | ||
| 1 | 1 | import https from 'node:https'; |
| 2 | -import { createRequire } from 'node:module'; | |
| 3 | -import { Buffer } from 'node:buffer'; | |
| 4 | 2 | |
| 5 | 3 | import fetch from 'node-fetch'; |
| 6 | 4 | import express from 'express'; |
| 7 | -import iconv from 'iconv-lite'; | |
| 8 | 5 | import bingTranslateApi from 'bing-translate-api'; |
| 9 | - | |
| 6 | +import googleTranslateApi from 'google-translate-api-x'; | |
| 10 | -const require = createRequire(import.meta.url); | |
| 11 | -const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser'); | |
| 12 | 7 | |
| 13 | 8 | import { readSecret, SECRET_KEYS } from './secrets.js'; |
| 14 | 9 | import { getConfigValue, uuidv4 } from '../util.js'; |
| @@ -17,20 +12,6 @@ import { jsonParser } from '../express-common.js'; | ||
| 17 | 12 | const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate'; |
| 18 | 13 | const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate'; |
| 19 | 14 | |
| 20 | -/** | |
| 21 | - * Tries to decode a Node.js Buffer to a string using iconv-lite for UTF-8. | |
| 22 | - * @param {Buffer} buffer Node.js Buffer | |
| 23 | - * @returns {string} Decoded string | |
| 24 | - */ | |
| 25 | -function decodeBuffer(buffer) { | |
| 26 | - try { | |
| 27 | - return iconv.decode(buffer, 'utf-8'); | |
| 28 | - } catch (error) { | |
| 29 | - console.log('Failed to decode buffer:', error); | |
| 30 | - return buffer.toString('utf-8'); | |
| 31 | - } | |
| 32 | -} | |
| 33 | - | |
| 34 | 15 | export const router = express.Router(); |
| 35 | 16 | |
| 36 | 17 | router.post('/libre', jsonParser, async (request, response) => { |
| @@ -100,31 +81,12 @@ router.post('/google', jsonParser, async (request, response) => { | ||
| 100 | 81 | |
| 101 | 82 | console.log('Input text: ' + text); |
| 102 | 83 | |
| 103 | 84 | const urlresult = generateRequestUrlawait googleTranslateApi(text, { to: lang, forceBatch: false }); |
| 85 | + const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text; | |
| 104 | 86 | |
| 105 | - https.get(url, (resp) => { | |
| 87 | + response.setHeader('Content-Type', 'text/plain; charset=utf-8'); | |
| 106 | - const data = []; | |
| 88 | + console.log('Translated text: ' + translatedText); | |
| 107 | - | |
| 89 | + return response.send(translatedText); | |
| 108 | - resp.on('data', (chunk) => { | |
| 109 | - data.push(chunk); | |
| 110 | - }); | |
| 111 | - | |
| 112 | - resp.on('end', () => { | |
| 113 | - try { | |
| 114 | - const decodedData = decodeBuffer(Buffer.concat(data)); | |
| 115 | - const result = normaliseResponse(JSON.parse(decodedData)); | |
| 116 | - console.log('Translated text: ' + result.text); | |
| 117 | - response.setHeader('Content-Type', 'text/plain; charset=utf-8'); | |
| 118 | - return response.send(result.text); | |
| 119 | - } catch (error) { | |
| 120 | - console.log('Translation error', error); | |
| 121 | - return response.sendStatus(500); | |
| 122 | - } | |
| 123 | - }); | |
| 124 | - }).on('error', (err) => { | |
| 125 | - console.log('Translation error: ' + err.message); | |
| 126 | - return response.sendStatus(500); | |
| 127 | - }); | |
| 128 | 90 | } catch (error) { |
| 129 | 91 | console.log('Translation error', error); |
| 130 | 92 | return response.sendStatus(500); |