@yxw007/translate 0.4.0 → 0.5.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Potter.yan
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Potter.yan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -53,7 +53,7 @@ English | [简体中文](./README_zh-CN.md)
53
53
  yarn add @yxw007/translate
54
54
  ```
55
55
 
56
- - pnpm
56
+ - pnpm
57
57
 
58
58
  ```bash
59
59
  pnpm i @yxw007/translate
@@ -75,7 +75,7 @@ English | [简体中文](./README_zh-CN.md)
75
75
 
76
76
  - Translation examples
77
77
  ```typescript
78
- translator.addEngine(engines.google());
78
+ translator.addEngine(engines.google);
79
79
  const res1 = await translator.translate("hello", { from: "en", to: "zh" });
80
80
  console.log(res1);
81
81
 
@@ -90,9 +90,9 @@ English | [简体中文](./README_zh-CN.md)
90
90
  ```
91
91
  - Language detection examples
92
92
  ```typescript
93
- translator.addEngine(engines.google());
93
+ translator.addEngine(engines.google);
94
94
  const res1 = await translator.checkLanguage("hello", { engine:"google" });
95
- console.log("en");
95
+ console.log(res1);
96
96
 
97
97
  ```
98
98
  Output results
@@ -102,13 +102,13 @@ English | [简体中文](./README_zh-CN.md)
102
102
 
103
103
  ### Browser
104
104
 
105
- use jsDelivr CDN
105
+ use jsDelivr CDN
106
106
 
107
107
  - `development`
108
108
  ```html
109
109
  <script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
110
110
  ```
111
-
111
+
112
112
  - `production`
113
113
 
114
114
  ```html
@@ -130,7 +130,7 @@ use jsDelivr CDN
130
130
  <script>
131
131
  (async () => {
132
132
  const { engines, translator } = translate;
133
- translator.addEngine(engines.google());
133
+ translator.addEngine(engines.google);
134
134
  const res = await translator.translate("hello", { from: "en", to: "zh" });
135
135
  console.log(res);
136
136
  })();
@@ -142,7 +142,7 @@ use jsDelivr CDN
142
142
  ```
143
143
 
144
144
 
145
- ## 📚 API
145
+ ## 📚 API
146
146
 
147
147
  ### Translator
148
148
 
@@ -166,6 +166,12 @@ class Translator {
166
166
  removeEngine(engineName: string) {
167
167
  ...
168
168
  }
169
+ getFromLanguages(engineName: string) {
170
+ ...
171
+ }
172
+ getToLanguages(engineName: string) {
173
+ ...
174
+ }
169
175
  translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
170
176
  ...
171
177
  }
@@ -179,7 +185,11 @@ Add a translation engine to transitorion engine to translator
179
185
  ```typescript
180
186
  type Engine = {
181
187
  name: string;
182
- translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
188
+ getFromLanguages(): Record<string, string>;
189
+ getToLanguages(): Record<string, string>;
190
+ normalFromLanguage(language?: string): string;
191
+ normalToLanguage(language?: string): string;
192
+ translate(text: string | string[], options: EngineTranslateOptions) {
183
193
  };
184
194
  ```
185
195
 
@@ -191,6 +201,15 @@ You can pass a text or pass a text array, which will return a translated ```Prom
191
201
  translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>)
192
202
  ```
193
203
 
204
+ #### `getFromLanguages` / `getToLanguages`
205
+
206
+ Read the language list for the specified engine.
207
+
208
+ ```typescript
209
+ translator.getFromLanguages("google")
210
+ translator.getToLanguages("google")
211
+ ```
212
+
194
213
  #### TranslateOptions
195
214
 
196
215
  ```typescript
@@ -209,17 +228,60 @@ export interface TranslateOptions {
209
228
  }
210
229
  ```
211
230
 
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)
231
+ > Note: Each engine now maintains its own `from`/`to` language configuration. You can inspect the engine-scoped configuration under `src/language/engines/*`.
216
232
 
217
233
  ### Each translation of Engine's Option
218
234
 
219
235
  #### BaseEngineOption
220
236
 
