Merge branch 'release' into staging
| @@ -33,6 +33,7 @@ | |||
| 33 | "express": "^4.21.0", | 33 | "express": "^4.21.0", |
| 34 | "form-data": "^4.0.0", | 34 | "form-data": "^4.0.0", |
| 35 | "fuse.js": "^7.0.0", | 35 | "fuse.js": "^7.0.0", |
| 36 | "google-translate-api-browser": "^3.0.1", | ||
| 36 | "google-translate-api-x": "^10.7.1", | 37 | "google-translate-api-x": "^10.7.1", |
| 37 | "handlebars": "^4.7.8", | 38 | "handlebars": "^4.7.8", |
| 38 | "helmet": "^7.1.0", | 39 | "helmet": "^7.1.0", |
| @@ -4214,6 +4215,12 @@ | |||
| 4214 | "url": "https://github.com/sponsors/sindresorhus" | 4215 | "url": "https://github.com/sponsors/sindresorhus" |
| 4215 | } | 4216 | } |
| 4216 | }, | 4217 | }, |
| 4218 | "node_modules/google-translate-api-browser": { | ||
| 4219 | "version": "3.0.1", | ||
| 4220 | "resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz", | ||
| 4221 | "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==", | ||
| 4222 | "license": "MIT" | ||
| 4223 | }, | ||
| 4217 | "node_modules/google-translate-api-x": { | 4224 | "node_modules/google-translate-api-x": { |
| 4218 | "version": "10.7.1", | 4225 | "version": "10.7.1", |
| 4219 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz", | 4226 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz", |
| @@ -23,6 +23,7 @@ | |||
| 23 | "express": "^4.21.0", | 23 | "express": "^4.21.0", |
| 24 | "form-data": "^4.0.0", | 24 | "form-data": "^4.0.0", |
| 25 | "fuse.js": "^7.0.0", | 25 | "fuse.js": "^7.0.0", |
| 26 | "google-translate-api-browser": "^3.0.1", | ||
| 26 | "google-translate-api-x": "^10.7.1", | 27 | "google-translate-api-x": "^10.7.1", |
| 27 | "handlebars": "^4.7.8", | 28 | "handlebars": "^4.7.8", |
| 28 | "helmet": "^7.1.0", | 29 | "helmet": "^7.1.0", |
| @@ -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); |