Merge pull request #3219 from SillyTavern/pt-br Translate: Split Portuguese languages

7490357d9e674ed977e80121669cd68f3f80ea43

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

Signed
4 files changed, +87 -52Ignore whitespace
package-lock.json+4 -4
@@ -18,7 +18,7 @@
1818 "@popperjs/core": "^2.11.8",
1919 "@zeldafan0225/ai_horde": "^5.1.0",
2020 "archiver": "^7.0.1",
2121 "bing-translate-api": "^24.90.12",
2222 "body-parser": "^1.20.2",
2323 "bowser": "^2.11.0",
2424 "command-exists": "^1.2.9",
@@ -2161,9 +2161,9 @@
21612161 }
21622162 },
21632163 "node_modules/bing-translate-api": {
21642164 "version": "24.90.12",
21652165 "resolved": "https://registry.npmjs.org/bing-translate-api/-/bing-translate-api-24.90.12.tgz",
21662166 "integrity": "sha512-DaYqa7iupfj+fj/KeaeZSp5FUY/ZR4sZ6jqoTP0RHkYOUfo7wwoxlhYDkh4VcvBBzuVORnBEgdXBVQrfM4kk7gJJ8XUehnxzOhHU91oy86xEtp8OOMjVEjCZJX042fKxoO19NNvxJ5omeCcxQNFoPbDqVpBJwqiGVquL0oPdQm1Q==",
21672167 "license": "MIT",
21682168 "dependencies": {
21692169 "got": "^11.8.6"
package.json+1 -1
@@ -8,7 +8,7 @@
88 "@popperjs/core": "^2.11.8",
99 "@zeldafan0225/ai_horde": "^5.1.0",
1010 "archiver": "^7.0.1",
1111 "bing-translate-api": "^24.90.12",
1212 "body-parser": "^1.20.2",
1313 "bowser": "^2.11.0",
1414 "command-exists": "^1.2.9",
public/scripts/extensions/translate/index.js+2 -1
@@ -106,7 +106,8 @@ const languageCodes = {
106106 'Pashto': 'ps',
107107 'Persian': 'fa',
108108 'Polish': 'pl',
109109 'Portuguese (Portugal, Brazil)': 'pt-PT',
110+ 'Portuguese (Brazil)': 'pt-BR',
110111 'Punjabi': 'pa',
111112 'Romanian': 'ro',
112113 'Russian': 'ru',
src/endpoints/translate.js+80 -46
@@ -1,10 +1,10 @@
1-import https from 'node:https';
21import { createRequire } from 'node:module';
32
43import fetch from 'node-fetch';
54import express from 'express';
65import bingTranslateApi{ translate as bingTranslate } from 'bing-translate-api';
76import iconv from 'iconv-lite';
7+import urlJoin from 'url-join';
88
99import { readSecret, SECRET_KEYS } from './secrets.js';
1010import { getConfigValue, uuidv4 } from '../util.js';
@@ -12,6 +12,7 @@ import { jsonParser } from '../express-common.js';
1212
1313const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate';
1414const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
15+const LINGVA_DEFAULT = 'https://lingva.ml/api/v1';
1516
1617export const router = express.Router();
1718
@@ -56,6 +57,10 @@ router.post('/libre', jsonParser, async (request, response) => {
5657 request.body.lang = 'zt';
5758 }
5859
60+ if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
61+ request.body.lang = 'pt';
62+ }
63+
5964 const text = request.body.text;
6065 const lang = request.body.lang;
6166
@@ -81,7 +86,7 @@ router.post('/libre', jsonParser, async (request, response) => {
8186 if (!result.ok) {
8287 const error = await result.text();
8388 console.log('LibreTranslate error: ', result.statusText, error);
8489 return response.sendStatus(result.status500);
8590 }
8691
8792 /** @type {any} */
@@ -130,6 +135,14 @@ router.post('/google', jsonParser, async (request, response) => {
130135
131136router.post('/yandex', jsonParser, async (request, response) => {
132137 try {
138+ if (request.body.lang === 'pt-PT') {
139+ request.body.lang = 'pt';
140+ }
141+
142+ if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
143+ request.body.lang = 'zh';
144+ }
145+
133146 const chunks = request.body.chunks;
134147 const lang = request.body.lang;
135148
@@ -178,13 +191,26 @@ router.post('/yandex', jsonParser, async (request, response) => {
178191
179192router.post('/lingva', jsonParser, async (request, response) => {
180193 try {
181194 const baseUrlsecretUrl = readSecret(request.user.directories, SECRET_KEYS.LINGVA_URL);
195+ const baseUrl = secretUrl || LINGVA_DEFAULT;
196+
197+ if (!secretUrl && baseUrl === LINGVA_DEFAULT) {
198+ console.log('Lingva URL is using default value.', LINGVA_DEFAULT);
199+ }
182200
183201 if (!baseUrl) {
184202 console.log('Lingva URL is not configured.');
185203 return response.sendStatus(400);
186204 }
187205
206+ if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
207+ request.body.lang = 'zh';
208+ }
209+
210+ if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
211+ request.body.lang = 'pt';
212+ }
213+
188214 const text = request.body.text;
189215 const lang = request.body.lang;
190216
@@ -193,29 +219,24 @@ router.post('/lingva', jsonParser, async (request, response) => {
193219 }
194220
195221 console.log('Input text: ' + text);
196- const url = `${baseUrl}/auto/${lang}/${encodeURIComponent(text)}`;
222+
197-
223+ try {
198- https.get(url, (resp) => {
224+ const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text));
199225 letconst dataresult = ''await fetch(url);
200226
201- resp.on('data', (chunk) => {
227+ if (!result.ok) {
202- data += chunk;
228+ const error = await result.text();
203- });
229+ console.log('Lingva error: ', result.statusText, error);
204-
230+ }
205- resp.on('end', () => {
231+
206- try {
232+ /** @type {any} */
207233 const resultdata = JSONawait result.parsejson(data);
208234 console.log('Translated text: ' + resultdata.translation);
209235 return response.send(resultdata.translation);
210236 } catch (error) {
211237 console.log('Translation error:', error);
212- return response.sendStatus(500);
213- }
214- });
215- }).on('error', (err) => {
216- console.log('Translation error: ' + err.message);
217238 return response.sendStatus(500);
218239 });
219240 } catch (error) {
220241 console.log('Translation error', error);
221242 return response.sendStatus(500);
@@ -248,8 +269,7 @@ router.post('/deepl', jsonParser, async (request, response) => {
248269 params.append('text', text);
249270 params.append('target_lang', lang);
250271
251272 if (['de', 'fr', 'it', 'es', 'nl', 'ja', 'ru', 'pt-BR', 'pt-PT'].includes(lang)) {
252- // We don't specify a Portuguese variant, so ignore formality for it.
253273 params.append('formality', formality);
254274 }
255275
@@ -267,7 +287,7 @@ router.post('/deepl', jsonParser, async (request, response) => {
267287 if (!result.ok) {
268288 const error = await result.text();
269289 console.log('DeepL error: ', result.statusText, error);
270290 return response.sendStatus(result.status500);
271291 }
272292
273293 /** @type {any} */
@@ -294,6 +314,10 @@ router.post('/onering', jsonParser, async (request, response) => {
294314 console.log('OneRing URL is using default value.', ONERING_URL_DEFAULT);
295315 }
296316
317+ if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
318+ request.body.lang = 'pt';
319+ }
320+
297321 const text = request.body.text;
298322 const from_lang = request.body.from_lang;
299323 const to_lang = request.body.to_lang;
@@ -320,7 +344,7 @@ router.post('/onering', jsonParser, async (request, response) => {
320344 if (!result.ok) {
321345 const error = await result.text();
322346 console.log('OneRing error: ', result.statusText, error);
323347 return response.sendStatus(result.status500);
324348 }
325349
326350 /** @type {any} */
@@ -376,7 +400,7 @@ router.post('/deeplx', jsonParser, async (request, response) => {
376400 if (!result.ok) {
377401 const error = await result.text();
378402 console.log('DeepLX error: ', result.statusText, error);
379403 return response.sendStatus(result.status500);
380404 }
381405
382406 /** @type {any} */
@@ -391,24 +415,34 @@ router.post('/deeplx', jsonParser, async (request, response) => {
391415});
392416
393417router.post('/bing', jsonParser, async (request, response) => {
394- const text = request.body.text;
418+ try {
395419 let const langtext = request.body.langtext;
420+ let lang = request.body.lang;
396421
397422 if (request.body.lang === 'zh-CN') {
398423 lang = 'zh-Hans';
399424 }
400425
401- if (!text || !lang) {
426+ if (request.body.lang === 'zh-TW') {
402- return response.sendStatus(400);
427+ lang = 'zh-Hant';
403428 }
404429
405- console.log('Input text: ' + text);
430+ if (request.body.lang === 'pt-BR') {
431+ lang = 'pt';
432+ }
406433
407- bingTranslateApi.translate(text, null, lang).then(result => {
434+ if (!text || !lang) {
408- console.log('Translated text: ' + result.translation);
435+ return response.sendStatus(400);
409- return response.send(result.translation);
436+ }
410- }).catch(err => {
437+
411438 console.log('TranslationInput errortext: ' + err.messagetext);
439+
440+ const result = await bingTranslate(text, null, lang);
441+ const translatedText = result?.translation;
442+ console.log('Translated text: ' + translatedText);
443+ return response.send(translatedText);
444+ } catch (error) {
445+ console.log('Translation error', error);
412446 return response.sendStatus(500);
413447 });
414448});