221
237
  ```typescript
222
- interface BaseEngineOption {}
238
+ interface BaseEngineOption {
239
+ fromLanguages?: Record<string, string>;
240
+ toLanguages?: Record<string, string>;
241
+ }
242
+ ```
243
+
244
+ > `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.
245
+
246
+ #### Custom Engine
247
+
248
+ ```typescript
249
+ import { Translator, type Engine } from "@yxw007/translate";
250
+
251
+ const translator = new Translator();
252
+
253
+ const fromLanguages = { Auto: "auto", English: "en" };
254
+ const toLanguages = { Chinese: "zh", Japanese: "ja" };
255
+
256
+ const customEngine: Engine = {
257
+ name: "custom",
258
+ getFromLanguages() {
259
+ return fromLanguages;
260
+ },
261
+ getToLanguages() {
262
+ return toLanguages;
263
+ },
264
+ normalFromLanguage(language) {
265
+ if (!language || language === "auto") return "auto";
266
+ return fromLanguages[language as keyof typeof fromLanguages] ?? "";
267
+ },
268
+ normalToLanguage(language) {
269
+ if (!language) return "";
270
+ return toLanguages[language as keyof typeof toLanguages] ?? "";
271
+ },
272
+ async translate(text, options) {
273
+ const list = Array.isArray(text) ? text : [text];
274
+ return list.map((item) => `[${options.from}->${options.to}] ${item}`);
275
+ },
276
+ };
277
+
278
+ translator.addEngine(customEngine);
279
+ ```
280
+
281
+ Built-in engines are directly referenced:
282
+
283
+ ```typescript
284
+ translator.addEngine(engines.google);
223
285
  ```
224
286
 
225
287
  #### AzureEngineOption
@@ -250,7 +312,7 @@ interface AmazonEngineOption extends BaseEngineOption{
250
312
 
251
313
  - Related document:https://docs.aws.amazon.com/translate/latest/dg/what-is.html
252
314
  - Related library:https://www.npmjs.com/package/@aws-sdk/client-translate
253
-
315
+
254
316
  #### BaiduEngineOption
255
317
 
256
318
  ```typescript
@@ -328,7 +390,7 @@ export interface TencentEngineOption extends BaseEngineOption {
328
390
  }
329
391
  ```
330
392
 
331
- > Description: Option Param Please obtain it from the corresponding platform.
393
+ > Description: Option Param Please obtain it from the corresponding platform.
332
394
  - Related documentation:https://console.cloud.tencent.com/cam/capi
333
395
 
334
396
  - Region Configuration table
@@ -374,6 +436,9 @@ export interface TencentEngineOption extends BaseEngineOption {
374
436
  const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
375
437
  return {
376
438
  name: "yandex",
439
+ async checkLanguage<T extends Engines>(text: string): Promise<string> {
440
+ //TODO: This can be done with translate, in which case the target language configuration is reused.
441
+ },
377
442
  async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
378
443
  const { from, to } = opts;
379
444
  if (!Array.isArray(text)) {
@@ -393,7 +458,7 @@ export interface TencentEngineOption extends BaseEngineOption {
393
458
  }
394
459
  ```
395
460
  - Add the plugin to Engines(Location:```/src/engines/index.ts```)
396
-
461
+
397
462
  ```typescript
398
463
  import { xx } from "./xx";
399
464
  export const engines = {
@@ -407,14 +472,14 @@ export interface TencentEngineOption extends BaseEngineOption {
407
472
  } as const;
408
473
  ```
409
474
  - Add the origin language configuration supported by the engine
410
-
475
+
411
476
  ```typescript
412
477
  //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
413
- //src/language/origin/index.ts
478
+ //src/language/origin/index.ts
414
479
  import azure from "../target/azure";
415
480
  ...
416
481
  import xxx from "../target/xxx"
417
-
482
+
418
483
  export const originLanguages = {
419
484
  azure: azure,
420
485
  ...
@@ -436,9 +501,9 @@ export interface TencentEngineOption extends BaseEngineOption {
436
501
  ```
437
502
 
438
503
  - Add the target language that is supported by the engine
439
-
504
+
440
505
  ```typescript
441
- //src/language/target/index.ts
506
+ //src/language/target/index.ts
442
507
  import azure from "./azure";
443
508
  ...
444
509
  import xxx from "./amazon";
package/README_zh-CN.md CHANGED
@@ -56,7 +56,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
56
56
  yarn add @yxw007/translate
57
57
  ```
58
58
 
59
- - pnpm
59
+ - pnpm
60
60
 
