@yxw007/translate 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/README_zh-CN.md +12 -1
- package/dist/browser/index.cjs +431 -33
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +426 -34
- package/dist/browser/index.esm.js.map +1 -1
- package/dist/browser/index.esm.min.js +1 -1
- package/dist/browser/index.esm.min.js.map +1 -1
- package/dist/browser/index.min.cjs +1 -1
- package/dist/browser/index.min.cjs.map +1 -1
- package/dist/browser/index.umd.js +431 -33
- package/dist/browser/index.umd.js.map +1 -1
- package/dist/browser/index.umd.min.js +1 -1
- package/dist/browser/index.umd.min.js.map +1 -1
- package/dist/index.d.ts +2147 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/node/index.cjs +431 -33
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +426 -34
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/node/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// translate v0.
|
|
1
|
+
// translate v0.4.0 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -17,6 +17,14 @@ class TranslationError extends Error {
|
|
|
17
17
|
Error.captureStackTrace(this, this.constructor);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
+
class CheckLanguageError extends Error {
|
|
21
|
+
region;
|
|
22
|
+
constructor(region, message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.region = region;
|
|
25
|
+
Error.captureStackTrace(this, this.constructor);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
20
28
|
const OPEN_AI_MODELS = [
|
|
21
29
|
"o1-preview",
|
|
22
30
|
"o1-preview-2024-09-12",
|
|
@@ -202,7 +210,7 @@ function isOverMaxCharacterNum(text, max_character_num) {
|
|
|
202
210
|
return total > max_character_num;
|
|
203
211
|
}
|
|
204
212
|
|
|
205
|
-
function google(options) {
|
|
213
|
+
function google$1(options) {
|
|
206
214
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
207
215
|
return {
|
|
208
216
|
name: "google",
|
|
@@ -231,13 +239,29 @@ function google(options) {
|
|
|
231
239
|
}
|
|
232
240
|
return translations;
|
|
233
241
|
},
|
|
242
|
+
async checkLanguage(text) {
|
|
243
|
+
const url = `${base}?client=gtx&sl=auto&tl=en&dt=t&q=${encodeURI(text)}`;
|
|
244
|
+
const res = await fetch(url);
|
|
245
|
+
if (!res.ok) {
|
|
246
|
+
throw await throwResponseError(this.name, res);
|
|
247
|
+
}
|
|
248
|
+
const body = await res.json();
|
|
249
|
+
if (!body || body.length < 3) {
|
|
250
|
+
throw new CheckLanguageError(this.name, "Check language fail! No result returned");
|
|
251
|
+
}
|
|
252
|
+
const detectedLanguage = body[2];
|
|
253
|
+
if (!detectedLanguage) {
|
|
254
|
+
throw new CheckLanguageError(this.name, "Check language fail! Language not detected");
|
|
255
|
+
}
|
|
256
|
+
return detectedLanguage;
|
|
257
|
+
},
|
|
234
258
|
};
|
|
235
259
|
}
|
|
236
260
|
|
|
237
261
|
/**
|
|
238
262
|
* Azure translate documentation: https://learn.microsoft.com/zh-cn/azure/ai-services/translator/reference/v3-0-translate
|
|
239
263
|
*/
|
|
240
|
-
function azure$
|
|
264
|
+
function azure$2(options) {
|
|
241
265
|
const { key, region } = options;
|
|
242
266
|
const name = "azure";
|
|
243
267
|
const checkOptions = () => {
|
|
@@ -270,7 +294,7 @@ function azure$1(options) {
|
|
|
270
294
|
}
|
|
271
295
|
const bodyRes = await res.json();
|
|
272
296
|
if (bodyRes.error) {
|
|
273
|
-
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
297
|
+
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message} \n Go to https://learn.microsoft.com/zh-cn/azure/ai-services/translator/text-translation/reference/v3/translate view details`);
|
|
274
298
|
}
|
|
275
299
|
const body = bodyRes;
|
|
276
300
|
if (!body || body.length === 0) {
|
|
@@ -285,10 +309,35 @@ function azure$1(options) {
|
|
|
285
309
|
}
|
|
286
310
|
return translations;
|
|
287
311
|
},
|
|
312
|
+
async checkLanguage(text) {
|
|
313
|
+
checkOptions();
|
|
314
|
+
const detectUrl = "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0";
|
|
315
|
+
const res = await fetch(detectUrl, {
|
|
316
|
+
method: "POST",
|
|
317
|
+
headers: {
|
|
318
|
+
"Content-Type": "application/json; charset=UTF-8",
|
|
319
|
+
"Ocp-Apim-Subscription-Key": key,
|
|
320
|
+
"Ocp-Apim-Subscription-Region": region,
|
|
321
|
+
},
|
|
322
|
+
body: JSON.stringify([{ Text: text }]),
|
|
323
|
+
});
|
|
324
|
+
if (!res.ok) {
|
|
325
|
+
throw await throwResponseError(this.name, res);
|
|
326
|
+
}
|
|
327
|
+
const response = await res.json();
|
|
328
|
+
if (!response || !Array.isArray(response) || response.length === 0) {
|
|
329
|
+
throw new TranslationError(this.name, "Check language fail! No result returned");
|
|
330
|
+
}
|
|
331
|
+
const detection = response[0];
|
|
332
|
+
if (!detection.language) {
|
|
333
|
+
throw new TranslationError(this.name, "Check language fail! Language not detected");
|
|
334
|
+
}
|
|
335
|
+
return detection.language;
|
|
336
|
+
},
|
|
288
337
|
};
|
|
289
338
|
}
|
|
290
339
|
|
|
291
|
-
function amazon$
|
|
340
|
+
function amazon$2(options) {
|
|
292
341
|
const { region, accessKeyId, secretAccessKey } = options;
|
|
293
342
|
const name = "amazon";
|
|
294
343
|
const checkOptions = () => {
|
|
@@ -319,6 +368,28 @@ function amazon$1(options) {
|
|
|
319
368
|
}
|
|
320
369
|
return translations;
|
|
321
370
|
},
|
|
371
|
+
async checkLanguage(text) {
|
|
372
|
+
checkOptions();
|
|
373
|
+
const translateClient = new clientTranslate.TranslateClient({
|
|
374
|
+
region: region,
|
|
375
|
+
credentials: { accessKeyId, secretAccessKey },
|
|
376
|
+
});
|
|
377
|
+
const command = new clientTranslate.TranslateTextCommand({
|
|
378
|
+
SourceLanguageCode: "auto",
|
|
379
|
+
TargetLanguageCode: "en",
|
|
380
|
+
Text: text,
|
|
381
|
+
});
|
|
382
|
+
try {
|
|
383
|
+
const response = await translateClient.send(command);
|
|
384
|
+
if (!response.SourceLanguageCode) {
|
|
385
|
+
throw new TranslationError(name, "Check language fail! Source language not detected");
|
|
386
|
+
}
|
|
387
|
+
return response.SourceLanguageCode;
|
|
388
|
+
}
|
|
389
|
+
catch (error) {
|
|
390
|
+
throw new TranslationError(name, `Check language fail! ${error.message || error}`);
|
|
391
|
+
}
|
|
392
|
+
},
|
|
322
393
|
};
|
|
323
394
|
}
|
|
324
395
|
|
|
@@ -1411,7 +1482,7 @@ function requireCore () {
|
|
|
1411
1482
|
var md5Exports = md5$1.exports;
|
|
1412
1483
|
var md5 = /*@__PURE__*/getDefaultExportFromCjs(md5Exports);
|
|
1413
1484
|
|
|
1414
|
-
function baidu$
|
|
1485
|
+
function baidu$2(options) {
|
|
1415
1486
|
const { appId, secretKey } = options;
|
|
1416
1487
|
const url = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate";
|
|
1417
1488
|
const name = "baidu";
|
|
@@ -1452,7 +1523,7 @@ function baidu$1(options) {
|
|
|
1452
1523
|
}
|
|
1453
1524
|
const data = await res.json();
|
|
1454
1525
|
if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
|
|
1455
|
-
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
|
|
1526
|
+
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg} \n Go to https://fanyi-api.baidu.com/product/123 view details`);
|
|
1456
1527
|
}
|
|
1457
1528
|
const translations = [];
|
|
1458
1529
|
for (const translation of data.trans_result) {
|
|
@@ -1462,10 +1533,35 @@ function baidu$1(options) {
|
|
|
1462
1533
|
}
|
|
1463
1534
|
return translations;
|
|
1464
1535
|
},
|
|
1536
|
+
async checkLanguage(text) {
|
|
1537
|
+
checkOptions();
|
|
1538
|
+
const salt = Date.now();
|
|
1539
|
+
const sign = md5(`${appId}${text}${salt}${secretKey}`).toString();
|
|
1540
|
+
const body = new URLSearchParams();
|
|
1541
|
+
body.append("q", text);
|
|
1542
|
+
body.append("appid", appId);
|
|
1543
|
+
body.append("salt", salt.toString());
|
|
1544
|
+
body.append("sign", sign);
|
|
1545
|
+
const res = await fetch("https://fanyi-api.baidu.com/api/trans/vip/language", {
|
|
1546
|
+
method: "POST",
|
|
1547
|
+
headers: {
|
|
1548
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
1549
|
+
},
|
|
1550
|
+
body: body.toString(),
|
|
1551
|
+
});
|
|
1552
|
+
if (!res.ok) {
|
|
1553
|
+
throw await throwResponseError(this.name, res);
|
|
1554
|
+
}
|
|
1555
|
+
const response = await res.json();
|
|
1556
|
+
if (!response || response.error_code != 0) {
|
|
1557
|
+
throw new CheckLanguageError(this.name, `Check language fail ! error_code:${response.error_code}, error_msg: ${response.error_msg} \n Go to https://fanyi-api.baidu.com/product/143 view details`);
|
|
1558
|
+
}
|
|
1559
|
+
return response.data.src;
|
|
1560
|
+
},
|
|
1465
1561
|
};
|
|
1466
1562
|
}
|
|
1467
1563
|
|
|
1468
|
-
function deepl$
|
|
1564
|
+
function deepl$3(options) {
|
|
1469
1565
|
const { key } = options;
|
|
1470
1566
|
const name = "deepl";
|
|
1471
1567
|
const checkOptions = () => {
|
|
@@ -1512,6 +1608,39 @@ function deepl$2(options) {
|
|
|
1512
1608
|
const translations = body.map((t) => t.text);
|
|
1513
1609
|
return translations;
|
|
1514
1610
|
},
|
|
1611
|
+
async checkLanguage(text) {
|
|
1612
|
+
checkOptions();
|
|
1613
|
+
const res = await fetch(url, {
|
|
1614
|
+
method: "POST",
|
|
1615
|
+
headers: {
|
|
1616
|
+
"Content-Type": "application/json; charset=UTF-8",
|
|
1617
|
+
Authorization: `DeepL-Auth-Key ${key}`,
|
|
1618
|
+
Accept: "*/*",
|
|
1619
|
+
Host: "api-free.deepl.com",
|
|
1620
|
+
Connection: "keep-alive",
|
|
1621
|
+
},
|
|
1622
|
+
body: JSON.stringify({
|
|
1623
|
+
text: [text],
|
|
1624
|
+
target_lang: "EN",
|
|
1625
|
+
}),
|
|
1626
|
+
});
|
|
1627
|
+
if (!res.ok) {
|
|
1628
|
+
throw await throwResponseError(this.name, res);
|
|
1629
|
+
}
|
|
1630
|
+
const bodyRes = await res.json();
|
|
1631
|
+
if (bodyRes.error) {
|
|
1632
|
+
throw new CheckLanguageError(this.name, `Check language fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
1633
|
+
}
|
|
1634
|
+
const body = bodyRes.translations;
|
|
1635
|
+
if (!body || body.length === 0) {
|
|
1636
|
+
throw new CheckLanguageError(this.name, "Check language fail! No result returned");
|
|
1637
|
+
}
|
|
1638
|
+
const detectedLanguage = body[0].detected_source_language;
|
|
1639
|
+
if (!detectedLanguage) {
|
|
1640
|
+
throw new CheckLanguageError(this.name, "Check language fail! Language not detected");
|
|
1641
|
+
}
|
|
1642
|
+
return detectedLanguage;
|
|
1643
|
+
},
|
|
1515
1644
|
};
|
|
1516
1645
|
}
|
|
1517
1646
|
|
|
@@ -1646,7 +1775,7 @@ function buildAuthorization({ secretId, secretKey, service, host, payload, httpR
|
|
|
1646
1775
|
headers["X-TC-Region"] = region;
|
|
1647
1776
|
return headers;
|
|
1648
1777
|
}
|
|
1649
|
-
function tencent$
|
|
1778
|
+
function tencent$3(options) {
|
|
1650
1779
|
const { secretId, secretKey, region = "ap-guangzhou" } = options;
|
|
1651
1780
|
const name = "tencent";
|
|
1652
1781
|
const host = "tmt.tencentcloudapi.com";
|
|
@@ -1694,7 +1823,7 @@ function tencent$2(options) {
|
|
|
1694
1823
|
}
|
|
1695
1824
|
const data = await res.json();
|
|
1696
1825
|
if (data.Response?.Error) {
|
|
1697
|
-
throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
|
|
1826
|
+
throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message} \n Go to https://cloud.tencent.com/document/product/551/15619 view details`);
|
|
1698
1827
|
}
|
|
1699
1828
|
const translatedResults = data.Response?.TargetText.split("\n") ?? [];
|
|
1700
1829
|
if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
|
|
@@ -1708,20 +1837,63 @@ function tencent$2(options) {
|
|
|
1708
1837
|
throw new TranslationError(name, `Translation failed: ${error}`);
|
|
1709
1838
|
}
|
|
1710
1839
|
},
|
|
1840
|
+
async checkLanguage(text) {
|
|
1841
|
+
checkOptions();
|
|
1842
|
+
const payloadObj = {
|
|
1843
|
+
Text: text,
|
|
1844
|
+
ProjectId: 0,
|
|
1845
|
+
};
|
|
1846
|
+
const payload = JSON.stringify(payloadObj);
|
|
1847
|
+
const headers = buildAuthorization({
|
|
1848
|
+
secretId,
|
|
1849
|
+
secretKey,
|
|
1850
|
+
service,
|
|
1851
|
+
host,
|
|
1852
|
+
payload,
|
|
1853
|
+
httpRequestMethod: "POST",
|
|
1854
|
+
action: "LanguageDetect",
|
|
1855
|
+
apiVersion,
|
|
1856
|
+
region,
|
|
1857
|
+
});
|
|
1858
|
+
try {
|
|
1859
|
+
const res = await fetch(endpoint, {
|
|
1860
|
+
method: "POST",
|
|
1861
|
+
headers,
|
|
1862
|
+
body: payload,
|
|
1863
|
+
});
|
|
1864
|
+
if (!res.ok) {
|
|
1865
|
+
throw new CheckLanguageError(name, `HTTP ${res.status}: ${await res.text()}`);
|
|
1866
|
+
}
|
|
1867
|
+
const data = await res.json();
|
|
1868
|
+
if (data.Response?.Error) {
|
|
1869
|
+
throw new CheckLanguageError(name, `Tencent language detect fail: ${data.Response.Error.Code}, ${data.Response.Error.Message} \n Go to https://cloud.tencent.com/document/product/551/15620 view details`);
|
|
1870
|
+
}
|
|
1871
|
+
const detectedLanguage = data.Response?.Lang;
|
|
1872
|
+
if (!detectedLanguage) {
|
|
1873
|
+
throw new CheckLanguageError(name, "Language detect fail! No result returned");
|
|
1874
|
+
}
|
|
1875
|
+
return detectedLanguage;
|
|
1876
|
+
}
|
|
1877
|
+
catch (error) {
|
|
1878
|
+
if (error instanceof CheckLanguageError)
|
|
1879
|
+
throw error;
|
|
1880
|
+
throw new CheckLanguageError(name, `Language detection failed: ${error}`);
|
|
1881
|
+
}
|
|
1882
|
+
},
|
|
1711
1883
|
};
|
|
1712
1884
|
}
|
|
1713
1885
|
|
|
1714
1886
|
const engines = {
|
|
1715
|
-
google,
|
|
1716
|
-
azure: azure$
|
|
1717
|
-
amazon: amazon$
|
|
1718
|
-
baidu: baidu$
|
|
1719
|
-
deepl: deepl$
|
|
1887
|
+
google: google$1,
|
|
1888
|
+
azure: azure$2,
|
|
1889
|
+
amazon: amazon$2,
|
|
1890
|
+
baidu: baidu$2,
|
|
1891
|
+
deepl: deepl$3,
|
|
1720
1892
|
openai: openai$1,
|
|
1721
|
-
tencent: tencent$
|
|
1893
|
+
tencent: tencent$3,
|
|
1722
1894
|
};
|
|
1723
1895
|
|
|
1724
|
-
var azure = {
|
|
1896
|
+
var azure$1 = {
|
|
1725
1897
|
Afrikaans: "af",
|
|
1726
1898
|
Albanian: "sq",
|
|
1727
1899
|
Amharic: "am",
|
|
@@ -2105,7 +2277,7 @@ var openai = {
|
|
|
2105
2277
|
Zulu: "zu",
|
|
2106
2278
|
};
|
|
2107
2279
|
|
|
2108
|
-
var baidu = {
|
|
2280
|
+
var baidu$1 = {
|
|
2109
2281
|
Achinese: "ach",
|
|
2110
2282
|
Afrikaans: "afr",
|
|
2111
2283
|
Akan: "aka",
|
|
@@ -2316,7 +2488,7 @@ var baidu = {
|
|
|
2316
2488
|
Zulu: "zul",
|
|
2317
2489
|
};
|
|
2318
2490
|
|
|
2319
|
-
var deepl$
|
|
2491
|
+
var deepl$2 = {
|
|
2320
2492
|
English: "en",
|
|
2321
2493
|
Bulgarian: "bg",
|
|
2322
2494
|
Chinese: "zh",
|
|
@@ -2352,7 +2524,7 @@ var deepl$1 = {
|
|
|
2352
2524
|
Ukrainian: "uk",
|
|
2353
2525
|
};
|
|
2354
2526
|
|
|
2355
|
-
var tencent$
|
|
2527
|
+
var tencent$2 = {
|
|
2356
2528
|
Auto: "auto",
|
|
2357
2529
|
Chinese: "zh",
|
|
2358
2530
|
TraditionalChinese: "zh-TW",
|
|
@@ -2374,7 +2546,7 @@ var tencent$1 = {
|
|
|
2374
2546
|
Hindi: "hi",
|
|
2375
2547
|
};
|
|
2376
2548
|
|
|
2377
|
-
var amazon = {
|
|
2549
|
+
var amazon$1 = {
|
|
2378
2550
|
Afrikaans: "af",
|
|
2379
2551
|
Albanian: "sq",
|
|
2380
2552
|
Amharic: "am",
|
|
@@ -2453,16 +2625,16 @@ var amazon = {
|
|
|
2453
2625
|
};
|
|
2454
2626
|
|
|
2455
2627
|
const originLanguages = {
|
|
2456
|
-
azure: azure,
|
|
2628
|
+
azure: azure$1,
|
|
2457
2629
|
google: openai,
|
|
2458
|
-
baidu: baidu,
|
|
2459
|
-
deepl: deepl$
|
|
2460
|
-
amazon: amazon,
|
|
2630
|
+
baidu: baidu$1,
|
|
2631
|
+
deepl: deepl$2,
|
|
2632
|
+
amazon: amazon$1,
|
|
2461
2633
|
openai: openai,
|
|
2462
|
-
tencent: tencent$
|
|
2634
|
+
tencent: tencent$2,
|
|
2463
2635
|
};
|
|
2464
2636
|
|
|
2465
|
-
var deepl = {
|
|
2637
|
+
var deepl$1 = {
|
|
2466
2638
|
"American English": "en-US",
|
|
2467
2639
|
"Brazilian Portuguese": "pt-BR",
|
|
2468
2640
|
"British English": "en-GB",
|
|
@@ -2501,7 +2673,7 @@ var deepl = {
|
|
|
2501
2673
|
Ukrainian: "uk",
|
|
2502
2674
|
};
|
|
2503
2675
|
|
|
2504
|
-
var tencent = {
|
|
2676
|
+
var tencent$1 = {
|
|
2505
2677
|
"Simplified Chinese": "zh",
|
|
2506
2678
|
"Traditional Chinese": "zh-TW",
|
|
2507
2679
|
English: "en",
|
|
@@ -2523,13 +2695,197 @@ var tencent = {
|
|
|
2523
2695
|
};
|
|
2524
2696
|
|
|
2525
2697
|
const targetLanguages = {
|
|
2526
|
-
azure: azure,
|
|
2698
|
+
azure: azure$1,
|
|
2527
2699
|
google: openai,
|
|
2528
|
-
baidu: baidu,
|
|
2529
|
-
deepl: deepl,
|
|
2530
|
-
amazon: amazon,
|
|
2700
|
+
baidu: baidu$1,
|
|
2701
|
+
deepl: deepl$1,
|
|
2702
|
+
amazon: amazon$1,
|
|
2531
2703
|
openai: openai,
|
|
2704
|
+
tencent: tencent$1,
|
|
2705
|
+
};
|
|
2706
|
+
|
|
2707
|
+
var baidu = {
|
|
2708
|
+
Chinese: "zh",
|
|
2709
|
+
English: "en",
|
|
2710
|
+
Japanese: "jp",
|
|
2711
|
+
Korean: "kor",
|
|
2712
|
+
Thai: "th",
|
|
2713
|
+
Russian: "ru",
|
|
2714
|
+
};
|
|
2715
|
+
|
|
2716
|
+
var tencent = {
|
|
2717
|
+
Chinese: "zh",
|
|
2718
|
+
English: "en",
|
|
2719
|
+
Japanese: "jp",
|
|
2720
|
+
Korean: "kr",
|
|
2721
|
+
German: "de",
|
|
2722
|
+
French: "fr",
|
|
2723
|
+
Spanish: "es",
|
|
2724
|
+
Italian: "it",
|
|
2725
|
+
Turkish: "tr",
|
|
2726
|
+
Russian: "ru",
|
|
2727
|
+
Portuguese: "pt",
|
|
2728
|
+
Vietnamese: "vi",
|
|
2729
|
+
Indonesian: "id",
|
|
2730
|
+
Malay: "ms",
|
|
2731
|
+
Thai: "th",
|
|
2732
|
+
};
|
|
2733
|
+
|
|
2734
|
+
var azure = {
|
|
2735
|
+
Arabic: "ar",
|
|
2736
|
+
Bengali: "bn",
|
|
2737
|
+
Bulgarian: "bg",
|
|
2738
|
+
Catalan: "ca",
|
|
2739
|
+
Chinese: "zh-Hans",
|
|
2740
|
+
"Chinese (Traditional)": "zh-Hant",
|
|
2741
|
+
Croatian: "hr",
|
|
2742
|
+
Czech: "cs",
|
|
2743
|
+
Danish: "da",
|
|
2744
|
+
Dutch: "nl",
|
|
2745
|
+
English: "en",
|
|
2746
|
+
Finnish: "fi",
|
|
2747
|
+
French: "fr",
|
|
2748
|
+
German: "de",
|
|
2749
|
+
Greek: "el",
|
|
2750
|
+
Hebrew: "he",
|
|
2751
|
+
Hindi: "hi",
|
|
2752
|
+
Hungarian: "hu",
|
|
2753
|
+
Indonesian: "id",
|
|
2754
|
+
Italian: "it",
|
|
2755
|
+
Japanese: "ja",
|
|
2756
|
+
Korean: "ko",
|
|
2757
|
+
Malay: "ms",
|
|
2758
|
+
"Norwegian Bokmål": "nb",
|
|
2759
|
+
Persian: "fa",
|
|
2760
|
+
Polish: "pl",
|
|
2761
|
+
"Portugal (Brazil)": "pt",
|
|
2762
|
+
Romanian: "ro",
|
|
2763
|
+
Russian: "ru",
|
|
2764
|
+
Slovak: "sk",
|
|
2765
|
+
Spanish: "es",
|
|
2766
|
+
Swedish: "sv",
|
|
2767
|
+
Thai: "th",
|
|
2768
|
+
Turkish: "tr",
|
|
2769
|
+
Ukrainian: "uk",
|
|
2770
|
+
Urdu: "ur",
|
|
2771
|
+
Vietnamese: "vi",
|
|
2772
|
+
};
|
|
2773
|
+
|
|
2774
|
+
var amazon = {
|
|
2775
|
+
Arabic: "ar",
|
|
2776
|
+
Bengali: "bn",
|
|
2777
|
+
Bulgarian: "bg",
|
|
2778
|
+
Catalan: "ca",
|
|
2779
|
+
Chinese: "zh-Hans",
|
|
2780
|
+
"Chinese (Traditional)": "zh-Hant",
|
|
2781
|
+
Croatian: "hr",
|
|
2782
|
+
Czech: "cs",
|
|
2783
|
+
Danish: "da",
|
|
2784
|
+
Dutch: "nl",
|
|
2785
|
+
English: "en",
|
|
2786
|
+
Finnish: "fi",
|
|
2787
|
+
French: "fr",
|
|
2788
|
+
German: "de",
|
|
2789
|
+
Greek: "el",
|
|
2790
|
+
Hebrew: "he",
|
|
2791
|
+
Hindi: "hi",
|
|
2792
|
+
Hungarian: "hu",
|
|
2793
|
+
Indonesian: "id",
|
|
2794
|
+
Italian: "it",
|
|
2795
|
+
Japanese: "ja",
|
|
2796
|
+
Korean: "ko",
|
|
2797
|
+
Malay: "ms",
|
|
2798
|
+
Persian: "fa",
|
|
2799
|
+
Polish: "pl",
|
|
2800
|
+
"Portugal (Brazil)": "pt",
|
|
2801
|
+
Romanian: "ro",
|
|
2802
|
+
Russian: "ru",
|
|
2803
|
+
Slovak: "sk",
|
|
2804
|
+
Spanish: "es",
|
|
2805
|
+
Swedish: "sv",
|
|
2806
|
+
Thai: "th",
|
|
2807
|
+
Turkish: "tr",
|
|
2808
|
+
Ukrainian: "uk",
|
|
2809
|
+
Urdu: "ur",
|
|
2810
|
+
Vietnamese: "vi",
|
|
2811
|
+
};
|
|
2812
|
+
|
|
2813
|
+
var deepl = {
|
|
2814
|
+
"American English": "en",
|
|
2815
|
+
Bulgarian: "bg",
|
|
2816
|
+
Chinese: "zh",
|
|
2817
|
+
Czech: "cs",
|
|
2818
|
+
Danish: "da",
|
|
2819
|
+
Dutch: "nl",
|
|
2820
|
+
Estonian: "et",
|
|
2821
|
+
Finnish: "fi",
|
|
2822
|
+
French: "fr",
|
|
2823
|
+
German: "de",
|
|
2824
|
+
Greek: "el",
|
|
2825
|
+
Hungarian: "hu",
|
|
2826
|
+
Icelandic: "is",
|
|
2827
|
+
Indonesian: "id",
|
|
2828
|
+
Irish: "ga",
|
|
2829
|
+
Italian: "it",
|
|
2830
|
+
Japanese: "ja",
|
|
2831
|
+
Korean: "ko",
|
|
2832
|
+
Latvian: "lv",
|
|
2833
|
+
Lithuanian: "lt",
|
|
2834
|
+
Maltese: "mt",
|
|
2835
|
+
Norwegian: "nb",
|
|
2836
|
+
Polish: "pl",
|
|
2837
|
+
Portuguese: "pt",
|
|
2838
|
+
Romanian: "ro",
|
|
2839
|
+
Russian: "ru",
|
|
2840
|
+
Slovak: "sk",
|
|
2841
|
+
Slovenian: "sl",
|
|
2842
|
+
Spanish: "es",
|
|
2843
|
+
Swedish: "sv",
|
|
2844
|
+
Turkish: "tr",
|
|
2845
|
+
Ukrainian: "uk",
|
|
2846
|
+
};
|
|
2847
|
+
|
|
2848
|
+
var google = {
|
|
2849
|
+
English: "en",
|
|
2850
|
+
Bulgarian: "bg",
|
|
2851
|
+
Chinese: "zh-cn",
|
|
2852
|
+
Czech: "cs",
|
|
2853
|
+
Danish: "da",
|
|
2854
|
+
Dutch: "nl",
|
|
2855
|
+
Estonian: "et",
|
|
2856
|
+
Finnish: "fi",
|
|
2857
|
+
French: "fr",
|
|
2858
|
+
German: "de",
|
|
2859
|
+
Greek: "el",
|
|
2860
|
+
Hungarian: "hu",
|
|
2861
|
+
Icelandic: "is",
|
|
2862
|
+
Indonesian: "id",
|
|
2863
|
+
Irish: "ga",
|
|
2864
|
+
Italian: "it",
|
|
2865
|
+
Japanese: "ja",
|
|
2866
|
+
Korean: "ko",
|
|
2867
|
+
Latvian: "lv",
|
|
2868
|
+
Lithuanian: "lt",
|
|
2869
|
+
Maltese: "mt",
|
|
2870
|
+
Polish: "pl",
|
|
2871
|
+
Portuguese: "pt",
|
|
2872
|
+
Romanian: "ro",
|
|
2873
|
+
Russian: "ru",
|
|
2874
|
+
Slovak: "sk",
|
|
2875
|
+
Slovenian: "sl",
|
|
2876
|
+
Spanish: "es",
|
|
2877
|
+
Swedish: "sv",
|
|
2878
|
+
Turkish: "tr",
|
|
2879
|
+
Ukrainian: "uk",
|
|
2880
|
+
};
|
|
2881
|
+
|
|
2882
|
+
const checkLanguages = {
|
|
2883
|
+
baidu: baidu,
|
|
2532
2884
|
tencent: tencent,
|
|
2885
|
+
azure: azure,
|
|
2886
|
+
amazon: amazon,
|
|
2887
|
+
deepl: deepl,
|
|
2888
|
+
google: google,
|
|
2533
2889
|
};
|
|
2534
2890
|
|
|
2535
2891
|
function normalFromLanguage(from, engine) {
|
|
@@ -2610,6 +2966,42 @@ class Translator {
|
|
|
2610
2966
|
}
|
|
2611
2967
|
this.engines.delete(engineName);
|
|
2612
2968
|
}
|
|
2969
|
+
isSupportCheckLanguage(engineName) {
|
|
2970
|
+
if (!engineName || !this.engines.has(engineName)) {
|
|
2971
|
+
logger.warn("Engine name is required or not found");
|
|
2972
|
+
return false;
|
|
2973
|
+
}
|
|
2974
|
+
const engine = this.engines.get(engineName);
|
|
2975
|
+
if (!engine) {
|
|
2976
|
+
logger.warn(`Engine ${engineName} not found`);
|
|
2977
|
+
return false;
|
|
2978
|
+
}
|
|
2979
|
+
return !!engine.checkLanguage;
|
|
2980
|
+
}
|
|
2981
|
+
async checkLanguage(text, options) {
|
|
2982
|
+
const { engine = "google", max_character_num = defaultMaxCharacterNum } = options;
|
|
2983
|
+
if (!this.engines.has(engine)) {
|
|
2984
|
+
throw new CheckLanguageError(appName, `Engine ${engine} not found`);
|
|
2985
|
+
}
|
|
2986
|
+
const engineInstance = this.engines.get(engine);
|
|
2987
|
+
if (!engineInstance) {
|
|
2988
|
+
throw new CheckLanguageError(appName, `Engine ${engine} is null`);
|
|
2989
|
+
}
|
|
2990
|
+
if (!engineInstance.checkLanguage) {
|
|
2991
|
+
throw new CheckLanguageError(appName, `Engine ${engine} does not support checkLanguage method`);
|
|
2992
|
+
}
|
|
2993
|
+
const sample = max_character_num > 0 ? text.substring(0, max_character_num) : text;
|
|
2994
|
+
if (sample.length <= 0) {
|
|
2995
|
+
throw new TranslationError(appName, "Text is empty or too short to check language");
|
|
2996
|
+
}
|
|
2997
|
+
return engineInstance
|
|
2998
|
+
.checkLanguage(sample)
|
|
2999
|
+
.then((r) => r)
|
|
3000
|
+
.catch((e) => {
|
|
3001
|
+
logger.error(`${appName} Failed to check language for text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${getErrorMessages(e)}`);
|
|
3002
|
+
throw e;
|
|
3003
|
+
});
|
|
3004
|
+
}
|
|
2613
3005
|
async translate(text, options) {
|
|
2614
3006
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2615
3007
|
let { from = "auto", to } = options;
|
|
@@ -2621,7 +3013,7 @@ class Translator {
|
|
|
2621
3013
|
}
|
|
2622
3014
|
const engineInstance = this.engines.get(engine);
|
|
2623
3015
|
if (!engineInstance) {
|
|
2624
|
-
throw new TranslationError(appName, `Engine ${engine}
|
|
3016
|
+
throw new TranslationError(appName, `Engine ${engine} is null`);
|
|
2625
3017
|
}
|
|
2626
3018
|
if (!from) {
|
|
2627
3019
|
throw new TranslationError(appName, `Invalid origin language ${from}`);
|
|
@@ -2709,11 +3101,17 @@ var index = {
|
|
|
2709
3101
|
};
|
|
2710
3102
|
|
|
2711
3103
|
exports.Cache = Cache;
|
|
3104
|
+
exports.CheckLanguageError = CheckLanguageError;
|
|
2712
3105
|
exports.OPEN_AI_MODELS = OPEN_AI_MODELS;
|
|
2713
3106
|
exports.TranslationError = TranslationError;
|
|
2714
3107
|
exports.Translator = Translator;
|
|
3108
|
+
exports.checkLanguages = checkLanguages;
|
|
2715
3109
|
exports.default = index;
|
|
2716
3110
|
exports.engines = engines;
|
|
2717
3111
|
exports.getLanguage = getLanguage;
|
|
3112
|
+
exports.normalFromLanguage = normalFromLanguage;
|
|
3113
|
+
exports.normalToLanguage = normalToLanguage;
|
|
3114
|
+
exports.originLanguages = originLanguages;
|
|
3115
|
+
exports.targetLanguages = targetLanguages;
|
|
2718
3116
|
exports.translator = translator;
|
|
2719
3117
|
//# sourceMappingURL=index.cjs.map
|