@yxw007/translate 0.4.1 → 1.0.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.
Files changed (56) hide show
  1. package/README.md +165 -56
  2. package/README_zh-CN.md +166 -52
  3. package/dist/browser/index.cjs +2303 -2886
  4. package/dist/browser/index.esm.js +2303 -2876
  5. package/dist/browser/index.esm.min.js +28 -1
  6. package/dist/browser/index.min.cjs +28 -1
  7. package/dist/browser/index.umd.js +2393 -2972
  8. package/dist/browser/index.umd.min.js +28 -1
  9. package/dist/engines/amazon.d.ts +7 -0
  10. package/dist/engines/azure.d.ts +9 -0
  11. package/dist/engines/baidu.d.ts +6 -0
  12. package/dist/engines/deepl.d.ts +88 -0
  13. package/dist/engines/google.d.ts +3 -0
  14. package/dist/engines/helper.d.ts +2 -0
  15. package/dist/engines/index.d.ts +16 -0
  16. package/dist/engines/openai.d.ts +8 -0
  17. package/dist/engines/tencent.d.ts +7 -0
  18. package/dist/engines/yandex.d.ts +5 -0
  19. package/dist/index.d.ts +20 -5342
  20. package/dist/language/check/baidu.d.ts +9 -0
  21. package/dist/language/check/index.d.ts +775 -0
  22. package/dist/language/engines/amazon.d.ts +158 -0
  23. package/dist/language/engines/azure.d.ts +278 -0
  24. package/dist/language/engines/baidu.d.ts +424 -0
  25. package/dist/language/engines/deepl.d.ts +77 -0
  26. package/dist/language/engines/google.d.ts +494 -0
  27. package/dist/language/engines/index.d.ts +1973 -0
  28. package/dist/language/engines/openai.d.ts +494 -0
  29. package/dist/language/engines/tencent.d.ts +44 -0
  30. package/dist/language/index.d.ts +8 -0
  31. package/dist/language/origin/deepl.d.ts +36 -0
  32. package/dist/language/origin/index.d.ts +998 -0
  33. package/dist/language/origin/tencent.d.ts +22 -0
  34. package/dist/language/target/amazon.d.ts +78 -0
  35. package/dist/language/target/azure.d.ts +138 -0
  36. package/dist/language/target/baidu.d.ts +211 -0
  37. package/dist/language/target/deepl.d.ts +39 -0
  38. package/dist/language/target/google.d.ts +246 -0
  39. package/dist/language/target/index.d.ts +1000 -0
  40. package/dist/language/target/tencent.d.ts +21 -0
  41. package/dist/node/index.cjs +2300 -2853
  42. package/dist/node/index.js +2299 -2844
  43. package/dist/node/index.min.cjs +28 -1
  44. package/dist/node/index.min.js +28 -1
  45. package/dist/package.json +14 -23
  46. package/dist/types/index.d.ts +56 -0
  47. package/dist/types/typescript.d.ts +1 -0
  48. package/dist/utils/cache.d.ts +15 -0
  49. package/dist/utils/check.d.ts +2 -0
  50. package/dist/utils/constant.d.ts +2 -0
  51. package/dist/utils/index.d.ts +6 -0
  52. package/dist/utils/logger.d.ts +6 -0
  53. package/dist/utils/node.d.ts +2 -0
  54. package/dist/utils/path.d.ts +1 -0
  55. package/dist/utils/utils.d.ts +7 -0
  56. package/package.json +14 -23
package/README.md CHANGED
@@ -9,6 +9,7 @@ English | [简体中文](./README_zh-CN.md)
9
9
  ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/yxw007/translate/ci.yml)
10
10
 
11
11
  ## ❓ Why do I need translate?
12
+
12
13
  1. a lot of translation tool libraries on the market, basically not very well-maintained
13
14
  2. not written by ts, not friendly enough when using the prompts
14
15
  3. single function, does not support batch translation Or only support a translation engine
@@ -17,6 +18,7 @@ English | [简体中文](./README_zh-CN.md)
17
18
  > Note: Translate helps you to solve all the above problems, and will expand more in the future!
18
19
 
19
20
  ## ✨ Features
21
+
20
22
  - 🌐 **Multi-environment support**: Node environment, browser environment
21
23
  - ✨ **Easy to use**: provides a concise API, you can easily help you to translate
22
24
  - 🌍 **Multi-translation engine support**: Google, Azure Translate, Amazon Translate, Deepl, Baidu, OpenAI, etc. (will expand more in the future)
