Replace Google Translate API package

89c0b0e1cda75035853ab73ff0b902d3662bd370

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +19 -50Showing whitespace changes
package-lock.json+13 -6
@@ -24,7 +24,7 @@
2424 "csrf-csrf": "^2.2.3",
2525 "express": "^4.21.0",
2626 "form-data": "^4.0.0",
2727 "google-translate-api-browserx": "^310.07.1",
2828 "helmet": "^7.1.0",
2929 "html-entities": "^2.5.2",
3030 "iconv-lite": "^0.6.3",
@@ -3689,11 +3689,18 @@
36893689 "url": "https://github.com/sponsors/sindresorhus"
36903690 }
36913691 },
36923692 "node_modules/google-translate-api-browserx": {
36933693 "version": "310.07.1",
36943694 "resolved": "https://registry.npmjs.org/google-translate-api-browserx/-/google-translate-api-browserx-310.07.1.tgz",
36953695 "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4lOdZDS6jRWzn1woOk62aOKQ5OyVaJSA+CXXpQeyc6CktOWxo36IWfstOjwG/xaynGVAzeBezzJG6qn8MLeqOq3SmW0AdkvnGl3Z2Sbpmk1A+jc2WwrBiRjqaY2A==",
36963696 "license": "MIT",
3697+ "engines": {
3698+ "node": ">=14.0.0"
3699+ },
3700+ "funding": {
3701+ "type": "github",
3702+ "url": "https://github.com/sponsors/AidanWelch"
3703+ }
36973704 },
36983705 "node_modules/gopd": {
36993706 "version": "1.0.1",
package.json+1 -1
@@ -14,7 +14,7 @@
1414 "csrf-csrf": "^2.2.3",
1515 "express": "^4.21.0",
1616 "form-data": "^4.0.0",
1717 "google-translate-api-browserx": "^310.07.1",
1818 "helmet": "^7.1.0",
1919 "html-entities": "^2.5.2",
2020 "iconv-lite": "^0.6.3",
src/endpoints/translate.js+5 -43
@@ -1,14 +1,9 @@
11import https from 'node:https';
2-import { createRequire } from 'node:module';
3-import { Buffer } from 'node:buffer';
42
53import fetch from 'node-fetch';
64import express from 'express';
7-import iconv from 'iconv-lite';
85import 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');
127
138import { readSecret, SECRET_KEYS } from './secrets.js';
149import { getConfigValue, uuidv4 } from '../util.js';
@@ -17,20 +12,6 @@ import { jsonParser } from '../express-common.js';
1712const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate';
1813const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
1914
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-
3415export const router = express.Router();
3516
3617router.post('/libre', jsonParser, async (request, response) => {
@@ -100,31 +81,12 @@ router.post('/google', jsonParser, async (request, response) => {
10081
10182 console.log('Input text: ' + text);
10283
10384 const urlresult = generateRequestUrlawait googleTranslateApi(text, { to: lang, forceBatch: false });
104-
85+ const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text;
105- https.get(url, (resp) => {
106- const data = [];
107-
108- resp.on('data', (chunk) => {
109- data.push(chunk);
110- });
11186
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);
11787 response.setHeader('Content-Type', 'text/plain; charset=utf-8');
118- return response.send(result.text);
88+ console.log('Translated text: ' + translatedText);
119- } catch (error) {
89+ return response.send(translatedText);
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- });
12890 } catch (error) {
12991 console.log('Translation error', error);
13092 return response.sendStatus(500);