mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Update bing translate API package, fix language codes
This commit is contained in:
8
package-lock.json
generated
8
package-lock.json
generated
@ -18,7 +18,7 @@
|
|||||||
"@popperjs/core": "^2.11.8",
|
"@popperjs/core": "^2.11.8",
|
||||||
"@zeldafan0225/ai_horde": "^5.1.0",
|
"@zeldafan0225/ai_horde": "^5.1.0",
|
||||||
"archiver": "^7.0.1",
|
"archiver": "^7.0.1",
|
||||||
"bing-translate-api": "^2.9.1",
|
"bing-translate-api": "^4.0.2",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"bowser": "^2.11.0",
|
"bowser": "^2.11.0",
|
||||||
"command-exists": "^1.2.9",
|
"command-exists": "^1.2.9",
|
||||||
@ -2161,9 +2161,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/bing-translate-api": {
|
"node_modules/bing-translate-api": {
|
||||||
"version": "2.9.1",
|
"version": "4.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/bing-translate-api/-/bing-translate-api-2.9.1.tgz",
|
"resolved": "https://registry.npmjs.org/bing-translate-api/-/bing-translate-api-4.0.2.tgz",
|
||||||
"integrity": "sha512-DaYqa7iupfj+fj/KeaeZSp5FUY/ZR4sZ6jqoTP0RHkYOUfo7wwoxlhYDkh4VcvBBzuVORnBEgdXBVQrfM4kk7g==",
|
"integrity": "sha512-JJ8XUehnxzOhHU91oy86xEtp8OOMjVEjCZJX042fKxoO19NNvxJ5omeCcxQNFoPbDqVpBJwqiGVquL0oPdQm1Q==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"got": "^11.8.6"
|
"got": "^11.8.6"
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
"@popperjs/core": "^2.11.8",
|
"@popperjs/core": "^2.11.8",
|
||||||
"@zeldafan0225/ai_horde": "^5.1.0",
|
"@zeldafan0225/ai_horde": "^5.1.0",
|
||||||
"archiver": "^7.0.1",
|
"archiver": "^7.0.1",
|
||||||
"bing-translate-api": "^2.9.1",
|
"bing-translate-api": "^4.0.2",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"bowser": "^2.11.0",
|
"bowser": "^2.11.0",
|
||||||
"command-exists": "^1.2.9",
|
"command-exists": "^1.2.9",
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import https from 'node:https';
|
|
||||||
import { createRequire } from 'node:module';
|
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 { translate as bingTranslate } from 'bing-translate-api';
|
||||||
import iconv from 'iconv-lite';
|
import iconv from 'iconv-lite';
|
||||||
|
|
||||||
import { readSecret, SECRET_KEYS } from './secrets.js';
|
import { readSecret, SECRET_KEYS } from './secrets.js';
|
||||||
@ -56,6 +55,10 @@ router.post('/libre', jsonParser, async (request, response) => {
|
|||||||
request.body.lang = 'zt';
|
request.body.lang = 'zt';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
|
||||||
|
request.body.lang = 'pt';
|
||||||
|
}
|
||||||
|
|
||||||
const text = request.body.text;
|
const text = request.body.text;
|
||||||
const lang = request.body.lang;
|
const lang = request.body.lang;
|
||||||
|
|
||||||
@ -81,7 +84,7 @@ router.post('/libre', jsonParser, async (request, response) => {
|
|||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
const error = await result.text();
|
const error = await result.text();
|
||||||
console.log('LibreTranslate error: ', result.statusText, error);
|
console.log('LibreTranslate error: ', result.statusText, error);
|
||||||
return response.sendStatus(result.status);
|
return response.sendStatus(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {any} */
|
/** @type {any} */
|
||||||
@ -130,6 +133,14 @@ router.post('/google', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
router.post('/yandex', jsonParser, async (request, response) => {
|
router.post('/yandex', jsonParser, async (request, response) => {
|
||||||
try {
|
try {
|
||||||
|
if (request.body.lang === 'pt-PT') {
|
||||||
|
request.body.lang = 'pt';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
|
||||||
|
request.body.lang = 'zh';
|
||||||
|
}
|
||||||
|
|
||||||
const chunks = request.body.chunks;
|
const chunks = request.body.chunks;
|
||||||
const lang = request.body.lang;
|
const lang = request.body.lang;
|
||||||
|
|
||||||
@ -185,6 +196,14 @@ router.post('/lingva', jsonParser, async (request, response) => {
|
|||||||
return response.sendStatus(400);
|
return response.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
|
||||||
|
request.body.lang = 'zh';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
|
||||||
|
request.body.lang = 'pt';
|
||||||
|
}
|
||||||
|
|
||||||
const text = request.body.text;
|
const text = request.body.text;
|
||||||
const lang = request.body.lang;
|
const lang = request.body.lang;
|
||||||
|
|
||||||
@ -193,29 +212,24 @@ router.post('/lingva', jsonParser, async (request, response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
const url = `${baseUrl}/auto/${lang}/${encodeURIComponent(text)}`;
|
|
||||||
|
|
||||||
https.get(url, (resp) => {
|
try {
|
||||||
let data = '';
|
const url = `${baseUrl}/auto/${lang}/${encodeURIComponent(text)}`;
|
||||||
|
const result = await fetch(url);
|
||||||
|
|
||||||
resp.on('data', (chunk) => {
|
if (!result.ok) {
|
||||||
data += chunk;
|
const error = await result.text();
|
||||||
});
|
console.log('Lingva error: ', result.statusText, error);
|
||||||
|
}
|
||||||
|
|
||||||
resp.on('end', () => {
|
/** @type {any} */
|
||||||
try {
|
const data = await result.json();
|
||||||
const result = JSON.parse(data);
|
console.log('Translated text: ' + data.translation);
|
||||||
console.log('Translated text: ' + result.translation);
|
return response.send(data.translation);
|
||||||
return response.send(result.translation);
|
} catch (error) {
|
||||||
} catch (error) {
|
console.log('Translation error:', error);
|
||||||
console.log('Translation error', error);
|
|
||||||
return response.sendStatus(500);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}).on('error', (err) => {
|
|
||||||
console.log('Translation error: ' + err.message);
|
|
||||||
return response.sendStatus(500);
|
return response.sendStatus(500);
|
||||||
});
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Translation error', error);
|
console.log('Translation error', error);
|
||||||
return response.sendStatus(500);
|
return response.sendStatus(500);
|
||||||
@ -266,7 +280,7 @@ router.post('/deepl', jsonParser, async (request, response) => {
|
|||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
const error = await result.text();
|
const error = await result.text();
|
||||||
console.log('DeepL error: ', result.statusText, error);
|
console.log('DeepL error: ', result.statusText, error);
|
||||||
return response.sendStatus(result.status);
|
return response.sendStatus(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {any} */
|
/** @type {any} */
|
||||||
@ -319,7 +333,7 @@ router.post('/onering', jsonParser, async (request, response) => {
|
|||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
const error = await result.text();
|
const error = await result.text();
|
||||||
console.log('OneRing error: ', result.statusText, error);
|
console.log('OneRing error: ', result.statusText, error);
|
||||||
return response.sendStatus(result.status);
|
return response.sendStatus(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {any} */
|
/** @type {any} */
|
||||||
@ -375,7 +389,7 @@ router.post('/deeplx', jsonParser, async (request, response) => {
|
|||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
const error = await result.text();
|
const error = await result.text();
|
||||||
console.log('DeepLX error: ', result.statusText, error);
|
console.log('DeepLX error: ', result.statusText, error);
|
||||||
return response.sendStatus(result.status);
|
return response.sendStatus(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {any} */
|
/** @type {any} */
|
||||||
@ -390,24 +404,34 @@ router.post('/deeplx', jsonParser, async (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/bing', jsonParser, async (request, response) => {
|
router.post('/bing', jsonParser, async (request, response) => {
|
||||||
const text = request.body.text;
|
try {
|
||||||
let lang = request.body.lang;
|
const text = request.body.text;
|
||||||
|
let lang = request.body.lang;
|
||||||
|
|
||||||
if (request.body.lang === 'zh-CN') {
|
if (request.body.lang === 'zh-CN') {
|
||||||
lang = 'zh-Hans';
|
lang = 'zh-Hans';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!text || !lang) {
|
if (request.body.lang === 'zh-TW') {
|
||||||
return response.sendStatus(400);
|
lang = 'zh-Hant';
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Input text: ' + text);
|
if (request.body.lang === 'pt-BR') {
|
||||||
|
lang = 'pt';
|
||||||
|
}
|
||||||
|
|
||||||
bingTranslateApi.translate(text, null, lang).then(result => {
|
if (!text || !lang) {
|
||||||
console.log('Translated text: ' + result.translation);
|
return response.sendStatus(400);
|
||||||
return response.send(result.translation);
|
}
|
||||||
}).catch(err => {
|
|
||||||
console.log('Translation error: ' + err.message);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
|
const result = await bingTranslate(text, null, lang);
|
||||||
|
const translatedText = result?.translation;
|
||||||
|
console.log('Translated text: ' + translatedText);
|
||||||
|
return response.send(translatedText);
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Translation error', error);
|
||||||
return response.sendStatus(500);
|
return response.sendStatus(500);
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user