@@ -26,6 +28,8 @@ English | [简体中文](./README_zh-CN.md)
26
28
 
27
29
  > **Special reminder: although the library has supported the use of the browser environment, but please only use the google engine translation (google does not need key), the use of other translation engine need to configure the key, the use of the front-end will lead to key leakage, do not do it**
28
30
 
31
+ > Browser bundles exclude Node-only helpers such as filesystem utilities. Keep browser usage focused on engines that are safe to expose client-side.
32
+
29
33
  ## 💻Translation engines, integration cases
30
34
 
31
35
  | Name | Support | Description |
@@ -39,6 +43,40 @@ English | [简体中文](./README_zh-CN.md)
39
43
  | tencent | ✔ | Commissioned and ready for use |
40
44
  | yandex | | I have not tuned in as I do not have a bank account supported by the platform (help from those who are in a position to do so is welcome and appreciated) |
41
45
 
46
+ ## 🛠 Development
47
+
48
+ This project now uses `rolldown`, `oxlint`, and `oxfmt` instead of `rollup`, `eslint`, and `prettier`.
49
+
50
+ ```bash
51
+ pnpm build
52
+ pnpm lint
53
+ pnpm lint:fix
54
+ pnpm format
55
+ pnpm format:check
56
+ pnpm test
57
+ ```
58
+
59
+ - `pnpm build`: builds Node and browser bundles with `rolldown`, then emits `.d.ts` files with TypeScript.
60
+ - `pnpm lint`: runs `oxlint` and `oxfmt --check`.
61
+ - `pnpm lint:fix`: applies `oxlint --fix` and formats files with `oxfmt`.
62
+ - `pnpm format`: formats the repository with `oxfmt`.
63
+ - `pnpm test`: runs stable offline-safe tests.
64
+
65
+ Integration tests that require real network access and third-party credentials are disabled by default. To run them manually, set `RUN_INTEGRATION_TESTS=true` before running `pnpm test`.
66
+
67
+ - Bash
68
+
69
+ ```bash
70
+ RUN_INTEGRATION_TESTS=true pnpm test
71
+ ```
72
+
73
+ - PowerShell
74
+
75
+ ```powershell
76
+ $env:RUN_INTEGRATION_TESTS = "true"
77
+ pnpm test
78
+ ```
79
+
42
80
  ## 🚀 Install
43
81
 
44
82
  - npm
@@ -53,7 +91,7 @@ English | [简体中文](./README_zh-CN.md)
53
91
  yarn add @yxw007/translate
54
92
  ```
55
93
 
56
- - pnpm
94
+ - pnpm
57
95
 
58
96
  ```bash
59
97
  pnpm i @yxw007/translate
@@ -64,18 +102,21 @@ English | [简体中文](./README_zh-CN.md)
64
102
  ### Node
65
103
 
66
104
  - ESM
105
+
67
106
  ```typescript
68
- import { translator, engines } from "@yxw007/translate"
107
+ import { translator, engines } from "@yxw007/translate";
69
108
  ```
70
109
 
71
110
  - Commonjs
111
+
72
112
  ```typescript
73
- const { translator, engines } = required("@yxw007/translate")
113
+ const { translator, engines } = required("@yxw007/translate");
74
114
  ```
75
115
 
76
116
  - Translation examples
117
+
77
118
  ```typescript
78
- translator.addEngine(engines.google());
119
+ translator.addEngine(engines.google);
79
120
  const res1 = await translator.translate("hello", { from: "en", to: "zh" });
80
121
  console.log(res1);
81
122
 
@@ -84,31 +125,36 @@ English | [简体中文](./README_zh-CN.md)
84
125
  ```
85
126
 
86
127
  Output results
128
+
87
129
  ```bash
88
130
  ['你好']
89
131
  ["你好", "好的"]
90
132
  ```
133
+
91
134
  - Language detection examples
135
+
92
136
  ```typescript
93
- translator.addEngine(engines.google());
94
- const res1 = await translator.checkLanguage("hello", { engine:"google" });
137
+ translator.addEngine(engines.google);
138
+ const res1 = await translator.checkLanguage("hello", { engine: "google" });
95
139
  console.log(res1);
96
-
97
140
  ```
141
+
98
142
  Output results
143
+
99
144
  ```bash
100
145
  en
101
146
  ```
102
147
 
103
148
  ### Browser
104
149
 
