@yxw007/translate 0.2.2 → 0.3.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 +33 -0
- package/README_zh-CN.md +34 -0
- package/dist/browser/index.cjs +163 -1
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +163 -1
- 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 +166 -5
- 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 +131 -36
- package/dist/index.d.ts.map +1 -1
- package/dist/node/index.cjs +163 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +163 -1
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +3 -2
- package/package.json +3 -2
package/dist/node/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// translate v0.
|
|
1
|
+
// translate v0.3.0 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
import 'fs/promises';
|
|
3
3
|
import 'fs';
|
|
4
4
|
import 'path';
|
|
5
5
|
import { TranslateClient, TranslateTextCommand } from '@aws-sdk/client-translate';
|
|
6
|
+
import crypto from 'crypto';
|
|
6
7
|
|
|
7
8
|
class TranslationError extends Error {
|
|
8
9
|
region;
|
|
@@ -1591,6 +1592,121 @@ function openai$1(options) {
|
|
|
1591
1592
|
};
|
|
1592
1593
|
}
|
|
1593
1594
|
|
|
1595
|
+
function sha256(message, secret = "", encoding) {
|
|
1596
|
+
if (encoding) {
|
|
1597
|
+
return crypto.createHmac("sha256", secret).update(message).digest(encoding);
|
|
1598
|
+
}
|
|
1599
|
+
return crypto.createHmac("sha256", secret).update(message).digest();
|
|
1600
|
+
}
|
|
1601
|
+
function getHash(message, encoding = "hex") {
|
|
1602
|
+
return crypto.createHash("sha256").update(message).digest(encoding);
|
|
1603
|
+
}
|
|
1604
|
+
function getDate(timestamp) {
|
|
1605
|
+
const date = new Date(timestamp * 1000);
|
|
1606
|
+
return `${date.getUTCFullYear()}-${("0" + (date.getUTCMonth() + 1)).slice(-2)}-${("0" + date.getUTCDate()).slice(-2)}`;
|
|
1607
|
+
}
|
|
1608
|
+
function buildAuthorization({ secretId, secretKey, service, host, payload, httpRequestMethod, action, apiVersion, region }) {
|
|
1609
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
1610
|
+
const date = getDate(timestamp);
|
|
1611
|
+
const canonicalUri = "/";
|
|
1612
|
+
const canonicalQueryString = "";
|
|
1613
|
+
const canonicalHeaders = "content-type:application/json; charset=utf-8\nhost:" + host + "\n";
|
|
1614
|
+
const signedHeaders = "content-type;host";
|
|
1615
|
+
const hashedRequestPayload = getHash(payload);
|
|
1616
|
+
const canonicalRequest = [
|
|
1617
|
+
httpRequestMethod,
|
|
1618
|
+
canonicalUri,
|
|
1619
|
+
canonicalQueryString,
|
|
1620
|
+
canonicalHeaders,
|
|
1621
|
+
signedHeaders,
|
|
1622
|
+
hashedRequestPayload,
|
|
1623
|
+
].join("\n");
|
|
1624
|
+
const algorithm = "TC3-HMAC-SHA256";
|
|
1625
|
+
const credentialScope = `${date}/${service}/tc3_request`;
|
|
1626
|
+
const hashedCanonicalRequest = getHash(canonicalRequest);
|
|
1627
|
+
const stringToSign = [algorithm, timestamp, credentialScope, hashedCanonicalRequest].join("\n");
|
|
1628
|
+
const kDate = sha256(date, "TC3" + secretKey);
|
|
1629
|
+
const kService = sha256(service, kDate);
|
|
1630
|
+
const kSigning = sha256("tc3_request", kService);
|
|
1631
|
+
const signature = sha256(stringToSign, kSigning, "hex");
|
|
1632
|
+
const authorization = `${algorithm} Credential=${secretId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
|
|
1633
|
+
const headers = {
|
|
1634
|
+
Authorization: authorization,
|
|
1635
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
1636
|
+
Host: host,
|
|
1637
|
+
"X-TC-Action": action,
|
|
1638
|
+
"X-TC-Timestamp": timestamp,
|
|
1639
|
+
"X-TC-Version": apiVersion,
|
|
1640
|
+
};
|
|
1641
|
+
if (region)
|
|
1642
|
+
headers["X-TC-Region"] = region;
|
|
1643
|
+
return headers;
|
|
1644
|
+
}
|
|
1645
|
+
function tencent$2(options) {
|
|
1646
|
+
const { secretId, secretKey, region = "ap-guangzhou" } = options;
|
|
1647
|
+
const name = "tencent";
|
|
1648
|
+
const host = "tmt.tencentcloudapi.com";
|
|
1649
|
+
const endpoint = `https://${host}/`;
|
|
1650
|
+
const service = "tmt";
|
|
1651
|
+
const apiVersion = "2018-03-21";
|
|
1652
|
+
const action = "TextTranslate";
|
|
1653
|
+
function checkOptions() {
|
|
1654
|
+
if (!secretId || !secretKey) {
|
|
1655
|
+
throw new TranslationError(name, `${name} secretId and secretKey are required`);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
return {
|
|
1659
|
+
name,
|
|
1660
|
+
async translate(text, opts) {
|
|
1661
|
+
checkOptions();
|
|
1662
|
+
const { from = "auto", to } = opts;
|
|
1663
|
+
const source = from === "auto" ? "" : from;
|
|
1664
|
+
const payloadObj = {
|
|
1665
|
+
SourceText: Array.isArray(text) ? text.join("\n") : text,
|
|
1666
|
+
Source: source,
|
|
1667
|
+
Target: to,
|
|
1668
|
+
ProjectId: 0,
|
|
1669
|
+
};
|
|
1670
|
+
const payload = JSON.stringify(payloadObj);
|
|
1671
|
+
const headers = buildAuthorization({
|
|
1672
|
+
secretId,
|
|
1673
|
+
secretKey,
|
|
1674
|
+
service,
|
|
1675
|
+
host,
|
|
1676
|
+
payload,
|
|
1677
|
+
httpRequestMethod: "POST",
|
|
1678
|
+
action,
|
|
1679
|
+
apiVersion,
|
|
1680
|
+
region,
|
|
1681
|
+
});
|
|
1682
|
+
try {
|
|
1683
|
+
const res = await fetch(endpoint, {
|
|
1684
|
+
method: "POST",
|
|
1685
|
+
headers,
|
|
1686
|
+
body: payload,
|
|
1687
|
+
});
|
|
1688
|
+
if (!res.ok) {
|
|
1689
|
+
throw new TranslationError(name, `HTTP ${res.status}: ${await res.text()}`);
|
|
1690
|
+
}
|
|
1691
|
+
const data = await res.json();
|
|
1692
|
+
if (data.Response?.Error) {
|
|
1693
|
+
throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
|
|
1694
|
+
}
|
|
1695
|
+
const translatedResults = data.Response?.TargetText.split("\n") ?? [];
|
|
1696
|
+
if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
|
|
1697
|
+
throw new TranslationError(name, "Translate fail! No result returned");
|
|
1698
|
+
}
|
|
1699
|
+
return translatedResults;
|
|
1700
|
+
}
|
|
1701
|
+
catch (error) {
|
|
1702
|
+
if (error instanceof TranslationError)
|
|
1703
|
+
throw error;
|
|
1704
|
+
throw new TranslationError(name, `Translation failed: ${error}`);
|
|
1705
|
+
}
|
|
1706
|
+
},
|
|
1707
|
+
};
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1594
1710
|
const engines = {
|
|
1595
1711
|
google,
|
|
1596
1712
|
azure: azure$1,
|
|
@@ -1598,6 +1714,7 @@ const engines = {
|
|
|
1598
1714
|
baidu: baidu$1,
|
|
1599
1715
|
deepl: deepl$2,
|
|
1600
1716
|
openai: openai$1,
|
|
1717
|
+
tencent: tencent$2,
|
|
1601
1718
|
};
|
|
1602
1719
|
|
|
1603
1720
|
var azure = {
|
|
@@ -2231,6 +2348,28 @@ var deepl$1 = {
|
|
|
2231
2348
|
Ukrainian: "uk",
|
|
2232
2349
|
};
|
|
2233
2350
|
|
|
2351
|
+
var tencent$1 = {
|
|
2352
|
+
Auto: "auto",
|
|
2353
|
+
Chinese: "zh",
|
|
2354
|
+
TraditionalChinese: "zh-TW",
|
|
2355
|
+
English: "en",
|
|
2356
|
+
Japanese: "ja",
|
|
2357
|
+
Korean: "ko",
|
|
2358
|
+
French: "fr",
|
|
2359
|
+
Spanish: "es",
|
|
2360
|
+
Italian: "it",
|
|
2361
|
+
German: "de",
|
|
2362
|
+
Turkish: "tr",
|
|
2363
|
+
Russian: "ru",
|
|
2364
|
+
Portuguese: "pt",
|
|
2365
|
+
Vietnamese: "vi",
|
|
2366
|
+
Indonesian: "id",
|
|
2367
|
+
Thai: "th",
|
|
2368
|
+
Malay: "ms",
|
|
2369
|
+
Arabic: "ar",
|
|
2370
|
+
Hindi: "hi",
|
|
2371
|
+
};
|
|
2372
|
+
|
|
2234
2373
|
var amazon = {
|
|
2235
2374
|
Afrikaans: "af",
|
|
2236
2375
|
Albanian: "sq",
|
|
@@ -2316,6 +2455,7 @@ const originLanguages = {
|
|
|
2316
2455
|
deepl: deepl$1,
|
|
2317
2456
|
amazon: amazon,
|
|
2318
2457
|
openai: openai,
|
|
2458
|
+
tencent: tencent$1,
|
|
2319
2459
|
};
|
|
2320
2460
|
|
|
2321
2461
|
var deepl = {
|
|
@@ -2357,6 +2497,27 @@ var deepl = {
|
|
|
2357
2497
|
Ukrainian: "uk",
|
|
2358
2498
|
};
|
|
2359
2499
|
|
|
2500
|
+
var tencent = {
|
|
2501
|
+
"Simplified Chinese": "zh",
|
|
2502
|
+
"Traditional Chinese": "zh-TW",
|
|
2503
|
+
English: "en",
|
|
2504
|
+
Japanese: "ja",
|
|
2505
|
+
Korean: "ko",
|
|
2506
|
+
French: "fr",
|
|
2507
|
+
Spanish: "es",
|
|
2508
|
+
Italian: "it",
|
|
2509
|
+
German: "de",
|
|
2510
|
+
Turkish: "tr",
|
|
2511
|
+
Russian: "ru",
|
|
2512
|
+
Portuguese: "pt",
|
|
2513
|
+
Vietnamese: "vi",
|
|
2514
|
+
Indonesian: "id",
|
|
2515
|
+
Thai: "th",
|
|
2516
|
+
Malay: "ms",
|
|
2517
|
+
Arabic: "ar",
|
|
2518
|
+
Hindi: "hi",
|
|
2519
|
+
};
|
|
2520
|
+
|
|
2360
2521
|
const targetLanguages = {
|
|
2361
2522
|
azure: azure,
|
|
2362
2523
|
google: openai,
|
|
@@ -2364,6 +2525,7 @@ const targetLanguages = {
|
|
|
2364
2525
|
deepl: deepl,
|
|
2365
2526
|
amazon: amazon,
|
|
2366
2527
|
openai: openai,
|
|
2528
|
+
tencent: tencent,
|
|
2367
2529
|
};
|
|
2368
2530
|
|
|
2369
2531
|
function normalFromLanguage(from, engine) {
|