mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Revert Google Translate client library
This commit is contained in:
7
package-lock.json
generated
7
package-lock.json
generated
@ -24,6 +24,7 @@
|
|||||||
"csrf-csrf": "^2.2.3",
|
"csrf-csrf": "^2.2.3",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
|
"google-translate-api-browser": "^3.0.1",
|
||||||
"google-translate-api-x": "^10.7.1",
|
"google-translate-api-x": "^10.7.1",
|
||||||
"helmet": "^7.1.0",
|
"helmet": "^7.1.0",
|
||||||
"html-entities": "^2.5.2",
|
"html-entities": "^2.5.2",
|
||||||
@ -3693,6 +3694,12 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/google-translate-api-browser": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz",
|
||||||
|
"integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/google-translate-api-x": {
|
"node_modules/google-translate-api-x": {
|
||||||
"version": "10.7.1",
|
"version": "10.7.1",
|
||||||
"resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz",
|
"resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz",
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
"csrf-csrf": "^2.2.3",
|
"csrf-csrf": "^2.2.3",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
|
"google-translate-api-browser": "^3.0.1",
|
||||||
"google-translate-api-x": "^10.7.1",
|
"google-translate-api-x": "^10.7.1",
|
||||||
"helmet": "^7.1.0",
|
"helmet": "^7.1.0",
|
||||||
"html-entities": "^2.5.2",
|
"html-entities": "^2.5.2",
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import https from 'node:https';
|
import https from 'node:https';
|
||||||
|
import { createRequire } from 'node:module';
|
||||||
|
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import bingTranslateApi from 'bing-translate-api';
|
import bingTranslateApi from 'bing-translate-api';
|
||||||
import googleTranslateApi from 'google-translate-api-x';
|
import iconv from 'iconv-lite';
|
||||||
|
|
||||||
import { readSecret, SECRET_KEYS } from './secrets.js';
|
import { readSecret, SECRET_KEYS } from './secrets.js';
|
||||||
import { getConfigValue, uuidv4 } from '../util.js';
|
import { getConfigValue, uuidv4 } from '../util.js';
|
||||||
@ -14,6 +15,30 @@ const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
|
|||||||
|
|
||||||
export const router = express.Router();
|
export const router = express.Router();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Google Translate API client.
|
||||||
|
* @returns {import('google-translate-api-browser')} Google Translate API client
|
||||||
|
*/
|
||||||
|
function getGoogleTranslateClient() {
|
||||||
|
const require = createRequire(import.meta.url);
|
||||||
|
const googleTranslateApi = require('google-translate-api-browser');
|
||||||
|
return googleTranslateApi;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to decode an ArrayBuffer to a string using iconv-lite for UTF-8.
|
||||||
|
* @param {ArrayBuffer} buffer ArrayBuffer
|
||||||
|
* @returns {string} Decoded string
|
||||||
|
*/
|
||||||
|
function decodeBuffer(buffer) {
|
||||||
|
try {
|
||||||
|
return iconv.decode(Buffer.from(buffer), 'utf-8');
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Failed to decode buffer:', error);
|
||||||
|
return Buffer.from(buffer).toString('utf-8');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
router.post('/libre', jsonParser, async (request, response) => {
|
router.post('/libre', jsonParser, async (request, response) => {
|
||||||
const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
|
const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
|
||||||
const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
|
const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
|
||||||
@ -81,8 +106,18 @@ router.post('/google', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
const result = await googleTranslateApi(text, { to: lang, forceBatch: false });
|
const { generateRequestUrl, normaliseResponse } = getGoogleTranslateClient();
|
||||||
const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text;
|
const requestUrl = generateRequestUrl(text, { to: lang });
|
||||||
|
const result = await fetch(requestUrl);
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
console.log('Google Translate error: ', result.statusText);
|
||||||
|
return response.sendStatus(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await result.arrayBuffer();
|
||||||
|
const translateResponse = normaliseResponse(JSON.parse(decodeBuffer(buffer)));
|
||||||
|
const translatedText = translateResponse.text;
|
||||||
|
|
||||||
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||||||
console.log('Translated text: ' + translatedText);
|
console.log('Translated text: ' + translatedText);
|
||||||
|
Reference in New Issue
Block a user