105
- use jsDelivr CDN
150
+ use jsDelivr CDN
106
151
 
107
152
  - `development`
153
+
108
154
  ```html
109
155
  <script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
110
156
  ```
111
-
157
+
112
158
  - `production`
113
159
 
114
160
  ```html
@@ -130,7 +176,7 @@ use jsDelivr CDN
130
176
  <script>
131
177
  (async () => {
132
178
  const { engines, translator } = translate;
133
- translator.addEngine(engines.google());
179
+ translator.addEngine(engines.google);
134
180
  const res = await translator.translate("hello", { from: "en", to: "zh" });
135
181
  console.log(res);
136
182
  })();
@@ -141,8 +187,7 @@ use jsDelivr CDN
141
187
 
142
188
  ```
143
189
 
144
-
145
- ## 📚 API
190
+ ## 📚 API
146
191
 
147
192
  ### Translator
148
193
 
@@ -166,6 +211,12 @@ class Translator {
166
211
  removeEngine(engineName: string) {
167
212
  ...
168
213
  }
214
+ getFromLanguages(engineName: string) {
215
+ ...
216
+ }
217
+ getToLanguages(engineName: string) {
218
+ ...
219
+ }
169
220
  translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
170
221
  ...
171
222
  }
@@ -179,18 +230,31 @@ Add a translation engine to transitorion engine to translator
179
230
  ```typescript
180
231
  type Engine = {
181
232
  name: string;
182
- translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
233
+ getFromLanguages(): Record<string, string>;
234
+ getToLanguages(): Record<string, string>;
235
+ normalFromLanguage(language?: string): string;
236
+ normalToLanguage(language?: string): string;
237
+ translate(text: string | string[], options: EngineTranslateOptions) {
183
238
  };
184
239
  ```
185
240
 
186
241
  #### `translate`
187
242
 
188
- You can pass a text or pass a text array, which will return a translated ```Promise<string[]>```
243
+ You can pass a text or pass a text array, which will return a translated `Promise<string[]>`
189
244
 
190
245
  ```typescript
191
246
  translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>)
192
247
  ```
193
248
 
249
+ #### `getFromLanguages` / `getToLanguages`
250
+
251
+ Read the language list for the specified engine.
252
+
253
+ ```typescript
254
+ translator.getFromLanguages("google");
255
+ translator.getToLanguages("google");
256
+ ```
257
+
194
258
  #### TranslateOptions
195
259
 
196
260
  ```typescript
@@ -198,7 +262,7 @@ export interface TranslateOptions {
198
262
  from?: FromLanguage<T>;
199
263
  to: ToLanguage<T>;
200
264
  engine?: Engines;
201
- /**
265
+ /**
202
266
  * Cache time in milliseconds
203
267
  */
204
268
  cache_time?: number;
@@ -209,17 +273,60 @@ export interface TranslateOptions {
209
273
  }
210
274
  ```
211
275
 
