Revert Google Translate client library
| @@ -24,6 +24,7 @@ | |||
| 24 | "csrf-csrf": "^2.2.3", | 24 | "csrf-csrf": "^2.2.3", |
| 25 | "express": "^4.21.0", | 25 | "express": "^4.21.0", |
| 26 | "form-data": "^4.0.0", | 26 | "form-data": "^4.0.0", |
| 27 | "google-translate-api-browser": "^3.0.1", | ||
| 27 | "google-translate-api-x": "^10.7.1", | 28 | "google-translate-api-x": "^10.7.1", |
| 28 | "helmet": "^7.1.0", | 29 | "helmet": "^7.1.0", |
| 29 | "html-entities": "^2.5.2", | 30 | "html-entities": "^2.5.2", |
| @@ -3693,6 +3694,12 @@ | |||
| 3693 | "url": "https://github.com/sponsors/sindresorhus" | 3694 | "url": "https://github.com/sponsors/sindresorhus" |
| 3694 | } | 3695 | } |
| 3695 | }, | 3696 | }, |
| 3697 | "node_modules/google-translate-api-browser": { | ||
| 3698 | "version": "3.0.1", | ||
| 3699 | "resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz", | ||
| 3700 | "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==", | ||
| 3701 | "license": "MIT" | ||
| 3702 | }, | ||
| 3696 | "node_modules/google-translate-api-x": { | 3703 | "node_modules/google-translate-api-x": { |
| 3697 | "version": "10.7.1", | 3704 | "version": "10.7.1", |
| 3698 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz", | 3705 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz", |
| @@ -14,6 +14,7 @@ | |||
| 14 | "csrf-csrf": "^2.2.3", | 14 | "csrf-csrf": "^2.2.3", |
| 15 | "express": "^4.21.0", | 15 | "express": "^4.21.0", |
| 16 | "form-data": "^4.0.0", | 16 | "form-data": "^4.0.0", |
| 17 | "google-translate-api-browser": "^3.0.1", | ||
| 17 | "google-translate-api-x": "^10.7.1", | 18 | "google-translate-api-x": "^10.7.1", |
| 18 | "helmet": "^7.1.0", | 19 | "helmet": "^7.1.0", |
| 19 | "html-entities": "^2.5.2", | 20 | "html-entities": "^2.5.2", |
| @@ -1,9 +1,10 @@ | |||
| 1 | import https from 'node:https'; | 1 | import https from 'node:https'; |
| 2 | import { createRequire } from 'node:module'; | ||
| 2 | 3 | ||
| 3 | import fetch from 'node-fetch'; | 4 | import fetch from 'node-fetch'; |
| 4 | import express from 'express'; | 5 | import express from 'express'; |
| 5 | import bingTranslateApi from 'bing-translate-api'; | 6 | import bingTranslateApi from 'bing-translate-api'; |
| 6 | import googleTranslateApi from 'google-translate-api-x'; | 7 | import iconv from 'iconv-lite'; |
| 7 | 8 | ||
| 8 | import { readSecret, SECRET_KEYS } from './secrets.js'; | 9 | import { readSecret, SECRET_KEYS } from './secrets.js'; |
| 9 | import { getConfigValue, uuidv4 } from '../util.js'; | 10 | import { getConfigValue, uuidv4 } from '../util.js'; |
| @@ -14,6 +15,30 @@ const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate'; | |||
| 14 | 15 | ||
| 15 | export const router = express.Router(); | 16 | export const router = express.Router(); |
| 16 | 17 | ||
| 18 | /** | ||
| 19 | * Get the Google Translate API client. | ||
| 20 | * @returns {import('google-translate-api-browser')} Google Translate API client | ||
| 21 | */ | ||
| 22 | function getGoogleTranslateClient() { | ||
| 23 | const require = createRequire(import.meta.url); | ||
| 24 | const googleTranslateApi = require('google-translate-api-browser'); | ||
| 25 | return googleTranslateApi; | ||
| 26 | } | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Tries to decode an ArrayBuffer to a string using iconv-lite for UTF-8. | ||
| 30 | * @param {ArrayBuffer} buffer ArrayBuffer | ||
| 31 | * @returns {string} Decoded string | ||
| 32 | */ | ||
| 33 | function decodeBuffer(buffer) { | ||
| 34 | try { | ||
| 35 | return iconv.decode(Buffer.from(buffer), 'utf-8'); | ||
| 36 | } catch (error) { | ||
| 37 | console.log('Failed to decode buffer:', error); | ||
| 38 | return Buffer.from(buffer).toString('utf-8'); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 17 | router.post('/libre', jsonParser, async (request, response) => { | 42 | router.post('/libre', jsonParser, async (request, response) => { |
| 18 | const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE); | 43 | const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE); |
| 19 | const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL); | 44 | const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL); |
| @@ -81,8 +106,18 @@ router.post('/google', jsonParser, async (request, response) => { | |||
| 81 | 106 | ||
| 82 | console.log('Input text: ' + text); | 107 | console.log('Input text: ' + text); |
| 83 | 108 | ||
| 84 | const result = await googleTranslateApi(text, { to: lang, forceBatch: false }); | 109 | const { generateRequestUrl, normaliseResponse } = getGoogleTranslateClient(); |
| 85 | const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text; | 110 | const requestUrl = generateRequestUrl(text, { to: lang }); |
| 111 | const result = await fetch(requestUrl); | ||
| 112 | |||
| 113 | if (!result.ok) { | ||
| 114 | console.log('Google Translate error: ', result.statusText); | ||
| 115 | return response.sendStatus(500); | ||
| 116 | } | ||
| 117 | |||
| 118 | const buffer = await result.arrayBuffer(); | ||
| 119 | const translateResponse = normaliseResponse(JSON.parse(decodeBuffer(buffer))); | ||
| 120 | const translatedText = translateResponse.text; | ||
| 86 | 121 | ||
| 87 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); | 122 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); |
| 88 | console.log('Translated text: ' + translatedText); | 123 | console.log('Translated text: ' + translatedText); |