@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/browser/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
|
|
|
@@ -1445,7 +1516,7 @@ function requireCore () {
|
|
|
1445
1516
|
var md5Exports = md5$1.exports;
|
|
1446
1517
|
var md5 = /*@__PURE__*/getDefaultExportFromCjs(md5Exports);
|
|
1447
1518
|
|
|
1448
|
-
function baidu$
|
|
1519
|
+
function baidu$2(options) {
|
|
1449
1520
|
const { appId, secretKey } = options;
|
|
1450
1521
|
const url = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate";
|
|
1451
1522
|
const name = "baidu";
|
|
@@ -1486,7 +1557,7 @@ function baidu$1(options) {
|
|
|
1486
1557
|
}
|
|
1487
1558
|
const data = await res.json();
|
|
1488
1559
|
if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
|
|
1489
|
-
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
|
|
1560
|
+
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`);
|
|
1490
1561
|
}
|
|
1491
1562
|
const translations = [];
|
|
1492
1563
|
for (const translation of data.trans_result) {
|
|
@@ -1496,10 +1567,35 @@ function baidu$1(options) {
|
|
|
1496
1567
|
}
|
|
1497
1568
|
return translations;
|
|
1498
1569
|
},
|
|
1570
|
+
async checkLanguage(text) {
|
|
1571
|
+
checkOptions();
|
|
1572
|
+
const salt = Date.now();
|
|
1573
|
+
const sign = md5(`${appId}${text}${salt}${secretKey}`).toString();
|
|
1574
|
+
const body = new URLSearchParams();
|
|
1575
|
+
body.append("q", text);
|
|
1576
|
+
body.append("appid", appId);
|
|
1577
|
+
body.append("salt", salt.toString());
|
|
1578
|
+
body.append("sign", sign);
|
|
1579
|
+
const res = await fetch("https://fanyi-api.baidu.com/api/trans/vip/language", {
|
|
1580
|
+
method: "POST",
|
|
1581
|
+
headers: {
|
|
1582
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
1583
|
+
},
|
|
1584
|
+
body: body.toString(),
|
|
1585
|
+
});
|
|
1586
|
+
if (!res.ok) {
|
|
1587
|
+
throw await throwResponseError(this.name, res);
|
|
1588
|
+
}
|
|
1589
|
+
const response = await res.json();
|
|
1590
|
+
if (!response || response.error_code != 0) {
|
|
1591
|
+
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`);
|
|
1592
|
+
}
|
|
1593
|
+
return response.data.src;
|
|
1594
|
+
},
|
|
1499
1595
|
};
|
|
1500
1596
|
}
|
|
1501
1597
|
|
|
1502
|
-
function deepl$
|
|
1598
|
+
function deepl$3(options) {
|
|
1503
1599
|
const { key } = options;
|
|
1504
1600
|
const name = "deepl";
|
|
1505
1601
|
const checkOptions = () => {
|
|
@@ -1546,6 +1642,39 @@ function deepl$2(options) {
|
|
|
1546
1642
|
const translations = body.map((t) => t.text);
|
|
1547
1643
|
return translations;
|
|
1548
1644
|
},
|
|
1645
|
+
async checkLanguage(text) {
|
|
1646
|
+
checkOptions();
|
|
1647
|
+
const res = await fetch(url, {
|
|
1648
|
+
method: "POST",
|
|
1649
|
+
headers: {
|
|
1650
|
+
"Content-Type": "application/json; charset=UTF-8",
|
|
1651
|
+
Authorization: `DeepL-Auth-Key ${key}`,
|
|
1652
|
+
Accept: "*/*",
|
|
1653
|
+
Host: "api-free.deepl.com",
|
|
1654
|
+
Connection: "keep-alive",
|
|
1655
|
+
},
|
|
1656
|
+
body: JSON.stringify({
|
|
1657
|
+
text: [text],
|
|
1658
|
+
target_lang: "EN",
|
|
1659
|
+
}),
|
|
1660
|
+
});
|
|
1661
|
+
if (!res.ok) {
|
|
1662
|
+
throw await throwResponseError(this.name, res);
|
|
1663
|
+
}
|
|
1664
|
+
const bodyRes = await res.json();
|
|
1665
|
+
if (bodyRes.error) {
|
|
1666
|
+
throw new CheckLanguageError(this.name, `Check language fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
1667
|
+
}
|
|
1668
|
+
const body = bodyRes.translations;
|
|
1669
|
+
if (!body || body.length === 0) {
|
|
1670
|
+
throw new CheckLanguageError(this.name, "Check language fail! No result returned");
|
|
1671
|
+
}
|
|
1672
|
+
const detectedLanguage = body[0].detected_source_language;
|
|
1673
|
+
if (!detectedLanguage) {
|
|
1674
|
+
throw new CheckLanguageError(this.name, "Check language fail! Language not detected");
|
|
1675
|
+
}
|
|
1676
|
+
return detectedLanguage;
|
|
1677
|
+
},
|
|
1549
1678
|
};
|
|
1550
1679
|
}
|
|
1551
1680
|
|
|
@@ -1680,7 +1809,7 @@ function buildAuthorization({ secretId, secretKey, service, host, payload, httpR
|
|
|
1680
1809
|
headers["X-TC-Region"] = region;
|
|
1681
1810
|
return headers;
|
|
1682
1811
|
}
|
|
1683
|
-
function tencent$
|
|
1812
|
+
function tencent$3(options) {
|
|
1684
1813
|
const { secretId, secretKey, region = "ap-guangzhou" } = options;
|
|
1685
1814
|
const name = "tencent";
|
|
1686
1815
|
const host = "tmt.tencentcloudapi.com";
|
|
@@ -1728,7 +1857,7 @@ function tencent$2(options) {
|
|
|
1728
1857
|
}
|
|
1729
1858
|
const data = await res.json();
|
|
1730
1859
|
if (data.Response?.Error) {
|
|
1731
|
-
throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
|
|
1860
|
+
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`);
|
|
1732
1861
|
}
|
|
1733
1862
|
const translatedResults = data.Response?.TargetText.split("\n") ?? [];
|
|
1734
1863
|
if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
|
|
@@ -1742,20 +1871,63 @@ function tencent$2(options) {
|
|
|
1742
1871
|
throw new TranslationError(name, `Translation failed: ${error}`);
|
|
1743
1872
|
}
|
|
1744
1873
|
},
|
|
1874
|
+
async checkLanguage(text) {
|
|
1875
|
+
checkOptions();
|
|
1876
|
+
const payloadObj = {
|
|
1877
|
+
Text: text,
|
|
1878
|
+
ProjectId: 0,
|
|
1879
|
+
};
|
|
1880
|
+
const payload = JSON.stringify(payloadObj);
|
|
1881
|
+
const headers = buildAuthorization({
|
|
1882
|
+
secretId,
|
|
1883
|
+
secretKey,
|
|
1884
|
+
service,
|
|
1885
|
+
host,
|
|
1886
|
+
payload,
|
|
1887
|
+
httpRequestMethod: "POST",
|
|
1888
|
+
action: "LanguageDetect",
|
|
1889
|
+
apiVersion,
|
|
1890
|
+
region,
|
|
1891
|
+
});
|
|
1892
|
+
try {
|
|
1893
|
+
const res = await fetch(endpoint, {
|
|
1894
|
+
method: "POST",
|
|
1895
|
+
headers,
|
|
1896
|
+
body: payload,
|
|
1897
|
+
});
|
|
1898
|
+
if (!res.ok) {
|
|
1899
|
+
throw new CheckLanguageError(name, `HTTP ${res.status}: ${await res.text()}`);
|
|
1900
|
+
}
|
|
1901
|
+
const data = await res.json();
|
|
1902
|
+
if (data.Response?.Error) {
|
|
1903
|
+
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`);
|
|
1904
|
+
}
|
|
1905
|
+
const detectedLanguage = data.Response?.Lang;
|
|
1906
|
+
if (!detectedLanguage) {
|
|
1907
|
+
throw new CheckLanguageError(name, "Language detect fail! No result returned");
|
|
1908
|
+
}
|
|
1909
|
+
return detectedLanguage;
|
|
1910
|
+
}
|
|
1911
|
+
catch (error) {
|
|
1912
|
+
if (error instanceof CheckLanguageError)
|
|
1913
|
+
throw error;
|
|
1914
|
+
throw new CheckLanguageError(name, `Language detection failed: ${error}`);
|
|
1915
|
+
}
|
|
1916
|
+
},
|
|
1745
1917
|
};
|
|
1746
1918
|
}
|
|
1747
1919
|
|
|
1748
1920
|
const engines = {
|
|
1749
|
-
google,
|
|
1750
|
-
azure: azure$
|
|
1751
|
-
amazon: amazon$
|
|
1752
|
-
baidu: baidu$
|
|
1753
|
-
deepl: deepl$
|
|
1921
|
+
google: google$1,
|
|
1922
|
+
azure: azure$2,
|
|
1923
|
+
amazon: amazon$2,
|
|
1924
|
+
baidu: baidu$2,
|
|
1925
|
+
deepl: deepl$3,
|
|
1754
1926
|
openai: openai$1,
|
|
1755
|
-
tencent: tencent$
|
|
1927
|
+
tencent: tencent$3,
|
|
1756
1928
|
};
|
|
1757
1929
|
|
|
1758
|
-
var azure = {
|
|
1930
|
+
var azure$1 = {
|
|
1759
1931
|
Afrikaans: "af",
|
|
1760
1932
|
Albanian: "sq",
|
|
1761
1933
|
Amharic: "am",
|
|
@@ -2139,7 +2311,7 @@ var openai = {
|
|
|
2139
2311
|
Zulu: "zu",
|
|
2140
2312
|
};
|
|
2141
2313
|
|
|
2142
|
-
var baidu = {
|
|
2314
|
+
var baidu$1 = {
|
|
2143
2315
|
Achinese: "ach",
|
|
2144
2316
|
Afrikaans: "afr",
|
|
2145
2317
|
Akan: "aka",
|
|
@@ -2350,7 +2522,7 @@ var baidu = {
|
|
|
2350
2522
|
Zulu: "zul",
|
|
2351
2523
|
};
|
|
2352
2524
|
|
|
2353
|
-
var deepl$
|
|
2525
|
+
var deepl$2 = {
|
|
2354
2526
|
English: "en",
|
|
2355
2527
|
Bulgarian: "bg",
|
|
2356
2528
|
Chinese: "zh",
|
|
@@ -2386,7 +2558,7 @@ var deepl$1 = {
|
|
|
2386
2558
|
Ukrainian: "uk",
|
|
2387
2559
|
};
|
|
2388
2560
|
|
|
2389
|
-
var tencent$
|
|
2561
|
+
var tencent$2 = {
|
|
2390
2562
|
Auto: "auto",
|
|
2391
2563
|
Chinese: "zh",
|
|
2392
2564
|
TraditionalChinese: "zh-TW",
|
|
@@ -2408,7 +2580,7 @@ var tencent$1 = {
|
|
|
2408
2580
|
Hindi: "hi",
|
|
2409
2581
|
};
|
|
2410
2582
|
|
|
2411
|
-
var amazon = {
|
|
2583
|
+
var amazon$1 = {
|
|
2412
2584
|
Afrikaans: "af",
|
|
2413
2585
|
Albanian: "sq",
|
|
2414
2586
|
Amharic: "am",
|
|
@@ -2487,16 +2659,16 @@ var amazon = {
|
|
|
2487
2659
|
};
|
|
2488
2660
|
|
|
2489
2661
|
const originLanguages = {
|
|
2490
|
-
azure: azure,
|
|
2662
|
+
azure: azure$1,
|
|
2491
2663
|
google: openai,
|
|
2492
|
-
baidu: baidu,
|
|
2493
|
-
deepl: deepl$
|
|
2494
|
-
amazon: amazon,
|
|
2664
|
+
baidu: baidu$1,
|
|
2665
|
+
deepl: deepl$2,
|
|
2666
|
+
amazon: amazon$1,
|
|
2495
2667
|
openai: openai,
|
|
2496
|
-
tencent: tencent$
|
|
2668
|
+
tencent: tencent$2,
|
|
2497
2669
|
};
|
|
2498
2670
|
|
|
2499
|
-
var deepl = {
|
|
2671
|
+
var deepl$1 = {
|
|
2500
2672
|
"American English": "en-US",
|
|
2501
2673
|
"Brazilian Portuguese": "pt-BR",
|
|
2502
2674
|
"British English": "en-GB",
|
|
@@ -2535,7 +2707,7 @@ var deepl = {
|
|
|
2535
2707
|
Ukrainian: "uk",
|
|
2536
2708
|
};
|
|
2537
2709
|
|
|
2538
|
-
var tencent = {
|
|
2710
|
+
var tencent$1 = {
|
|
2539
2711
|
"Simplified Chinese": "zh",
|
|
2540
2712
|
"Traditional Chinese": "zh-TW",
|
|
2541
2713
|
English: "en",
|
|
@@ -2557,13 +2729,197 @@ var tencent = {
|
|
|
2557
2729
|
};
|
|
2558
2730
|
|
|
2559
2731
|
const targetLanguages = {
|
|
2560
|
-
azure: azure,
|
|
2732
|
+
azure: azure$1,
|
|
2561
2733
|
google: openai,
|
|
2562
|
-
baidu: baidu,
|
|
2563
|
-
deepl: deepl,
|
|
2564
|
-
amazon: amazon,
|
|
2734
|
+
baidu: baidu$1,
|
|
2735
|
+
deepl: deepl$1,
|
|
2736
|
+
amazon: amazon$1,
|
|
2565
2737
|
openai: openai,
|
|
2738
|
+
tencent: tencent$1,
|
|
2739
|
+
};
|
|
2740
|
+
|
|
2741
|
+
var baidu = {
|
|
2742
|
+
Chinese: "zh",
|
|
2743
|
+
English: "en",
|
|
2744
|
+
Japanese: "jp",
|
|
2745
|
+
Korean: "kor",
|
|
2746
|
+
Thai: "th",
|
|
2747
|
+
Russian: "ru",
|
|
2748
|
+
};
|
|
2749
|
+
|
|
2750
|
+
var tencent = {
|
|
2751
|
+
Chinese: "zh",
|
|
2752
|
+
English: "en",
|
|
2753
|
+
Japanese: "jp",
|
|
2754
|
+
Korean: "kr",
|
|
2755
|
+
German: "de",
|
|
2756
|
+
French: "fr",
|
|
2757
|
+
Spanish: "es",
|
|
2758
|
+
Italian: "it",
|
|
2759
|
+
Turkish: "tr",
|
|
2760
|
+
Russian: "ru",
|
|
2761
|
+
Portuguese: "pt",
|
|
2762
|
+
Vietnamese: "vi",
|
|
2763
|
+
Indonesian: "id",
|
|
2764
|
+
Malay: "ms",
|
|
2765
|
+
Thai: "th",
|
|
2766
|
+
};
|
|
2767
|
+
|
|
2768
|
+
var azure = {
|
|
2769
|
+
Arabic: "ar",
|
|
2770
|
+
Bengali: "bn",
|
|
2771
|
+
Bulgarian: "bg",
|
|
2772
|
+
Catalan: "ca",
|
|
2773
|
+
Chinese: "zh-Hans",
|
|
2774
|
+
"Chinese (Traditional)": "zh-Hant",
|
|
2775
|
+
Croatian: "hr",
|
|
2776
|
+
Czech: "cs",
|
|
2777
|
+
Danish: "da",
|
|
2778
|
+
Dutch: "nl",
|
|
2779
|
+
English: "en",
|
|
2780
|
+
Finnish: "fi",
|
|
2781
|
+
French: "fr",
|
|
2782
|
+
German: "de",
|
|
2783
|
+
Greek: "el",
|
|
2784
|
+
Hebrew: "he",
|
|
2785
|
+
Hindi: "hi",
|
|
2786
|
+
Hungarian: "hu",
|
|
2787
|
+
Indonesian: "id",
|
|
2788
|
+
Italian: "it",
|
|
2789
|
+
Japanese: "ja",
|
|
2790
|
+
Korean: "ko",
|
|
2791
|
+
Malay: "ms",
|
|
2792
|
+
"Norwegian Bokmål": "nb",
|
|
2793
|
+
Persian: "fa",
|
|
2794
|
+
Polish: "pl",
|
|
2795
|
+
"Portugal (Brazil)": "pt",
|
|
2796
|
+
Romanian: "ro",
|
|
2797
|
+
Russian: "ru",
|
|
2798
|
+
Slovak: "sk",
|
|
2799
|
+
Spanish: "es",
|
|
2800
|
+
Swedish: "sv",
|
|
2801
|
+
Thai: "th",
|
|
2802
|
+
Turkish: "tr",
|
|
2803
|
+
Ukrainian: "uk",
|
|
2804
|
+
Urdu: "ur",
|
|
2805
|
+
Vietnamese: "vi",
|
|
2806
|
+
};
|
|
2807
|
+
|
|
2808
|
+
var amazon = {
|
|
2809
|
+
Arabic: "ar",
|
|
2810
|
+
Bengali: "bn",
|
|
2811
|
+
Bulgarian: "bg",
|
|
2812
|
+
Catalan: "ca",
|
|
2813
|
+
Chinese: "zh-Hans",
|
|
2814
|
+
"Chinese (Traditional)": "zh-Hant",
|
|
2815
|
+
Croatian: "hr",
|
|
2816
|
+
Czech: "cs",
|
|
2817
|
+
Danish: "da",
|
|
2818
|
+
Dutch: "nl",
|
|
2819
|
+
English: "en",
|
|
2820
|
+
Finnish: "fi",
|
|
2821
|
+
French: "fr",
|
|
2822
|
+
German: "de",
|
|
2823
|
+
Greek: "el",
|
|
2824
|
+
Hebrew: "he",
|
|
2825
|
+
Hindi: "hi",
|
|
2826
|
+
Hungarian: "hu",
|
|
2827
|
+
Indonesian: "id",
|
|
2828
|
+
Italian: "it",
|
|
2829
|
+
Japanese: "ja",
|
|
2830
|
+
Korean: "ko",
|
|
2831
|
+
Malay: "ms",
|
|
2832
|
+
Persian: "fa",
|
|
2833
|
+
Polish: "pl",
|
|
2834
|
+
"Portugal (Brazil)": "pt",
|
|
2835
|
+
Romanian: "ro",
|
|
2836
|
+
Russian: "ru",
|
|
2837
|
+
Slovak: "sk",
|
|
2838
|
+
Spanish: "es",
|
|
2839
|
+
Swedish: "sv",
|
|
2840
|
+
Thai: "th",
|
|
2841
|
+
Turkish: "tr",
|
|
2842
|
+
Ukrainian: "uk",
|
|
2843
|
+
Urdu: "ur",
|
|
2844
|
+
Vietnamese: "vi",
|
|
2845
|
+
};
|
|
2846
|
+
|
|
2847
|
+
var deepl = {
|
|
2848
|
+
"American English": "en",
|
|
2849
|
+
Bulgarian: "bg",
|
|
2850
|
+
Chinese: "zh",
|
|
2851
|
+
Czech: "cs",
|
|
2852
|
+
Danish: "da",
|
|
2853
|
+
Dutch: "nl",
|
|
2854
|
+
Estonian: "et",
|
|
2855
|
+
Finnish: "fi",
|
|
2856
|
+
French: "fr",
|
|
2857
|
+
German: "de",
|
|
2858
|
+
Greek: "el",
|
|
2859
|
+
Hungarian: "hu",
|
|
2860
|
+
Icelandic: "is",
|
|
2861
|
+
Indonesian: "id",
|
|
2862
|
+
Irish: "ga",
|
|
2863
|
+
Italian: "it",
|
|
2864
|
+
Japanese: "ja",
|
|
2865
|
+
Korean: "ko",
|
|
2866
|
+
Latvian: "lv",
|
|
2867
|
+
Lithuanian: "lt",
|
|
2868
|
+
Maltese: "mt",
|
|
2869
|
+
Norwegian: "nb",
|
|
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
|
+
var google = {
|
|
2883
|
+
English: "en",
|
|
2884
|
+
Bulgarian: "bg",
|
|
2885
|
+
Chinese: "zh-cn",
|
|
2886
|
+
Czech: "cs",
|
|
2887
|
+
Danish: "da",
|
|
2888
|
+
Dutch: "nl",
|
|
2889
|
+
Estonian: "et",
|
|
2890
|
+
Finnish: "fi",
|
|
2891
|
+
French: "fr",
|
|
2892
|
+
German: "de",
|
|
2893
|
+
Greek: "el",
|
|
2894
|
+
Hungarian: "hu",
|
|
2895
|
+
Icelandic: "is",
|
|
2896
|
+
Indonesian: "id",
|
|
2897
|
+
Irish: "ga",
|
|
2898
|
+
Italian: "it",
|
|
2899
|
+
Japanese: "ja",
|
|
2900
|
+
Korean: "ko",
|
|
2901
|
+
Latvian: "lv",
|
|
2902
|
+
Lithuanian: "lt",
|
|
2903
|
+
Maltese: "mt",
|
|
2904
|
+
Polish: "pl",
|
|
2905
|
+
Portuguese: "pt",
|
|
2906
|
+
Romanian: "ro",
|
|
2907
|
+
Russian: "ru",
|
|
2908
|
+
Slovak: "sk",
|
|
2909
|
+
Slovenian: "sl",
|
|
2910
|
+
Spanish: "es",
|
|
2911
|
+
Swedish: "sv",
|
|
2912
|
+
Turkish: "tr",
|
|
2913
|
+
Ukrainian: "uk",
|
|
2914
|
+
};
|
|
2915
|
+
|
|
2916
|
+
const checkLanguages = {
|
|
2917
|
+
baidu: baidu,
|
|
2566
2918
|
tencent: tencent,
|
|
2919
|
+
azure: azure,
|
|
2920
|
+
amazon: amazon,
|
|
2921
|
+
deepl: deepl,
|
|
2922
|
+
google: google,
|
|
2567
2923
|
};
|
|
2568
2924
|
|
|
2569
2925
|
function normalFromLanguage(from, engine) {
|
|
@@ -2644,6 +3000,42 @@ class Translator {
|
|
|
2644
3000
|
}
|
|
2645
3001
|
this.engines.delete(engineName);
|
|
2646
3002
|
}
|
|
3003
|
+
isSupportCheckLanguage(engineName) {
|
|
3004
|
+
if (!engineName || !this.engines.has(engineName)) {
|
|
3005
|
+
logger.warn("Engine name is required or not found");
|
|
3006
|
+
return false;
|
|
3007
|
+
}
|
|
3008
|
+
const engine = this.engines.get(engineName);
|
|
3009
|
+
if (!engine) {
|
|
3010
|
+
logger.warn(`Engine ${engineName} not found`);
|
|
3011
|
+
return false;
|
|
3012
|
+
}
|
|
3013
|
+
return !!engine.checkLanguage;
|
|
3014
|
+
}
|
|
3015
|
+
async checkLanguage(text, options) {
|
|
3016
|
+
const { engine = "google", max_character_num = defaultMaxCharacterNum } = options;
|
|
3017
|
+
if (!this.engines.has(engine)) {
|
|
3018
|
+
throw new CheckLanguageError(appName, `Engine ${engine} not found`);
|
|
3019
|
+
}
|
|
3020
|
+
const engineInstance = this.engines.get(engine);
|
|
3021
|
+
if (!engineInstance) {
|
|
3022
|
+
throw new CheckLanguageError(appName, `Engine ${engine} is null`);
|
|
3023
|
+
}
|
|
3024
|
+
if (!engineInstance.checkLanguage) {
|
|
3025
|
+
throw new CheckLanguageError(appName, `Engine ${engine} does not support checkLanguage method`);
|
|
3026
|
+
}
|
|
3027
|
+
const sample = max_character_num > 0 ? text.substring(0, max_character_num) : text;
|
|
3028
|
+
if (sample.length <= 0) {
|
|
3029
|
+
throw new TranslationError(appName, "Text is empty or too short to check language");
|
|
3030
|
+
}
|
|
3031
|
+
return engineInstance
|
|
3032
|
+
.checkLanguage(sample)
|
|
3033
|
+
.then((r) => r)
|
|
3034
|
+
.catch((e) => {
|
|
3035
|
+
logger.error(`${appName} Failed to check language for text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${getErrorMessages(e)}`);
|
|
3036
|
+
throw e;
|
|
3037
|
+
});
|
|
3038
|
+
}
|
|
2647
3039
|
async translate(text, options) {
|
|
2648
3040
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2649
3041
|
let { from = "auto", to } = options;
|
|
@@ -2655,7 +3047,7 @@ class Translator {
|
|
|
2655
3047
|
}
|
|
2656
3048
|
const engineInstance = this.engines.get(engine);
|
|
2657
3049
|
if (!engineInstance) {
|
|
2658
|
-
throw new TranslationError(appName, `Engine ${engine}
|
|
3050
|
+
throw new TranslationError(appName, `Engine ${engine} is null`);
|
|
2659
3051
|
}
|
|
2660
3052
|
if (!from) {
|
|
2661
3053
|
throw new TranslationError(appName, `Invalid origin language ${from}`);
|
|
@@ -2743,11 +3135,17 @@ var index = {
|
|
|
2743
3135
|
};
|
|
2744
3136
|
|
|
2745
3137
|
exports.Cache = Cache;
|
|
3138
|
+
exports.CheckLanguageError = CheckLanguageError;
|
|
2746
3139
|
exports.OPEN_AI_MODELS = OPEN_AI_MODELS;
|
|
2747
3140
|
exports.TranslationError = TranslationError;
|
|
2748
3141
|
exports.Translator = Translator;
|
|
3142
|
+
exports.checkLanguages = checkLanguages;
|
|
2749
3143
|
exports.default = index;
|
|
2750
3144
|
exports.engines = engines;
|
|
2751
3145
|
exports.getLanguage = getLanguage;
|
|
3146
|
+
exports.normalFromLanguage = normalFromLanguage;
|
|
3147
|
+
exports.normalToLanguage = normalToLanguage;
|
|
3148
|
+
exports.originLanguages = originLanguages;
|
|
3149
|
+
exports.targetLanguages = targetLanguages;
|
|
2752
3150
|
exports.translator = translator;
|
|
2753
3151
|
//# sourceMappingURL=index.cjs.map
|