212
- > Note: To learn more about the support of each engine language, go to the following directory to view the corresponding configurations
213
-
214
- - from: [https://github.com/yxw007/translate/blob/master/src/language/origin/index.ts](https://github.com/yxw007/translate/blob/master/src/language/origin/index.ts)
215
- - to: [https://github.com/yxw007/translate/blob/master/src/language/target/index.ts](https://github.com/yxw007/translate/blob/master/src/language/target/index.ts)
276
+ > Note: Each engine now maintains its own `from`/`to` language configuration. You can inspect the engine-scoped configuration under `src/language/engines/*`.
216
277
 
217
278
  ### Each translation of Engine's Option
218
279
 
219
280
  #### BaseEngineOption
220
281
 
221
282
  ```typescript
222
- interface BaseEngineOption {}
283
+ interface BaseEngineOption {
284
+ fromLanguages?: Record<string, string>;
285
+ toLanguages?: Record<string, string>;
286
+ }
287
+ ```
288
+
289
+ > `fromLanguages` / `toLanguages` are only initialization options for engine factories that need custom language tables. They are not exposed as public fields on the engine instance.
290
+
291
+ #### Custom Engine
292
+
293
+ ```typescript
294
+ import { Translator, type Engine } from "@yxw007/translate";
295
+
296
+ const translator = new Translator();
297
+
298
+ const fromLanguages = { Auto: "auto", English: "en" };
299
+ const toLanguages = { Chinese: "zh", Japanese: "ja" };
300
+
301
+ const customEngine: Engine = {
302
+ name: "custom",
303
+ getFromLanguages() {
304
+ return fromLanguages;
305
+ },
306
+ getToLanguages() {
307
+ return toLanguages;
308
+ },
309
+ normalFromLanguage(language) {
310
+ if (!language || language === "auto") return "auto";
311
+ return fromLanguages[language as keyof typeof fromLanguages] ?? "";
312
+ },
313
+ normalToLanguage(language) {
314
+ if (!language) return "";
315
+ return toLanguages[language as keyof typeof toLanguages] ?? "";
316
+ },
317
+ async translate(text, options) {
318
+ const list = Array.isArray(text) ? text : [text];
319
+ return list.map((item) => `[${options.from}->${options.to}] ${item}`);
320
+ },
321
+ };
322
+
323
+ translator.addEngine(customEngine);
324
+ ```
325
+
326
+ Built-in engines are directly referenced:
327
+
328
+ ```typescript
329
+ translator.addEngine(engines.google);
223
330
  ```
224
331
 
225
332
  #### AzureEngineOption
@@ -235,11 +342,10 @@ interface AzureEngineOption extends BaseEngineOption {
235
342
 
236
343
  - Relative document:[rest-api-guide](https://learn.microsoft.com/zh-cn/azure/ai-services/translator/reference/rest-api-guide?WT.mc_id=Portal-Microsoft_Azure_ProjectOxford)
237
344
 
238
-
239
345
  #### AmazonEngineOption
240
346
 
241
347
  ```typescript
242
- interface AmazonEngineOption extends BaseEngineOption{
348
+ interface AmazonEngineOption extends BaseEngineOption {
243
349
  region: string;
244
350
  accessKeyId: string;
245
351
  secretAccessKey: string;
@@ -250,7 +356,7 @@ interface AmazonEngineOption extends BaseEngineOption{
250
356
 
251
357
  - Related document:https://docs.aws.amazon.com/translate/latest/dg/what-is.html
252
358
  - Related library:https://www.npmjs.com/package/@aws-sdk/client-translate
253
-
359
+
254
360
  #### BaiduEngineOption
255
361
 
256
362
  ```typescript
@@ -322,34 +428,34 @@ export type OpenAIModel = (typeof OPEN_AI_MODELS)[number];
322
428
 
323
429
  ```typescript
324
430
  export interface TencentEngineOption extends BaseEngineOption {
325
- secretId: string;
326
- secretKey: string;
327
- region?: string;
431
+ secretId: string;
432
+ secretKey: string;
433
+ region?: string;
328
434
  }
329
435
  ```
330
436
 
331
- > Description: Option Param Please obtain it from the corresponding platform.
437
+ > Description: Option Param Please obtain it from the corresponding platform.
438
+
332
439
  - Related documentation:https://console.cloud.tencent.com/cam/capi
333
440
 
334
441
  - Region Configuration table
335
- | 地域 | 取值 |
442
+ | 地域 | 取值 |
336
443
  | ---------------------- | ---------------- |
337
- | 亚太东南(曼谷) | ap-bangkok |
338
- | 华北地区(北京) | ap-beijing |
339
- | 西南地区(成都) | ap-chengdu |
340
- | 西南地区(重庆) | ap-chongqing |
341
- | 华南地区(广州) | ap-guangzhou |
342
- | 港澳台地区(中国香港) | ap-hongkong |
343
- | 亚太东北(首尔) | ap-seoul |
344
- | 华东地区(上海) | ap-shanghai |
345
- | 华东地区(上海金融) | ap-shanghai-fsi |
346
- | 华南地区(深圳金融) | ap-shenzhen-fsi |
347
- | 亚太东南(新加坡) | ap-singapore |
348
- | 亚太东北(东京) | ap-tokyo |
349
- | 欧洲地区(法兰克福) | eu-frankfurt |
350
- | 美国东部(弗吉尼亚) | na-ashburn |
351
- | 美国西部(硅谷) | na-siliconvalley |
352
-
444
+ | 亚太东南(曼谷) | ap-bangkok |
445
+ | 华北地区(北京) | ap-beijing |
446
+ | 西南地区(成都) | ap-chengdu |
447
+ | 西南地区(重庆) | ap-chongqing |
448
+ | 华南地区(广州) | ap-guangzhou |
449
+ | 港澳台地区(中国香港) | ap-hongkong |
450
+ | 亚太东北(首尔) | ap-seoul |
451
+ | 华东地区(上海) | ap-shanghai |
452
+ | 华东地区(上海金融) | ap-shanghai-fsi |
453
+ | 华南地区(深圳金融) | ap-shenzhen-fsi |
454
+ | 亚太东南(新加坡) | ap-singapore |
455
+ | 亚太东北(东京) | ap-tokyo |
456
+ | 欧洲地区(法兰克福) | eu-frankfurt |
457
+ | 美国东部(弗吉尼亚) | na-ashburn |
458
+ | 美国西部(硅谷) | na-siliconvalley |
353
459
 
354
460
  ## 🤝 Contribute
355
461
 
@@ -362,8 +468,8 @@ export interface TencentEngineOption extends BaseEngineOption {
362
468
  ```
363
469
 
364
470
  - Add new Engine
365
-
366
471
  - Add a new platform ENGINE plugin
472
+
367
473
  ```typescript
368
474
  export interface XXEngineOption extends BaseEngineOption {
369
475
  key: string;
@@ -374,7 +480,7 @@ export interface TencentEngineOption extends BaseEngineOption {
374
480
  const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
375
481
  return {
376
482
  name: "yandex",
377
- async checkLanguage<T extends Engines>(text: string): Promise<string> {
483
+ async checkLanguage<T extends Engines>(text: string): Promise<string> {
378
484
  //TODO: This can be done with translate, in which case the target language configuration is reused.
379
485
  },
380
486
  async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
@@ -395,8 +501,9 @@ export interface TencentEngineOption extends BaseEngineOption {
395
501
  };
396
502
  }
397
503
  ```
398
- - Add the plugin to Engines(Location:```/src/engines/index.ts```)
399
-
504
+
505
+ - Add the plugin to Engines(Location:`/src/engines/index.ts`)
506
+
400
507
  ```typescript
401
508
  import { xx } from "./xx";
402
509
  export const engines = {
@@ -406,18 +513,19 @@ export interface TencentEngineOption extends BaseEngineOption {
406
513
  baidu,
407
514
  deepl,
408
515
  openai,
409
- xx
516
+ xx,
410
517
  } as const;
411
518
  ```
519
+
412
520
  - Add the origin language configuration supported by the engine
413
-
521
+
414
522
  ```typescript
415
523
  //Note: If the origin and target languages are the same, you can directly use the target language to configure them, otherwise please configure them separately
416
- //src/language/origin/index.ts
524
+ //src/language/origin/index.ts
417
525
  import azure from "../target/azure";
418
526
  ...
419
527
  import xxx from "../target/xxx"
420
-
528
+
421
529
  export const originLanguages = {
422
530
  azure: azure,
423
531
  ...
@@ -439,9 +547,9 @@ export interface TencentEngineOption extends BaseEngineOption {
439
547
  ```
440
548
 
441
549
  - Add the target language that is supported by the engine
442
-
550
+
443
551
  ```typescript
444
- //src/language/target/index.ts
552
+ //src/language/target/index.ts
445
553
  import azure from "./azure";
446
554
  ...
447
555
  import xxx from "./amazon";
@@ -463,8 +571,10 @@ export interface TencentEngineOption extends BaseEngineOption {
463
571
  ...
464
572
  xxx: ValuesOf<typeof xxx>;
465
573
  };
574
+ ```
466
575
 
467
576
  - Build
577
+
468
578
  ```bash
469
579
  pnpm build
470
580
  ```
@@ -476,8 +586,8 @@ export interface TencentEngineOption extends BaseEngineOption {
476
586
 
477
587
  > **Tips: At present, the library can be used normally. Welcome everyone to experience. If you have any questions and suggestions, you can mention the feedback to me.If you are interested, you are welcome to join, let us improve this tool together. Help to click star ⭐, let more people know this tool, thank you for everyone🙏**
478
588
 
479
-
480
589
  ## 🌹 Thanks
590
+
481
591
  - [franciscop/translate](https://github.com/franciscop/translate.git)
482
592
 
483
593
  > Note:Thanks to [franciscop/translate](https://github.com/franciscop/translate.git) for giving me ideas for a quick implementation of this library, and also indirectly some of his code. Much appreciated.🙏
@@ -485,4 +595,3 @@ export interface TencentEngineOption extends BaseEngineOption {
485
595
  ## 📄 License
486
596
 
487
597
  Translate is released under the MIT license. See the [`LICENSE`](./LICENSE) file.
488
-