@yxw007/translate 0.0.8 → 0.0.9

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
@@ -129,7 +129,7 @@ class Translator {
129
129
  use(engine: Engine) {
130
130
  ...
131
131
  }
132
- translate(text: string | string[], options: TranslateOptions) {
132
+ translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
133
133
  ...
134
134
  }
135
135
  }
@@ -142,7 +142,7 @@ Add a translation engine to transitorion engine to translator
142
142
  ```typescript
143
143
  type Engine = {
144
144
  name: string;
145
- translate: (text: string | string[], opts: EngineTranslateOptions) => Promise<string[]>;
145
+ translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
146
146
  };
147
147
  ```
148
148
 
@@ -151,24 +151,32 @@ type Engine = {
151
151
  You can pass a text or pass a text array, which will return a translated ```Promise<string[]>```
152
152
 
153
153
  ```typescript
154
- translate(text: string | string[], options: TranslateOptions)
154
+ translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>)
155
155
  ```
156
156
 
157
157
  #### TranslateOptions
158
158
 
159
159
  ```typescript
160
160
  export interface TranslateOptions {
161
- from: Language;
162
- to: Language;
161
+ from?: FromLanguage<T>;
162
+ to: ToLanguage<T>;
163
163
  engine?: Engines;
164
164
  /**
165
165
  * Cache time in milliseconds
166
166
  */
167
167
  cache_time?: number;
168
+ /**
169
+ * Domain to use for translation
170
+ */
168
171
  domain?: string;
169
172
  }
170
173
  ```
171
174
 
175
+ > Note: To learn more about the support of each engine language, go to the following directory to view the corresponding configurations
176
+
177
+ - 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)
178
+ - 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)
179
+
172
180
  ### Each translation of Engine's Option
173
181
 
174
182
  #### BaseEngineOption
