@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.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// translate v0.
|
|
1
|
+
// translate v0.4.0 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
import 'fs/promises';
|
|
3
3
|
import 'fs';
|
|
4
4
|
import 'path';
|
|
@@ -13,6 +13,14 @@ class TranslationError extends Error {
|
|
|
13
13
|
Error.captureStackTrace(this, this.constructor);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
class CheckLanguageError extends Error {
|
|
17
|
+
region;
|
|
18
|
+
constructor(region, message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.region = region;
|
|
21
|
+
Error.captureStackTrace(this, this.constructor);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
16
24
|
const OPEN_AI_MODELS = [
|
|
17
25
|
"o1-preview",
|
|
18
26
|
"o1-preview-2024-09-12",
|
|
@@ -198,7 +206,7 @@ function isOverMaxCharacterNum(text, max_character_num) {
|
|
|
198
206
|
return total > max_character_num;
|
|
199
207
|
}
|
|
200
208
|
|
|
201
|
-
function google(options) {
|
|
209
|
+
function google$1(options) {
|
|
202
210
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
203
211
|
return {
|
|
204
212
|
name: "google",
|
|
@@ -227,13 +235,29 @@ function google(options) {
|
|
|
227
235
|
}
|
|
228
236
|
return translations;
|
|
229
237
|
},
|
|
238
|
+
async checkLanguage(text) {
|
|
239
|
+
const url = `${base}?client=gtx&sl=auto&tl=en&dt=t&q=${encodeURI(text)}`;
|
|
240
|
+
const res = await fetch(url);
|
|
241
|
+
if (!res.ok) {
|
|
242
|
+
throw await throwResponseError(this.name, res);
|
|
243
|
+
}
|
|
244
|
+
const body = await res.json();
|
|
245
|
+
if (!body || body.length < 3) {
|
|
246
|
+
throw new CheckLanguageError(this.name, "Check language fail! No result returned");
|
|
247
|
+
}
|
|
248
|
+
const detectedLanguage = body[2];
|
|
249
|
+
if (!detectedLanguage) {
|
|
250
|
+
throw new CheckLanguageError(this.name, "Check language fail! Language not detected");
|
|
251
|
+
}
|
|
252
|
+
return detectedLanguage;
|
|
253
|
+
},
|
|
230
254
|
};
|
|
231
255
|
}
|
|
232
256
|
|
|
233
257
|
/**
|
|
234
258
|
* Azure translate documentation: https://learn.microsoft.com/zh-cn/azure/ai-services/translator/reference/v3-0-translate
|
|
235
259
|
*/
|
|
236
|
-
function azure$
|
|
260
|
+
function azure$2(options) {
|
|
237
261
|
const { key, region } = options;
|
|
238
262
|
const name = "azure";
|
|
239
263
|
const checkOptions = () => {
|
|
@@ -266,7 +290,7 @@ function azure$1(options) {
|
|
|
266
290
|
}
|
|
267
291
|
const bodyRes = await res.json();
|
|
268
292
|
if (bodyRes.error) {
|
|
269
|
-
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
293
|
+
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`);
|
|
270
294
|
}
|
|
271
295
|
const body = bodyRes;
|
|
272
296
|
if (!body || body.length === 0) {
|
|
@@ -281,10 +305,35 @@ function azure$1(options) {
|
|
|
281
305
|
}
|
|
282
306
|
return translations;
|
|
283
307
|
},
|
|
308
|
+
async checkLanguage(text) {
|
|
309
|
+
checkOptions();
|
|
310
|
+
const detectUrl = "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0";
|
|
311
|
+
const res = await fetch(detectUrl, {
|
|
312
|
+
method: "POST",
|
|
313
|
+
headers: {
|
|
314
|
+
"Content-Type": "application/json; charset=UTF-8",
|
|
315
|
+
"Ocp-Apim-Subscription-Key": key,
|
|
316
|
+
"Ocp-Apim-Subscription-Region": region,
|
|
317
|
+
},
|
|
318
|
+
body: JSON.stringify([{ Text: text }]),
|
|
319
|
+
});
|
|
320
|
+
if (!res.ok) {
|
|
321
|
+
throw await throwResponseError(this.name, res);
|
|
322
|
+
}
|
|
323
|
+
const response = await res.json();
|
|
324
|
+
if (!response || !Array.isArray(response) || response.length === 0) {
|
|
325
|
+
throw new TranslationError(this.name, "Check language fail! No result returned");
|
|
326
|
+
}
|
|
327
|
+
const detection = response[0];
|
|
328
|
+
if (!detection.language) {
|
|
329
|
+
throw new TranslationError(this.name, "Check language fail! Language not detected");
|
|
330
|
+
}
|
|
331
|
+
return detection.language;
|
|
332
|
+
},
|
|
284
333
|
};
|
|
285
334
|
}
|
|
286
335
|
|
|
287
|
-
function amazon$
|
|
336
|
+
function amazon$2(options) {
|
|
288
337
|
const { region, accessKeyId, secretAccessKey } = options;
|
|
289
338
|
const name = "amazon";
|
|
290
339
|
const checkOptions = () => {
|
|
@@ -315,6 +364,28 @@ function amazon$1(options) {
|
|
|
315
364
|
}
|
|
316
365
|
return translations;
|
|
317
366
|
},
|
|
367
|
+
async checkLanguage(text) {
|
|
368
|
+
checkOptions();
|
|
369
|
+
const translateClient = new TranslateClient({
|
|
370
|
+
region: region,
|
|
371
|
+
credentials: { accessKeyId, secretAccessKey },
|
|
372
|
+
});
|
|
373
|
+
const command = new TranslateTextCommand({
|
|
374
|
+
SourceLanguageCode: "auto",
|
|
375
|
+
TargetLanguageCode: "en",
|
|
376
|
+
Text: text,
|
|
377
|
+
});
|
|
378
|
+
try {
|
|
379
|
+
const response = await translateClient.send(command);
|
|
380
|
+
if (!response.SourceLanguageCode) {
|
|
381
|
+
throw new TranslationError(name, "Check language fail! Source language not detected");
|
|
382
|
+
}
|
|
383
|
+
return response.SourceLanguageCode;
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
throw new TranslationError(name, `Check language fail! ${error.message || error}`);
|
|
387
|
+
}
|
|
388
|
+
},
|
|
318
389
|
};
|
|
319
390
|
}
|
|
320
391
|
|
|
@@ -1407,7 +1478,7 @@ function requireCore () {
|
|
|
1407
1478
|
var md5Exports = md5$1.exports;
|
|
1408
1479
|
var md5 = /*@__PURE__*/getDefaultExportFromCjs(md5Exports);
|
|
1409
1480
|
|
|
1410
|
-
function baidu$
|
|
1481
|
+
function baidu$2(options) {
|
|
1411
1482
|
const { appId, secretKey } = options;
|
|
1412
1483
|
const url = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate";
|
|
1413
1484
|
const name = "baidu";
|
|
@@ -1448,7 +1519,7 @@ function baidu$1(options) {
|
|
|
1448
1519
|
}
|
|
1449
1520
|
const data = await res.json();
|
|
1450
1521
|
if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
|
|
1451
|
-
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
|
|
1522
|
+
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`);
|
|
1452
1523
|
}
|
|
1453
1524
|
const translations = [];
|
|
1454
1525
|
for (const translation of data.trans_result) {
|
|
@@ -1458,10 +1529,35 @@ function baidu$1(options) {
|
|
|
1458
1529
|
}
|
|
1459
1530
|
return translations;
|
|
1460
1531
|
},
|
|
1532
|
+
async checkLanguage(text) {
|
|
1533
|
+
checkOptions();
|
|
1534
|
+
const salt = Date.now();
|
|
1535
|
+
const sign = md5(`${appId}${text}${salt}${secretKey}`).toString();
|
|
1536
|
+
const body = new URLSearchParams();
|
|
1537
|
+
body.append("q", text);
|
|
1538
|
+
body.append("appid", appId);
|
|
1539
|
+
body.append("salt", salt.toString());
|
|
1540
|
+
body.append("sign", sign);
|
|
1541
|
+
const res = await fetch("https://fanyi-api.baidu.com/api/trans/vip/language", {
|
|
1542
|
+
method: "POST",
|
|
1543
|
+
headers: {
|
|
1544
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
1545
|
+
},
|
|
1546
|
+
body: body.toString(),
|
|
1547
|
+
});
|
|
1548
|
+
if (!res.ok) {
|
|
1549
|
+
throw await throwResponseError(this.name, res);
|
|
1550
|
+
}
|
|
1551
|
+
const response = await res.json();
|
|
1552
|
+
if (!response || response.error_code != 0) {
|
|
1553
|
+
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`);
|
|
1554
|
+
}
|
|
1555
|
+
return response.data.src;
|
|
1556
|
+
},
|
|
1461
1557
|
};
|
|
1462
1558
|
}
|
|
1463
1559
|
|
|
1464
|
-
function deepl$
|
|
1560
|
+
function deepl$3(options) {
|
|
1465
1561
|
const { key } = options;
|
|
1466
1562
|
const name = "deepl";
|
|
1467
1563
|
const checkOptions = () => {
|
|
@@ -1508,6 +1604,39 @@ function deepl$2(options) {
|
|
|
1508
1604
|
const translations = body.map((t) => t.text);
|
|
1509
1605
|
return translations;
|
|
1510
1606
|
},
|
|
1607
|
+
async checkLanguage(text) {
|
|
1608
|
+
checkOptions();
|
|
1609
|
+
const res = await fetch(url, {
|
|
1610
|
+
method: "POST",
|
|
1611
|
+
headers: {
|
|
1612
|
+
"Content-Type": "application/json; charset=UTF-8",
|
|
1613
|
+
Authorization: `DeepL-Auth-Key ${key}`,
|
|
1614
|
+
Accept: "*/*",
|
|
1615
|
+
Host: "api-free.deepl.com",
|
|
1616
|
+
Connection: "keep-alive",
|
|
1617
|
+
},
|
|
1618
|
+
body: JSON.stringify({
|
|
1619
|
+
text: [text],
|
|
1620
|
+
target_lang: "EN",
|
|
1621
|
+
}),
|
|
1622
|
+
});
|
|
1623
|
+
if (!res.ok) {
|
|
1624
|
+
throw await throwResponseError(this.name, res);
|
|
1625
|
+
}
|
|
1626
|
+
const bodyRes = await res.json();
|
|
1627
|
+
if (bodyRes.error) {
|
|
1628
|
+
throw new CheckLanguageError(this.name, `Check language fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
1629
|
+
}
|
|
1630
|
+
const body = bodyRes.translations;
|
|
1631
|
+
if (!body || body.length === 0) {
|
|
1632
|
+
throw new CheckLanguageError(this.name, "Check language fail! No result returned");
|
|
1633
|
+
}
|
|
1634
|
+
const detectedLanguage = body[0].detected_source_language;
|
|
1635
|
+
if (!detectedLanguage) {
|
|
1636
|
+
throw new CheckLanguageError(this.name, "Check language fail! Language not detected");
|
|
1637
|
+
}
|
|
1638
|
+
return detectedLanguage;
|
|
1639
|
+
},
|
|
1511
1640
|
};
|
|
1512
1641
|
}
|
|
1513
1642
|
|
|
@@ -1642,7 +1771,7 @@ function buildAuthorization({ secretId, secretKey, service, host, payload, httpR
|
|
|
1642
1771
|
headers["X-TC-Region"] = region;
|
|
1643
1772
|
return headers;
|
|
1644
1773
|
}
|
|
1645
|
-
function tencent$
|
|
1774
|
+
function tencent$3(options) {
|
|
1646
1775
|
const { secretId, secretKey, region = "ap-guangzhou" } = options;
|
|
1647
1776
|
const name = "tencent";
|
|
1648
1777
|
const host = "tmt.tencentcloudapi.com";
|
|
@@ -1690,7 +1819,7 @@ function tencent$2(options) {
|
|
|
1690
1819
|
}
|
|
1691
1820
|
const data = await res.json();
|
|
1692
1821
|
if (data.Response?.Error) {
|
|
1693
|
-
throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
|
|
1822
|
+
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`);
|
|
1694
1823
|
}
|
|
1695
1824
|
const translatedResults = data.Response?.TargetText.split("\n") ?? [];
|
|
1696
1825
|
if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
|
|
@@ -1704,20 +1833,63 @@ function tencent$2(options) {
|
|
|
1704
1833
|
throw new TranslationError(name, `Translation failed: ${error}`);
|
|
1705
1834
|
}
|
|
1706
1835
|
},
|
|
1836
|
+
async checkLanguage(text) {
|
|
1837
|
+
checkOptions();
|
|
1838
|
+
const payloadObj = {
|
|
1839
|
+
Text: text,
|
|
1840
|
+
ProjectId: 0,
|
|
1841
|
+
};
|
|
1842
|
+
const payload = JSON.stringify(payloadObj);
|
|
1843
|
+
const headers = buildAuthorization({
|
|
1844
|
+
secretId,
|
|
1845
|
+
secretKey,
|
|
1846
|
+
service,
|
|
1847
|
+
host,
|
|
1848
|
+
payload,
|
|
1849
|
+
httpRequestMethod: "POST",
|
|
1850
|
+
action: "LanguageDetect",
|
|
1851
|
+
apiVersion,
|
|
1852
|
+
region,
|
|
1853
|
+
});
|
|
1854
|
+
try {
|
|
1855
|
+
const res = await fetch(endpoint, {
|
|
1856
|
+
method: "POST",
|
|
1857
|
+
headers,
|
|
1858
|
+
body: payload,
|
|
1859
|
+
});
|
|
1860
|
+
if (!res.ok) {
|
|
1861
|
+
throw new CheckLanguageError(name, `HTTP ${res.status}: ${await res.text()}`);
|
|
1862
|
+
}
|
|
1863
|
+
const data = await res.json();
|
|
1864
|
+
if (data.Response?.Error) {
|
|
1865
|
+
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`);
|
|
1866
|
+
}
|
|
1867
|
+
const detectedLanguage = data.Response?.Lang;
|
|
1868
|
+
if (!detectedLanguage) {
|
|
1869
|
+
throw new CheckLanguageError(name, "Language detect fail! No result returned");
|
|
1870
|
+
}
|
|
1871
|
+
return detectedLanguage;
|
|
1872
|
+
}
|
|
1873
|
+
catch (error) {
|
|
1874
|
+
if (error instanceof CheckLanguageError)
|
|
1875
|
+
throw error;
|
|
1876
|
+
throw new CheckLanguageError(name, `Language detection failed: ${error}`);
|
|
1877
|
+
}
|
|
1878
|
+
},
|
|
1707
1879
|
};
|
|
1708
1880
|
}
|
|
1709
1881
|
|
|
1710
1882
|
const engines = {
|
|
1711
|
-
google,
|
|
1712
|
-
azure: azure$
|
|
1713
|
-
amazon: amazon$
|
|
1714
|
-
baidu: baidu$
|
|
1715
|
-
deepl: deepl$
|
|
1883
|
+
google: google$1,
|
|
1884
|
+
azure: azure$2,
|
|
1885
|
+
amazon: amazon$2,
|
|
1886
|
+
baidu: baidu$2,
|
|
1887
|
+
deepl: deepl$3,
|
|
1716
1888
|
openai: openai$1,
|
|
1717
|
-
tencent: tencent$
|
|
1889
|
+
tencent: tencent$3,
|
|
1718
1890
|
};
|
|
1719
1891
|
|
|
1720
|
-
var azure = {
|
|
1892
|
+
var azure$1 = {
|
|
1721
1893
|
Afrikaans: "af",
|
|
1722
1894
|
Albanian: "sq",
|
|
1723
1895
|
Amharic: "am",
|
|
@@ -2101,7 +2273,7 @@ var openai = {
|
|
|
2101
2273
|
Zulu: "zu",
|
|
2102
2274
|
};
|
|
2103
2275
|
|
|
2104
|
-
var baidu = {
|
|
2276
|
+
var baidu$1 = {
|
|
2105
2277
|
Achinese: "ach",
|
|
2106
2278
|
Afrikaans: "afr",
|
|
2107
2279
|
Akan: "aka",
|
|
@@ -2312,7 +2484,7 @@ var baidu = {
|
|
|
2312
2484
|
Zulu: "zul",
|
|
2313
2485
|
};
|
|
2314
2486
|
|
|
2315
|
-
var deepl$
|
|
2487
|
+
var deepl$2 = {
|
|
2316
2488
|
English: "en",
|
|
2317
2489
|
Bulgarian: "bg",
|
|
2318
2490
|
Chinese: "zh",
|
|
@@ -2348,7 +2520,7 @@ var deepl$1 = {
|
|
|
2348
2520
|
Ukrainian: "uk",
|
|
2349
2521
|
};
|
|
2350
2522
|
|
|
2351
|
-
var tencent$
|
|
2523
|
+
var tencent$2 = {
|
|
2352
2524
|
Auto: "auto",
|
|
2353
2525
|
Chinese: "zh",
|
|
2354
2526
|
TraditionalChinese: "zh-TW",
|
|
@@ -2370,7 +2542,7 @@ var tencent$1 = {
|
|
|
2370
2542
|
Hindi: "hi",
|
|
2371
2543
|
};
|
|
2372
2544
|
|
|
2373
|
-
var amazon = {
|
|
2545
|
+
var amazon$1 = {
|
|
2374
2546
|
Afrikaans: "af",
|
|
2375
2547
|
Albanian: "sq",
|
|
2376
2548
|
Amharic: "am",
|
|
@@ -2449,16 +2621,16 @@ var amazon = {
|
|
|
2449
2621
|
};
|
|
2450
2622
|
|
|
2451
2623
|
const originLanguages = {
|
|
2452
|
-
azure: azure,
|
|
2624
|
+
azure: azure$1,
|
|
2453
2625
|
google: openai,
|
|
2454
|
-
baidu: baidu,
|
|
2455
|
-
deepl: deepl$
|
|
2456
|
-
amazon: amazon,
|
|
2626
|
+
baidu: baidu$1,
|
|
2627
|
+
deepl: deepl$2,
|
|
2628
|
+
amazon: amazon$1,
|
|
2457
2629
|
openai: openai,
|
|
2458
|
-
tencent: tencent$
|
|
2630
|
+
tencent: tencent$2,
|
|
2459
2631
|
};
|
|
2460
2632
|
|
|
2461
|
-
var deepl = {
|
|
2633
|
+
var deepl$1 = {
|
|
2462
2634
|
"American English": "en-US",
|
|
2463
2635
|
"Brazilian Portuguese": "pt-BR",
|
|
2464
2636
|
"British English": "en-GB",
|
|
@@ -2497,7 +2669,7 @@ var deepl = {
|
|
|
2497
2669
|
Ukrainian: "uk",
|
|
2498
2670
|
};
|
|
2499
2671
|
|
|
2500
|
-
var tencent = {
|
|
2672
|
+
var tencent$1 = {
|
|
2501
2673
|
"Simplified Chinese": "zh",
|
|
2502
2674
|
"Traditional Chinese": "zh-TW",
|
|
2503
2675
|
English: "en",
|
|
@@ -2519,13 +2691,197 @@ var tencent = {
|
|
|
2519
2691
|
};
|
|
2520
2692
|
|
|
2521
2693
|
const targetLanguages = {
|
|
2522
|
-
azure: azure,
|
|
2694
|
+
azure: azure$1,
|
|
2523
2695
|
google: openai,
|
|
2524
|
-
baidu: baidu,
|
|
2525
|
-
deepl: deepl,
|
|
2526
|
-
amazon: amazon,
|
|
2696
|
+
baidu: baidu$1,
|
|
2697
|
+
deepl: deepl$1,
|
|
2698
|
+
amazon: amazon$1,
|
|
2527
2699
|
openai: openai,
|
|
2700
|
+
tencent: tencent$1,
|
|
2701
|
+
};
|
|
2702
|
+
|
|
2703
|
+
var baidu = {
|
|
2704
|
+
Chinese: "zh",
|
|
2705
|
+
English: "en",
|
|
2706
|
+
Japanese: "jp",
|
|
2707
|
+
Korean: "kor",
|
|
2708
|
+
Thai: "th",
|
|
2709
|
+
Russian: "ru",
|
|
2710
|
+
};
|
|
2711
|
+
|
|
2712
|
+
var tencent = {
|
|
2713
|
+
Chinese: "zh",
|
|
2714
|
+
English: "en",
|
|
2715
|
+
Japanese: "jp",
|
|
2716
|
+
Korean: "kr",
|
|
2717
|
+
German: "de",
|
|
2718
|
+
French: "fr",
|
|
2719
|
+
Spanish: "es",
|
|
2720
|
+
Italian: "it",
|
|
2721
|
+
Turkish: "tr",
|
|
2722
|
+
Russian: "ru",
|
|
2723
|
+
Portuguese: "pt",
|
|
2724
|
+
Vietnamese: "vi",
|
|
2725
|
+
Indonesian: "id",
|
|
2726
|
+
Malay: "ms",
|
|
2727
|
+
Thai: "th",
|
|
2728
|
+
};
|
|
2729
|
+
|
|
2730
|
+
var azure = {
|
|
2731
|
+
Arabic: "ar",
|
|
2732
|
+
Bengali: "bn",
|
|
2733
|
+
Bulgarian: "bg",
|
|
2734
|
+
Catalan: "ca",
|
|
2735
|
+
Chinese: "zh-Hans",
|
|
2736
|
+
"Chinese (Traditional)": "zh-Hant",
|
|
2737
|
+
Croatian: "hr",
|
|
2738
|
+
Czech: "cs",
|
|
2739
|
+
Danish: "da",
|
|
2740
|
+
Dutch: "nl",
|
|
2741
|
+
English: "en",
|
|
2742
|
+
Finnish: "fi",
|
|
2743
|
+
French: "fr",
|
|
2744
|
+
German: "de",
|
|
2745
|
+
Greek: "el",
|
|
2746
|
+
Hebrew: "he",
|
|
2747
|
+
Hindi: "hi",
|
|
2748
|
+
Hungarian: "hu",
|
|
2749
|
+
Indonesian: "id",
|
|
2750
|
+
Italian: "it",
|
|
2751
|
+
Japanese: "ja",
|
|
2752
|
+
Korean: "ko",
|
|
2753
|
+
Malay: "ms",
|
|
2754
|
+
"Norwegian Bokmål": "nb",
|
|
2755
|
+
Persian: "fa",
|
|
2756
|
+
Polish: "pl",
|
|
2757
|
+
"Portugal (Brazil)": "pt",
|
|
2758
|
+
Romanian: "ro",
|
|
2759
|
+
Russian: "ru",
|
|
2760
|
+
Slovak: "sk",
|
|
2761
|
+
Spanish: "es",
|
|
2762
|
+
Swedish: "sv",
|
|
2763
|
+
Thai: "th",
|
|
2764
|
+
Turkish: "tr",
|
|
2765
|
+
Ukrainian: "uk",
|
|
2766
|
+
Urdu: "ur",
|
|
2767
|
+
Vietnamese: "vi",
|
|
2768
|
+
};
|
|
2769
|
+
|
|
2770
|
+
var amazon = {
|
|
2771
|
+
Arabic: "ar",
|
|
2772
|
+
Bengali: "bn",
|
|
2773
|
+
Bulgarian: "bg",
|
|
2774
|
+
Catalan: "ca",
|
|
2775
|
+
Chinese: "zh-Hans",
|
|
2776
|
+
"Chinese (Traditional)": "zh-Hant",
|
|
2777
|
+
Croatian: "hr",
|
|
2778
|
+
Czech: "cs",
|
|
2779
|
+
Danish: "da",
|
|
2780
|
+
Dutch: "nl",
|
|
2781
|
+
English: "en",
|
|
2782
|
+
Finnish: "fi",
|
|
2783
|
+
French: "fr",
|
|
2784
|
+
German: "de",
|
|
2785
|
+
Greek: "el",
|
|
2786
|
+
Hebrew: "he",
|
|
2787
|
+
Hindi: "hi",
|
|
2788
|
+
Hungarian: "hu",
|
|
2789
|
+
Indonesian: "id",
|
|
2790
|
+
Italian: "it",
|
|
2791
|
+
Japanese: "ja",
|
|
2792
|
+
Korean: "ko",
|
|
2793
|
+
Malay: "ms",
|
|
2794
|
+
Persian: "fa",
|
|
2795
|
+
Polish: "pl",
|
|
2796
|
+
"Portugal (Brazil)": "pt",
|
|
2797
|
+
Romanian: "ro",
|
|
2798
|
+
Russian: "ru",
|
|
2799
|
+
Slovak: "sk",
|
|
2800
|
+
Spanish: "es",
|
|
2801
|
+
Swedish: "sv",
|
|
2802
|
+
Thai: "th",
|
|
2803
|
+
Turkish: "tr",
|
|
2804
|
+
Ukrainian: "uk",
|
|
2805
|
+
Urdu: "ur",
|
|
2806
|
+
Vietnamese: "vi",
|
|
2807
|
+
};
|
|
2808
|
+
|
|
2809
|
+
var deepl = {
|
|
2810
|
+
"American English": "en",
|
|
2811
|
+
Bulgarian: "bg",
|
|
2812
|
+
Chinese: "zh",
|
|
2813
|
+
Czech: "cs",
|
|
2814
|
+
Danish: "da",
|
|
2815
|
+
Dutch: "nl",
|
|
2816
|
+
Estonian: "et",
|
|
2817
|
+
Finnish: "fi",
|
|
2818
|
+
French: "fr",
|
|
2819
|
+
German: "de",
|
|
2820
|
+
Greek: "el",
|
|
2821
|
+
Hungarian: "hu",
|
|
2822
|
+
Icelandic: "is",
|
|
2823
|
+
Indonesian: "id",
|
|
2824
|
+
Irish: "ga",
|
|
2825
|
+
Italian: "it",
|
|
2826
|
+
Japanese: "ja",
|
|
2827
|
+
Korean: "ko",
|
|
2828
|
+
Latvian: "lv",
|
|
2829
|
+
Lithuanian: "lt",
|
|
2830
|
+
Maltese: "mt",
|
|
2831
|
+
Norwegian: "nb",
|
|
2832
|
+
Polish: "pl",
|
|
2833
|
+
Portuguese: "pt",
|
|
2834
|
+
Romanian: "ro",
|
|
2835
|
+
Russian: "ru",
|
|
2836
|
+
Slovak: "sk",
|
|
2837
|
+
Slovenian: "sl",
|
|
2838
|
+
Spanish: "es",
|
|
2839
|
+
Swedish: "sv",
|
|
2840
|
+
Turkish: "tr",
|
|
2841
|
+
Ukrainian: "uk",
|
|
2842
|
+
};
|
|
2843
|
+
|
|
2844
|
+
var google = {
|
|
2845
|
+
English: "en",
|
|
2846
|
+
Bulgarian: "bg",
|
|
2847
|
+
Chinese: "zh-cn",
|
|
2848
|
+
Czech: "cs",
|
|
2849
|
+
Danish: "da",
|
|
2850
|
+
Dutch: "nl",
|
|
2851
|
+
Estonian: "et",
|
|
2852
|
+
Finnish: "fi",
|
|
2853
|
+
French: "fr",
|
|
2854
|
+
German: "de",
|
|
2855
|
+
Greek: "el",
|
|
2856
|
+
Hungarian: "hu",
|
|
2857
|
+
Icelandic: "is",
|
|
2858
|
+
Indonesian: "id",
|
|
2859
|
+
Irish: "ga",
|
|
2860
|
+
Italian: "it",
|
|
2861
|
+
Japanese: "ja",
|
|
2862
|
+
Korean: "ko",
|
|
2863
|
+
Latvian: "lv",
|
|
2864
|
+
Lithuanian: "lt",
|
|
2865
|
+
Maltese: "mt",
|
|
2866
|
+
Polish: "pl",
|
|
2867
|
+
Portuguese: "pt",
|
|
2868
|
+
Romanian: "ro",
|
|
2869
|
+
Russian: "ru",
|
|
2870
|
+
Slovak: "sk",
|
|
2871
|
+
Slovenian: "sl",
|
|
2872
|
+
Spanish: "es",
|
|
2873
|
+
Swedish: "sv",
|
|
2874
|
+
Turkish: "tr",
|
|
2875
|
+
Ukrainian: "uk",
|
|
2876
|
+
};
|
|
2877
|
+
|
|
2878
|
+
const checkLanguages = {
|
|
2879
|
+
baidu: baidu,
|
|
2528
2880
|
tencent: tencent,
|
|
2881
|
+
azure: azure,
|
|
2882
|
+
amazon: amazon,
|
|
2883
|
+
deepl: deepl,
|
|
2884
|
+
google: google,
|
|
2529
2885
|
};
|
|
2530
2886
|
|
|
2531
2887
|
function normalFromLanguage(from, engine) {
|
|
@@ -2606,6 +2962,42 @@ class Translator {
|
|
|
2606
2962
|
}
|
|
2607
2963
|
this.engines.delete(engineName);
|
|
2608
2964
|
}
|
|
2965
|
+
isSupportCheckLanguage(engineName) {
|
|
2966
|
+
if (!engineName || !this.engines.has(engineName)) {
|
|
2967
|
+
logger.warn("Engine name is required or not found");
|
|
2968
|
+
return false;
|
|
2969
|
+
}
|
|
2970
|
+
const engine = this.engines.get(engineName);
|
|
2971
|
+
if (!engine) {
|
|
2972
|
+
logger.warn(`Engine ${engineName} not found`);
|
|
2973
|
+
return false;
|
|
2974
|
+
}
|
|
2975
|
+
return !!engine.checkLanguage;
|
|
2976
|
+
}
|
|
2977
|
+
async checkLanguage(text, options) {
|
|
2978
|
+
const { engine = "google", max_character_num = defaultMaxCharacterNum } = options;
|
|
2979
|
+
if (!this.engines.has(engine)) {
|
|
2980
|
+
throw new CheckLanguageError(appName, `Engine ${engine} not found`);
|
|
2981
|
+
}
|
|
2982
|
+
const engineInstance = this.engines.get(engine);
|
|
2983
|
+
if (!engineInstance) {
|
|
2984
|
+
throw new CheckLanguageError(appName, `Engine ${engine} is null`);
|
|
2985
|
+
}
|
|
2986
|
+
if (!engineInstance.checkLanguage) {
|
|
2987
|
+
throw new CheckLanguageError(appName, `Engine ${engine} does not support checkLanguage method`);
|
|
2988
|
+
}
|
|
2989
|
+
const sample = max_character_num > 0 ? text.substring(0, max_character_num) : text;
|
|
2990
|
+
if (sample.length <= 0) {
|
|
2991
|
+
throw new TranslationError(appName, "Text is empty or too short to check language");
|
|
2992
|
+
}
|
|
2993
|
+
return engineInstance
|
|
2994
|
+
.checkLanguage(sample)
|
|
2995
|
+
.then((r) => r)
|
|
2996
|
+
.catch((e) => {
|
|
2997
|
+
logger.error(`${appName} Failed to check language for text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${getErrorMessages(e)}`);
|
|
2998
|
+
throw e;
|
|
2999
|
+
});
|
|
3000
|
+
}
|
|
2609
3001
|
async translate(text, options) {
|
|
2610
3002
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2611
3003
|
let { from = "auto", to } = options;
|
|
@@ -2617,7 +3009,7 @@ class Translator {
|
|
|
2617
3009
|
}
|
|
2618
3010
|
const engineInstance = this.engines.get(engine);
|
|
2619
3011
|
if (!engineInstance) {
|
|
2620
|
-
throw new TranslationError(appName, `Engine ${engine}
|
|
3012
|
+
throw new TranslationError(appName, `Engine ${engine} is null`);
|
|
2621
3013
|
}
|
|
2622
3014
|
if (!from) {
|
|
2623
3015
|
throw new TranslationError(appName, `Invalid origin language ${from}`);
|
|
@@ -2704,5 +3096,5 @@ var index = {
|
|
|
2704
3096
|
getLanguage,
|
|
2705
3097
|
};
|
|
2706
3098
|
|
|
2707
|
-
export { Cache, OPEN_AI_MODELS, TranslationError, Translator, index as default, engines, getLanguage, translator };
|
|
3099
|
+
export { Cache, CheckLanguageError, OPEN_AI_MODELS, TranslationError, Translator, checkLanguages, index as default, engines, getLanguage, normalFromLanguage, normalToLanguage, originLanguages, targetLanguages, translator };
|
|
2708
3100
|
//# sourceMappingURL=index.js.map
|