61
61
  ```bash
62
62
  pnpm i @yxw007/translate
@@ -78,7 +78,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
78
78
 
79
79
  - 翻译例子
80
80
  ```typescript
81
- translator.addEngine(engines.google());
81
+ translator.addEngine(engines.google);
82
82
  const res1 = await translator.translate("hello", { from: "en", to: "zh" });
83
83
  console.log(res1);
84
84
 
@@ -93,9 +93,9 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
93
93
  ```
94
94
  - 语言检测例子
95
95
  ```typescript
96
- translator.addEngine(engines.google());
96
+ translator.addEngine(engines.google);
97
97
  const res1 = await translator.checkLanguage("hello", { engine:"google" });
98
- console.log("en");
98
+ console.log(res1);
99
99
  ```
100
100
 
101
101
  输出结果
@@ -105,13 +105,13 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
105
105
 
106
106
  ### Browser
107
107
 
108
- 使用 jsDelivr CDN
108
+ 使用 jsDelivr CDN
109
109
 
110
110
  - `development`
111
111
  ```html
112
112
  <script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
113
113
  ```
114
-
114
+
115
115
  - `production`
116
116
 
117
117
  ```html
@@ -133,7 +133,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
133
133
  <script>
134
134
  (async () => {
135
135
  const { engines, translator } = translate;
136
- translator.addEngine(engines.google());
136
+ translator.addEngine(engines.google);
137
137
  const res = await translator.translate("hello", { from: "en", to: "zh" });
138
138
  console.log(res);
139
139
  })();
@@ -144,7 +144,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
144
144
 
145
145
  ```
146
146
 
147
- ## 📚 API
147
+ ## 📚 API
148
148
 
149
149
  ### Translator
150
150
 
@@ -168,6 +168,12 @@ class Translator {
168
168
  removeEngine(engineName: string) {
169
169
  ...
170
170
  }
171
+ getFromLanguages(engineName: string) {
172
+ ...
173
+ }
174
+ getToLanguages(engineName: string) {
175
+ ...
176
+ }
171
177
  translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
172
178
  ...
173
179
  }
@@ -181,7 +187,11 @@ class Translator {
181
187
  ```typescript
182
188
  export type Engine = {
183
189
  name: string;
184
- translate: <T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) => Promise<string[]>;
190
+ getFromLanguages: () => Record<string, string>;
191
+ getToLanguages: () => Record<string, string>;
192
+ normalFromLanguage: (language?: string) => string;
193
+ normalToLanguage: (language?: string) => string;
194
+ translate: (text: string | string[], opts: EngineTranslateOptions) => Promise<string[]>;
185
195
  };
186
196
  ```
187
197
 
@@ -193,6 +203,15 @@ export type Engine = {
193
203
  translate: <T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) => Promise<string[]>;
194
204
  ```
195
205
 
206
+ #### `getFromLanguages` / `getToLanguages`
207
+
208
+ 读取指定 Engine 的语言列表。
209
+
210
+ ```typescript
211
+ translator.getFromLanguages("google")
212
+ translator.getToLanguages("google")
213
+ ```
214
+
196
215
  #### TranslateOptions
197
216
 
198
217
  ```typescript
@@ -211,17 +230,60 @@ export type TranslateOptions<T extends Engines> = {
211
230
  };
212
231
  ```
213
232
 
214
- > 提示:如需了解各Engine语言支持情况,请到以下目录查看对应配置
215
-
216
- - 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)
217
- - 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)
233
+ > 提示:现在每个 Engine 都单独维护自己的 `from`/`to` 语言配置,可直接查看 `src/language/engines/*`。
218
234
 
219
235
  ### 各翻译Engine的Option
220
236
 
221
237
  #### BaseEngineOption
222
238
 
