@yxw007/translate 0.2.2 → 0.3.1
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
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// translate v0.
|
|
1
|
+
// translate v0.3.1 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;
|
|
@@ -1625,6 +1626,121 @@ function openai$1(options) {
|
|
|
1625
1626
|
};
|
|
1626
1627
|
}
|
|
1627
1628
|
|
|
1629
|
+
function sha256(message, secret = "", encoding) {
|
|
1630
|
+
if (encoding) {
|
|
1631
|
+
return crypto.createHmac("sha256", secret).update(message).digest(encoding);
|
|
1632
|
+
}
|
|
1633
|
+
return crypto.createHmac("sha256", secret).update(message).digest();
|
|
1634
|
+
}
|
|
1635
|
+
function getHash(message, encoding = "hex") {
|
|
1636
|
+
return crypto.createHash("sha256").update(message).digest(encoding);
|
|
1637
|
+
}
|
|
1638
|
+
function getDate(timestamp) {
|
|
1639
|
+
const date = new Date(timestamp * 1000);
|
|
1640
|
+
return `${date.getUTCFullYear()}-${("0" + (date.getUTCMonth() + 1)).slice(-2)}-${("0" + date.getUTCDate()).slice(-2)}`;
|
|
1641
|
+
}
|
|
1642
|
+
function buildAuthorization({ secretId, secretKey, service, host, payload, httpRequestMethod, action, apiVersion, region }) {
|
|
1643
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
1644
|
+
const date = getDate(timestamp);
|
|
1645
|
+
const canonicalUri = "/";
|
|
1646
|
+
const canonicalQueryString = "";
|
|
1647
|
+
const canonicalHeaders = "content-type:application/json; charset=utf-8\nhost:" + host + "\n";
|
|
1648
|
+
const signedHeaders = "content-type;host";
|
|
1649
|
+
const hashedRequestPayload = getHash(payload);
|
|
1650
|
+
const canonicalRequest = [
|
|
1651
|
+
httpRequestMethod,
|
|
1652
|
+
canonicalUri,
|
|
1653
|
+
canonicalQueryString,
|
|
1654
|
+
canonicalHeaders,
|
|
1655
|
+
signedHeaders,
|
|
1656
|
+
hashedRequestPayload,
|
|
1657
|
+
].join("\n");
|
|
1658
|
+
const algorithm = "TC3-HMAC-SHA256";
|
|
1659
|
+
const credentialScope = `${date}/${service}/tc3_request`;
|
|
1660
|
+
const hashedCanonicalRequest = getHash(canonicalRequest);
|
|
1661
|
+
const stringToSign = [algorithm, timestamp, credentialScope, hashedCanonicalRequest].join("\n");
|
|
1662
|
+
const kDate = sha256(date, "TC3" + secretKey);
|
|
1663
|
+
const kService = sha256(service, kDate);
|
|
1664
|
+
const kSigning = sha256("tc3_request", kService);
|
|
1665
|
+
const signature = sha256(stringToSign, kSigning, "hex");
|
|
1666
|
+
const authorization = `${algorithm} Credential=${secretId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
|
|
1667
|
+
const headers = {
|
|
1668
|
+
Authorization: authorization,
|
|
1669
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
1670
|
+
Host: host,
|
|
1671
|
+
"X-TC-Action": action,
|
|
1672
|
+
"X-TC-Timestamp": timestamp,
|
|
1673
|
+
"X-TC-Version": apiVersion,
|
|
1674
|
+
};
|
|
1675
|
+
if (region)
|
|
1676
|
+
headers["X-TC-Region"] = region;
|
|
1677
|
+
return headers;
|
|
1678
|
+
}
|
|
1679
|
+
function tencent$2(options) {
|
|
1680
|
+
const { secretId, secretKey, region = "ap-guangzhou" } = options;
|
|
1681
|
+
const name = "tencent";
|
|
1682
|
+
const host = "tmt.tencentcloudapi.com";
|
|
1683
|
+
const endpoint = `https://${host}/`;
|
|
1684
|
+
const service = "tmt";
|
|
1685
|
+
const apiVersion = "2018-03-21";
|
|
1686
|
+
const action = "TextTranslate";
|
|
1687
|
+
function checkOptions() {
|
|
1688
|
+
if (!secretId || !secretKey) {
|
|
1689
|
+
throw new TranslationError(name, `${name} secretId and secretKey are required`);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
return {
|
|
1693
|
+
name,
|
|
1694
|
+
async translate(text, opts) {
|
|
1695
|
+
checkOptions();
|
|
1696
|
+
const { from = "auto", to } = opts;
|
|
1697
|
+
const source = from === "auto" ? "auto" : from;
|
|
1698
|
+
const payloadObj = {
|
|
1699
|
+
SourceText: Array.isArray(text) ? text.join("\n") : text,
|
|
1700
|
+
Source: source,
|
|
1701
|
+
Target: to,
|
|
1702
|
+
ProjectId: 0,
|
|
1703
|
+
};
|
|
1704
|
+
const payload = JSON.stringify(payloadObj);
|
|
1705
|
+
const headers = buildAuthorization({
|
|
1706
|
+
secretId,
|
|
1707
|
+
secretKey,
|
|
1708
|
+
service,
|
|
1709
|
+
host,
|
|
1710
|
+
payload,
|
|
1711
|
+
httpRequestMethod: "POST",
|
|
1712
|
+
action,
|
|
1713
|
+
apiVersion,
|
|
1714
|
+
region,
|
|
1715
|
+
});
|
|
1716
|
+
try {
|
|
1717
|
+
const res = await fetch(endpoint, {
|
|
1718
|
+
method: "POST",
|
|
1719
|
+
headers,
|
|
1720
|
+
body: payload,
|
|
1721
|
+
});
|
|
1722
|
+
if (!res.ok) {
|
|
1723
|
+
throw new TranslationError(name, `HTTP ${res.status}: ${await res.text()}`);
|
|
1724
|
+
}
|
|
1725
|
+
const data = await res.json();
|
|
1726
|
+
if (data.Response?.Error) {
|
|
1727
|
+
throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
|
|
1728
|
+
}
|
|
1729
|
+
const translatedResults = data.Response?.TargetText.split("\n") ?? [];
|
|
1730
|
+
if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
|
|
1731
|
+
throw new TranslationError(name, "Translate fail! No result returned");
|
|
1732
|
+
}
|
|
1733
|
+
return translatedResults;
|
|
1734
|
+
}
|
|
1735
|
+
catch (error) {
|
|
1736
|
+
if (error instanceof TranslationError)
|
|
1737
|
+
throw error;
|
|
1738
|
+
throw new TranslationError(name, `Translation failed: ${error}`);
|
|
1739
|
+
}
|
|
1740
|
+
},
|
|
1741
|
+
};
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1628
1744
|
const engines = {
|
|
1629
1745
|
google,
|
|
1630
1746
|
azure: azure$1,
|
|
@@ -1632,6 +1748,7 @@ const engines = {
|
|
|
1632
1748
|
baidu: baidu$1,
|
|
1633
1749
|
deepl: deepl$2,
|
|
1634
1750
|
openai: openai$1,
|
|
1751
|
+
tencent: tencent$2,
|
|
1635
1752
|
};
|
|
1636
1753
|
|
|
1637
1754
|
var azure = {
|
|
@@ -2265,6 +2382,28 @@ var deepl$1 = {
|
|
|
2265
2382
|
Ukrainian: "uk",
|
|
2266
2383
|
};
|
|
2267
2384
|
|
|
2385
|
+
var tencent$1 = {
|
|
2386
|
+
Auto: "auto",
|
|
2387
|
+
Chinese: "zh",
|
|
2388
|
+
TraditionalChinese: "zh-TW",
|
|
2389
|
+
English: "en",
|
|
2390
|
+
Japanese: "ja",
|
|
2391
|
+
Korean: "ko",
|
|
2392
|
+
French: "fr",
|
|
2393
|
+
Spanish: "es",
|
|
2394
|
+
Italian: "it",
|
|
2395
|
+
German: "de",
|
|
2396
|
+
Turkish: "tr",
|
|
2397
|
+
Russian: "ru",
|
|
2398
|
+
Portuguese: "pt",
|
|
2399
|
+
Vietnamese: "vi",
|
|
2400
|
+
Indonesian: "id",
|
|
2401
|
+
Thai: "th",
|
|
2402
|
+
Malay: "ms",
|
|
2403
|
+
Arabic: "ar",
|
|
2404
|
+
Hindi: "hi",
|
|
2405
|
+
};
|
|
2406
|
+
|
|
2268
2407
|
var amazon = {
|
|
2269
2408
|
Afrikaans: "af",
|
|
2270
2409
|
Albanian: "sq",
|
|
@@ -2350,6 +2489,7 @@ const originLanguages = {
|
|
|
2350
2489
|
deepl: deepl$1,
|
|
2351
2490
|
amazon: amazon,
|
|
2352
2491
|
openai: openai,
|
|
2492
|
+
tencent: tencent$1,
|
|
2353
2493
|
};
|
|
2354
2494
|
|
|
2355
2495
|
var deepl = {
|
|
@@ -2391,6 +2531,27 @@ var deepl = {
|
|
|
2391
2531
|
Ukrainian: "uk",
|
|
2392
2532
|
};
|
|
2393
2533
|
|
|
2534
|
+
var tencent = {
|
|
2535
|
+
"Simplified Chinese": "zh",
|
|
2536
|
+
"Traditional Chinese": "zh-TW",
|
|
2537
|
+
English: "en",
|
|
2538
|
+
Japanese: "ja",
|
|
2539
|
+
Korean: "ko",
|
|
2540
|
+
French: "fr",
|
|
2541
|
+
Spanish: "es",
|
|
2542
|
+
Italian: "it",
|
|
2543
|
+
German: "de",
|
|
2544
|
+
Turkish: "tr",
|
|
2545
|
+
Russian: "ru",
|
|
2546
|
+
Portuguese: "pt",
|
|
2547
|
+
Vietnamese: "vi",
|
|
2548
|
+
Indonesian: "id",
|
|
2549
|
+
Thai: "th",
|
|
2550
|
+
Malay: "ms",
|
|
2551
|
+
Arabic: "ar",
|
|
2552
|
+
Hindi: "hi",
|
|
2553
|
+
};
|
|
2554
|
+
|
|
2394
2555
|
const targetLanguages = {
|
|
2395
2556
|
azure: azure,
|
|
2396
2557
|
google: openai,
|
|
@@ -2398,6 +2559,7 @@ const targetLanguages = {
|
|
|
2398
2559
|
deepl: deepl,
|
|
2399
2560
|
amazon: amazon,
|
|
2400
2561
|
openai: openai,
|
|
2562
|
+
tencent: tencent,
|
|
2401
2563
|
};
|
|
2402
2564
|
|
|
2403
2565
|
function normalFromLanguage(from, engine) {
|