@@ -254,7 +262,7 @@ export interface DeeplEngineOption {
254
262
  const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
255
263
  return {
256
264
  name: "yandex",
257
- async translate(text: string | string[], opts: EngineTranslateOptions): Promise<string[]> {
265
+ async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
258
266
  const { from, to } = opts;
259
267
  if (!Array.isArray(text)) {
260
268
  text = [text];
@@ -285,6 +293,61 @@ export interface DeeplEngineOption {
285
293
  xx
286
294
  } as const;
287
295
  ```
296
+ - Add the origin language configuration supported by the engine
297
+
298
+ ```typescript
299
+ //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
300
+ //src/language/origin/index.ts
301
+ import azure from "../target/azure";
302
+ ...
303
+ import xxx from "../target/xxx"
304
+
305
+ export const originLanguages = {
306
+ azure: azure,
307
+ ...
308
+ xxx: xxx,
309
+ } as const;
310
+
311
+ export type originLanguageMapNames = {
312
+ amazon: keyof typeof amazon;
313
+ ...
314
+ xxx: keyof typeof xxx;
315
+ };
316
+
317
+ export type originLanguageMapValues = {
318
+ amazon: ValuesOf<typeof amazon>;
319
+ ...
320
+ xxx: ValuesOf<typeof xxx>;
321
+ };
322
+
323
+ ```
324
+
325
+ - Add the target language that is supported by the engine
326
+
327
+ ```typescript
328
+ //src/language/target/index.ts
329
+ import azure from "./azure";
330
+ ...
331
+ import xxx from "./amazon";
332
+
333
+ export const targetLanguages = {
334
+ azure: azure,
335
+ ...
336
+ xxx: xxx,
337
+ } as const;
338
+
339
+ export type targetLanguageMapNames = {
340
+ amazon: keyof typeof amazon;
341
+ ...
342
+ xxx: keyof typeof xxx;
343
+ };
344
+
345
+ export type targetLanguageMapValues = {
346
+ amazon: ValuesOf<typeof amazon>;
347
+ ...
348
+ xxx: ValuesOf<typeof xxx>;
349
+ };
350
+
288
351
  - Build
289
352
  ```bash
290
353
  pnpm build
package/README_zh-CN.md CHANGED
@@ -142,7 +142,7 @@ class Translator {
142
142
  use(engine: Engine) {
143
143
  ...
144
144
  }
145
- translate(text: string | string[], options: TranslateOptions) {
145
+ translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
146
146
  ...
147
147
  }
148
148
  }
@@ -153,9 +153,9 @@ class Translator {
153
153
  给translator添加翻译引擎
154
154
 
155
155
  ```typescript
156
- type Engine = {
156
+ export type Engine = {
157
157
  name: string;
158
- translate: (text: string | string[], opts: EngineTranslateOptions) => Promise<string[]>;
158
+ translate: <T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) => Promise<string[]>;
159
159
  };
160
160
  ```
161
161
 
@@ -164,24 +164,32 @@ type Engine = {
164
164
  可以传一个文本or传一个文本数组,将返回一个翻译后的Promise<string[]>
165
165
 
166
166
  ```typescript
167
- translate(text: string | string[], options: TranslateOptions)
167
+ translate: <T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) => Promise<string[]>;
168
168
  ```
169
169
 
170
170
  #### TranslateOptions
171
171
 
172
172
  ```typescript
173
- export interface TranslateOptions {
174
- from: Language;
175
- to: Language;
176
- engine?: Engines;
177
- /**
173
+ export type TranslateOptions<T extends Engines> = {
174
+ from?: FromLanguage<T>;
175
+ to: ToLanguage<T>;
176
+ engine?: T;
177
+ /**
178
178
  * Cache time in milliseconds
179
179
  */
180
- cache_time?: number;
181
- domain?: string;
182
- }
180
+ cache_time?: number | undefined;
181
+ /**
182
+ * Domain to use for translation
183
+ */
184
+ domain?: string | undefined;
185
+ };
183
186
  ```
184
187
 
188
+ > 提示:如需了解各Engine语言支持情况,请到以下目录查看对应配置
189
+
190
+ - 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)
191
+ - 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)
192
+
185
193
  ### 各翻译Engine的Option
186
194
 
187
195
  #### BaseEngineOption
@@ -266,8 +274,8 @@ export interface DeeplEngineOption {
266
274
  const { key } = options;
267
275
  const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
268
276
  return {
269
- name: "yandex",
270
- async translate(text: string | string[], opts: EngineTranslateOptions): Promise<string[]> {
277
+ name: "xx",
278
+ async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
271
279
  const { from, to } = opts;
272
280
  if (!Array.isArray(text)) {
273
281
  text = [text];
@@ -288,16 +296,73 @@ export interface DeeplEngineOption {
288
296
  - 将插件添加至engines(位置:```/src/engines/index.ts```)
289
297
 
290
298
  ```typescript
291
- import { xx } from "./xx";
299
+ import { xxx } from "./xxx";
292
300
  export const engines = {
293
301
  google,
294
302
  azure,
295
303
  amazon,
296
304
  baidu,
297
305
  deepl,
298
- xx
306
+ xxx
307
+ } as const;
308
+ ```
309
+ - 添加对应Engine支持的origin语言配置
310
+
311
+ ```typescript
312
+ //说明:如果origin与target语言都一样,那么可以直接用target语言配置即可,否则请单独配置
313
+ //src/language/origin/index.ts
314
+ import azure from "../target/azure";
315
+ ...
316
+ import xxx from "../target/xxx"
317
+
318
+ export const originLanguages = {
319
+ azure: azure,
320
+ ...
321
+ xxx: xxx,
299
322
  } as const;
323
+
324
+ export type originLanguageMapNames = {
325
+ amazon: keyof typeof amazon;
326
+ ...
327
+ xxx: keyof typeof xxx;
328
+ };
329
+
330
+ export type originLanguageMapValues = {
331
+ amazon: ValuesOf<typeof amazon>;
332
+ ...
333
+ xxx: ValuesOf<typeof xxx>;
334
+ };
335
+
300
336
  ```
337
+
338
+ - 添加对应Engine支持的target语言配置
339
+
340
+ ```typescript
341
+ //src/language/target/index.ts
342
+ import azure from "./azure";
343
+ ...
344
+ import xxx from "./amazon";
345
+
346
+ export const targetLanguages = {
347
+ azure: azure,
348
+ ...
349
+ xxx: xxx,
350
+ } as const;
351
+
352
+ export type targetLanguageMapNames = {
353
+ amazon: keyof typeof amazon;
354
+ ...
355
+ xxx: keyof typeof xxx;
356
+ };
357
+
358
+ export type targetLanguageMapValues = {
359
+ amazon: ValuesOf<typeof amazon>;
360
+ ...
361
+ xxx: ValuesOf<typeof xxx>;
362
+ };
363
+
364
+ ```
365
+
301
366
  - 打包
302
367
  ```bash
303
368
  pnpm build
@@ -1,4 +1,4 @@
1
- // translate v0.0.8 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.0.9 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -1376,7 +1376,6 @@ function useLogger(name = "") {
1376
1376
  }
1377
1377
 
1378
1378
  var azure = {
1379
- Auto: "auto",
1380
1379
  Afrikaans: "af",
1381
1380
  Albanian: "sq",
1382
1381
  Amharic: "am",
@@ -1515,7 +1514,6 @@ var azure = {
1515
1514
  };
1516
1515
 
1517
1516
  var google = {
1518
- Auto: "auto",
1519
1517
  Abkhazian: "ab",
1520
1518
  Acehnese: "ace",
1521
1519
  "Acholi language": "ach",
@@ -1762,7 +1760,6 @@ var google = {
1762
1760
  };
1763
1761
 
1764
1762
  var baidu = {
1765
- Auto: "auto",
1766
1763
  Achinese: "ach",
1767
1764
  Afrikaans: "afr",
1768
1765
  Akan: "aka",
@@ -1974,7 +1971,6 @@ var baidu = {
1974
1971
  };
1975
1972
 
1976
1973
  var deepl$1 = {
1977
- Deepl: "auto",
1978
1974
  English: "en",
1979
1975
  Bulgarian: "bg",
1980
1976
  Chinese: "zh",
@@ -2011,7 +2007,6 @@ var deepl$1 = {
2011
2007
  };
2012
2008
 
2013
2009
  var amazon = {
2014
- Auto: "auto",
2015
2010
  Afrikaans: "af",
2016
2011
  Albanian: "sq",
2017
2012
  Amharic: "am",
@@ -2098,7 +2093,6 @@ const originLanguages = {
2098
2093
  };
2099
2094
 
2100
2095
  var deepl = {
2101
- Auto: "auto",
2102
2096
  "American English": "en-US",
2103
2097
  "Brazilian Portuguese": "pt-BR",
2104
2098
  "British English": "en-GB",