223
239
  ```typescript
224
- interface BaseEngineOption {}
240
+ interface BaseEngineOption {
241
+ fromLanguages?: Record<string, string>;
242
+ toLanguages?: Record<string, string>;
243
+ }
244
+ ```
245
+
246
+ > `fromLanguages` / `toLanguages` 仅用于某些工厂型 engine 初始化时传入自定义语言表,不会作为 engine 实例的公开字段暴露给上层。
247
+
248
+ #### 自定义 Engine
249
+
250
+ ```typescript
251
+ import { Translator, type Engine } from "@yxw007/translate";
252
+
253
+ const translator = new Translator();
254
+
255
+ const fromLanguages = { Auto: "auto", English: "en" };
256
+ const toLanguages = { Chinese: "zh", Japanese: "ja" };
257
+
258
+ const customEngine: Engine = {
259
+ name: "custom",
260
+ getFromLanguages() {
261
+ return fromLanguages;
262
+ },
263
+ getToLanguages() {
264
+ return toLanguages;
265
+ },
266
+ normalFromLanguage(language) {
267
+ if (!language || language === "auto") return "auto";
268
+ return fromLanguages[language as keyof typeof fromLanguages] ?? "";
269
+ },
270
+ normalToLanguage(language) {
271
+ if (!language) return "";
272
+ return toLanguages[language as keyof typeof toLanguages] ?? "";
273
+ },
274
+ async translate(text, options) {
275
+ const list = Array.isArray(text) ? text : [text];
276
+ return list.map((item) => `[${options.from}->${options.to}] ${item}`);
277
+ },
278
+ };
279
+
280
+ translator.addEngine(customEngine);
281
+ ```
282
+
283
+ 内置 engine 直接引用即可:
284
+
285
+ ```typescript
286
+ translator.addEngine(engines.google);
225
287
  ```
226
288
 
227
289
  #### AzureEngineOption
@@ -252,7 +314,7 @@ interface AmazonEngineOption extends BaseEngineOption{
252
314
 
253
315
  - 相关文档:https://docs.aws.amazon.com/translate/latest/dg/what-is.html
254
316
  - 相关库:https://www.npmjs.com/package/@aws-sdk/client-translate
255
-
317
+
256
318
  #### BaiduEngineOption
257
319
 
258
320
  ```typescript
@@ -330,7 +392,7 @@ export interface TencentEngineOption extends BaseEngineOption {
330
392
  }
331
393
  ```
332
394
 
333
- > 说明:option param 请从对应平台获取。
395
+ > 说明:option param 请从对应平台获取。
334
396
  - 相关文档:https://console.cloud.tencent.com/cam/capi
335
397
 
336
398
  - region 配置表
@@ -376,6 +438,9 @@ export interface TencentEngineOption extends BaseEngineOption {
376
438
  const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
377
439
  return {
378
440
  name: "xx",
441
+ async checkLanguage<T extends Engines>(text: string): Promise<string> {
442
+ //TODO: 可以用translate来实现,这样的话就复用target语言配置
443
+ },
379
444
  async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
380
445
  const { from, to } = opts;
381
446
  if (!Array.isArray(text)) {
@@ -395,7 +460,7 @@ export interface TencentEngineOption extends BaseEngineOption {
395
460
  }
396
461
  ```
397
462
  - 将插件添加至engines(位置:```/src/engines/index.ts```)
398
-
463
+
399
464
  ```typescript
400
465
  import { xxx } from "./xxx";
401
466
  export const engines = {
@@ -409,14 +474,14 @@ export interface TencentEngineOption extends BaseEngineOption {
409
474
  } as const;
410
475
  ```
411
476
  - 添加对应Engine支持的origin语言配置
412
-
477
+
413
478
  ```typescript
414
479
  //说明:如果origin与target语言都一样,那么可以直接用target语言配置即可,否则请单独配置
415
- //src/language/origin/index.ts
480
+ //src/language/origin/index.ts
416
481
  import azure from "../target/azure";
417
482
  ...
418
483
  import xxx from "../target/xxx"
419
-
484
+
420
485
  export const originLanguages = {
421
486
  azure: azure,
422
487
  ...
@@ -438,9 +503,9 @@ export interface TencentEngineOption extends BaseEngineOption {
438
503
  ```
439
504
 
440
505
  - 添加对应Engine支持的target语言配置
441
-
506
+
442
507
  ```typescript
443
- //src/language/target/index.ts
508
+ //src/language/target/index.ts
444
509
  import azure from "./azure";
445
510
  ...
446
511
  import xxx from "./amazon";
@@ -463,7 +528,7 @@ export interface TencentEngineOption extends BaseEngineOption {
463
528
  xxx: ValuesOf<typeof xxx>;
464
529
  };
465
530
  ```
466
-
531
+
467
532
  - 打包
468
533
  ```bash
469
534
  pnpm build