@yxw007/translate 0.2.0 → 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 CHANGED
@@ -306,6 +306,39 @@ export type OpenAIModel = (typeof OPEN_AI_MODELS)[number];
306
306
 
307
307
  - Related document:https://platform.openai.com/settings/organization/api-keys
308
308
 
309
+ #### TencentEnginOption
310
+
311
+ ```typescript
312
+ export interface TencentEngineOption extends BaseEngineOption {
313
+ secretId: string;
314
+ secretKey: string;
315
+ region?: string;
316
+ }
317
+ ```
318
+
319
+ > Description: Option Param Please obtain it from the corresponding platform.
320
+ - Related documentation:https://console.cloud.tencent.com/cam/capi
321
+
322
+ - Region Configuration table
323
+ | 地域 | 取值 |
324
+ | ---------------------- | ---------------- |
325
+ | 亚太东南(曼谷) | ap-bangkok |
326
+ | 华北地区(北京) | ap-beijing |
327
+ | 西南地区(成都) | ap-chengdu |
328
+ | 西南地区(重庆) | ap-chongqing |
329
+ | 华南地区(广州) | ap-guangzhou |
330
+ | 港澳台地区(中国香港) | ap-hongkong |
331
+ | 亚太东北(首尔) | ap-seoul |
332
+ | 华东地区(上海) | ap-shanghai |
333
+ | 华东地区(上海金融) | ap-shanghai-fsi |
334
+ | 华南地区(深圳金融) | ap-shenzhen-fsi |
335
+ | 亚太东南(新加坡) | ap-singapore |
336
+ | 亚太东北(东京) | ap-tokyo |
337
+ | 欧洲地区(法兰克福) | eu-frankfurt |
338
+ | 美国东部(弗吉尼亚) | na-ashburn |
339
+ | 美国西部(硅谷) | na-siliconvalley |
340
+
341
+
309
342
  ## 🤝 Contribute
310
343
 
311
344
  > Special attention: Please create a new branch based on the master, develop on the new branch, and create PR to Master after development.
package/README_zh-CN.md CHANGED
@@ -38,6 +38,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
38
38
  | baidu | √ | 已投产,可以正常使用 |
39
39
  | deepl | √ | 已投产,可以正常使用 |
40
40
  | openai | √ | 已投产,可以正常使用 |
41
+ | tencent | √ | 已投产,可以正常使用 |
41
42
  | yandex | | 由于我没有平台支持的银行账号,所以未调通(欢迎有条件的朋友帮忙调通,感谢) |
42
43
 
43
44
 
@@ -308,6 +309,39 @@ export type OpenAIModel = (typeof OPEN_AI_MODELS)[number];
308
309
 
309
310
  - 相关文档:https://platform.openai.com/settings/organization/api-keys
310
311
 
312
+ #### TencentEnginOption
313
+
314
+ ```typescript
315
+ export interface TencentEngineOption extends BaseEngineOption {
316
+ secretId: string;
317
+ secretKey: string;
318
+ region?: string;
319
+ }
320
+ ```
321
+
322
+ > 说明:option param 请从对应平台获取。
323
+ - 相关文档:https://console.cloud.tencent.com/cam/capi
324
+
325
+ - region 配置表
326
+ | 地域 | 取值 |
327
+ | ---------------------- | ---------------- |
328
+ | 亚太东南(曼谷) | ap-bangkok |
329
+ | 华北地区(北京) | ap-beijing |
330
+ | 西南地区(成都) | ap-chengdu |
331
+ | 西南地区(重庆) | ap-chongqing |
332
+ | 华南地区(广州) | ap-guangzhou |
333
+ | 港澳台地区(中国香港) | ap-hongkong |
334
+ | 亚太东北(首尔) | ap-seoul |
335
+ | 华东地区(上海) | ap-shanghai |
336
+ | 华东地区(上海金融) | ap-shanghai-fsi |
337
+ | 华南地区(深圳金融) | ap-shenzhen-fsi |
338
+ | 亚太东南(新加坡) | ap-singapore |
339
+ | 亚太东北(东京) | ap-tokyo |
340
+ | 欧洲地区(法兰克福) | eu-frankfurt |
341
+ | 美国东部(弗吉尼亚) | na-ashburn |
342
+ | 美国西部(硅谷) | na-siliconvalley |
343
+
344
+
311
345
  ## 🤝 贡献
312
346
 
313
347
  > 特别注意:请基于master创建一个新分支,在新分支上开发,开发完后创建PR至master
@@ -1,4 +1,4 @@
1
- // translate v0.2.0 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.3.0 Copyright (c) 2025 Potter<aa4790139@gmail.com> and contributors
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -7,6 +7,7 @@ require('fs/promises');
7
7
  require('fs');
8
8
  require('path');
9
9
  var clientTranslate = require('@aws-sdk/client-translate');
10
+ var crypto = require('crypto');
10
11
 
11
12
  class TranslationError extends Error {
12
13
  region;
@@ -1629,6 +1630,121 @@ function openai$1(options) {
1629
1630
  };
1630
1631
  }
1631
1632
 
1633
+ function sha256(message, secret = "", encoding) {
1634
+ if (encoding) {
1635
+ return crypto.createHmac("sha256", secret).update(message).digest(encoding);
1636
+ }
1637
+ return crypto.createHmac("sha256", secret).update(message).digest();
1638
+ }
1639
+ function getHash(message, encoding = "hex") {
1640
+ return crypto.createHash("sha256").update(message).digest(encoding);
1641
+ }
1642
+ function getDate(timestamp) {
1643
+ const date = new Date(timestamp * 1000);
1644
+ return `${date.getUTCFullYear()}-${("0" + (date.getUTCMonth() + 1)).slice(-2)}-${("0" + date.getUTCDate()).slice(-2)}`;
1645
+ }
1646
+ function buildAuthorization({ secretId, secretKey, service, host, payload, httpRequestMethod, action, apiVersion, region }) {
1647
+ const timestamp = Math.floor(Date.now() / 1000);
1648
+ const date = getDate(timestamp);
1649
+ const canonicalUri = "/";
1650
+ const canonicalQueryString = "";
1651
+ const canonicalHeaders = "content-type:application/json; charset=utf-8\nhost:" + host + "\n";
1652
+ const signedHeaders = "content-type;host";
1653
+ const hashedRequestPayload = getHash(payload);
1654
+ const canonicalRequest = [
1655
+ httpRequestMethod,
1656
+ canonicalUri,
1657
+ canonicalQueryString,
1658
+ canonicalHeaders,
1659
+ signedHeaders,
1660
+ hashedRequestPayload,
1661
+ ].join("\n");
1662
+ const algorithm = "TC3-HMAC-SHA256";
1663
+ const credentialScope = `${date}/${service}/tc3_request`;
1664
+ const hashedCanonicalRequest = getHash(canonicalRequest);
1665
+ const stringToSign = [algorithm, timestamp, credentialScope, hashedCanonicalRequest].join("\n");
1666
+ const kDate = sha256(date, "TC3" + secretKey);
1667
+ const kService = sha256(service, kDate);
1668
+ const kSigning = sha256("tc3_request", kService);
1669
+ const signature = sha256(stringToSign, kSigning, "hex");
1670
+ const authorization = `${algorithm} Credential=${secretId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
1671
+ const headers = {
1672
+ Authorization: authorization,
1673
+ "Content-Type": "application/json; charset=utf-8",
1674
+ Host: host,
1675
+ "X-TC-Action": action,
1676
+ "X-TC-Timestamp": timestamp,
1677
+ "X-TC-Version": apiVersion,
1678
+ };
1679
+ if (region)
1680
+ headers["X-TC-Region"] = region;
1681
+ return headers;
1682
+ }
1683
+ function tencent$2(options) {
1684
+ const { secretId, secretKey, region = "ap-guangzhou" } = options;
1685
+ const name = "tencent";
1686
+ const host = "tmt.tencentcloudapi.com";
1687
+ const endpoint = `https://${host}/`;
1688
+ const service = "tmt";
1689
+ const apiVersion = "2018-03-21";
1690
+ const action = "TextTranslate";
1691
+ function checkOptions() {
1692
+ if (!secretId || !secretKey) {
1693
+ throw new TranslationError(name, `${name} secretId and secretKey are required`);
1694
+ }
1695
+ }
1696
+ return {
1697
+ name,
1698
+ async translate(text, opts) {
1699
+ checkOptions();
1700
+ const { from = "auto", to } = opts;
1701
+ const source = from === "auto" ? "" : from;
1702
+ const payloadObj = {
1703
+ SourceText: Array.isArray(text) ? text.join("\n") : text,
1704
+ Source: source,
1705
+ Target: to,
1706
+ ProjectId: 0,
1707
+ };
1708
+ const payload = JSON.stringify(payloadObj);
1709
+ const headers = buildAuthorization({
1710
+ secretId,
1711
+ secretKey,
1712
+ service,
1713
+ host,
1714
+ payload,
1715
+ httpRequestMethod: "POST",
1716
+ action,
1717
+ apiVersion,
1718
+ region,
1719
+ });
1720
+ try {
1721
+ const res = await fetch(endpoint, {
1722
+ method: "POST",
1723
+ headers,
1724
+ body: payload,
1725
+ });
1726
+ if (!res.ok) {
1727
+ throw new TranslationError(name, `HTTP ${res.status}: ${await res.text()}`);
1728
+ }
1729
+ const data = await res.json();
1730
+ if (data.Response?.Error) {
1731
+ throw new TranslationError(name, `Tencent translate fail: ${data.Response.Error.Code}, ${data.Response.Error.Message}`);
1732
+ }
1733
+ const translatedResults = data.Response?.TargetText.split("\n") ?? [];
1734
+ if (!Array.isArray(translatedResults) || translatedResults.length === 0) {
1735
+ throw new TranslationError(name, "Translate fail! No result returned");
1736
+ }
1737
+ return translatedResults;
1738
+ }
1739
+ catch (error) {
1740
+ if (error instanceof TranslationError)
1741
+ throw error;
1742
+ throw new TranslationError(name, `Translation failed: ${error}`);
1743
+ }
1744
+ },
1745
+ };
1746
+ }
1747
+
1632
1748
  const engines = {
1633
1749
  google,
1634
1750
  azure: azure$1,
@@ -1636,6 +1752,7 @@ const engines = {
1636
1752
  baidu: baidu$1,
1637
1753
  deepl: deepl$2,
1638
1754
  openai: openai$1,
1755
+ tencent: tencent$2,
1639
1756
  };
1640
1757
 
1641
1758
  var azure = {
@@ -2269,6 +2386,28 @@ var deepl$1 = {
2269
2386
  Ukrainian: "uk",
2270
2387
  };
2271
2388
 
2389
+ var tencent$1 = {
2390
+ Auto: "auto",
2391
+ Chinese: "zh",
2392
+ TraditionalChinese: "zh-TW",
2393
+ English: "en",
2394
+ Japanese: "ja",
2395
+ Korean: "ko",
2396
+ French: "fr",
2397
+ Spanish: "es",
2398
+ Italian: "it",
2399
+ German: "de",
2400
+ Turkish: "tr",
2401
+ Russian: "ru",
2402
+ Portuguese: "pt",
2403
+ Vietnamese: "vi",
2404
+ Indonesian: "id",
2405
+ Thai: "th",
2406
+ Malay: "ms",
2407
+ Arabic: "ar",
2408
+ Hindi: "hi",
2409
+ };
2410
+
2272
2411
  var amazon = {
2273
2412
  Afrikaans: "af",
2274
2413
  Albanian: "sq",
@@ -2354,6 +2493,7 @@ const originLanguages = {
2354
2493
  deepl: deepl$1,
2355
2494
  amazon: amazon,
2356
2495
  openai: openai,
2496
+ tencent: tencent$1,
2357
2497
  };
2358
2498
 
2359
2499
  var deepl = {
@@ -2395,6 +2535,27 @@ var deepl = {
2395
2535
  Ukrainian: "uk",
2396
2536
  };
2397
2537
 
2538
+ var tencent = {
2539
+ "Simplified Chinese": "zh",
2540
+ "Traditional Chinese": "zh-TW",
2541
+ English: "en",
2542
+ Japanese: "ja",
2543
+ Korean: "ko",
2544
+ French: "fr",
2545
+ Spanish: "es",
2546
+ Italian: "it",
2547
+ German: "de",
2548
+ Turkish: "tr",
2549
+ Russian: "ru",
2550
+ Portuguese: "pt",
2551
+ Vietnamese: "vi",
2552
+ Indonesian: "id",
2553
+ Thai: "th",
2554
+ Malay: "ms",
2555
+ Arabic: "ar",
2556
+ Hindi: "hi",
2557
+ };
2558
+
2398
2559
  const targetLanguages = {
2399
2560
  azure: azure,
2400
2561
  google: openai,
@@ -2402,6 +2563,7 @@ const targetLanguages = {
2402
2563
  deepl: deepl,
2403
2564
  amazon: amazon,
2404
2565
  openai: openai,
2566
+ tencent: tencent,
2405
2567
  };
2406
2568
 
2407
2569
  function normalFromLanguage(from, engine) {