@yxw007/translate 0.0.7 → 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 +88 -12
- package/README_zh-CN.md +98 -19
- package/dist/browser/index.cjs +828 -465
- package/dist/browser/index.cjs.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 +828 -465
- 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 +2218 -664
- package/dist/node/index.cjs +828 -465
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +828 -465
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,13 +80,13 @@ use jsDelivr CDN
|
|
|
80
80
|
|
|
81
81
|
- `development`
|
|
82
82
|
```html
|
|
83
|
-
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.
|
|
83
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
|
|
84
84
|
```
|
|
85
85
|
|
|
86
86
|
- `production`
|
|
87
87
|
|
|
88
88
|
```html
|
|
89
|
-
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.
|
|
89
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.min.js"></script>
|
|
90
90
|
```
|
|
91
91
|
|
|
92
92
|
- example
|
|
@@ -97,7 +97,7 @@ use jsDelivr CDN
|
|
|
97
97
|
|
|
98
98
|
<head>
|
|
99
99
|
...
|
|
100
|
-
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.
|
|
100
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
|
|
101
101
|
</head>
|
|
102
102
|
|
|
103
103
|
<body>
|
|
@@ -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
|
|
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
|
|
162
|
-
to:
|
|
163
|
-
engine?:
|
|
161
|
+
from?: FromLanguage<T>;
|
|
162
|
+
to: ToLanguage<T>;
|
|
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
|
|
@@ -203,7 +211,7 @@ interface AmazonEngineOption extends BaseEngineOption{
|
|
|
203
211
|
|
|
204
212
|
> Note: Option Param, please get it from the corresponding platform
|
|
205
213
|
|
|
206
|
-
- Related
|
|
214
|
+
- Related document:https://docs.aws.amazon.com/translate/latest/dg/what-is.html
|
|
207
215
|
- Related library:https://www.npmjs.com/package/@aws-sdk/client-translate
|
|
208
216
|
|
|
209
217
|
#### BaiduEngineOption
|
|
@@ -217,7 +225,19 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
217
225
|
|
|
218
226
|
> Note: Option Param, please get it from the corresponding platform
|
|
219
227
|
|
|
220
|
-
- Related
|
|
228
|
+
- Related document:https://fanyi-api.baidu.com/product/121
|
|
229
|
+
|
|
230
|
+
#### DeeplEngineOption
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
export interface DeeplEngineOption {
|
|
234
|
+
key: string;
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
> Note: Option Param, please get it from the corresponding platform
|
|
239
|
+
|
|
240
|
+
- Related document:https://www.deepl.com/en/your-account/keys
|
|
221
241
|
|
|
222
242
|
## 🤝 Contribute
|
|
223
243
|
|
|
@@ -242,7 +262,7 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
242
262
|
const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
|
|
243
263
|
return {
|
|
244
264
|
name: "yandex",
|
|
245
|
-
async translate(text: string | string[], opts: EngineTranslateOptions
|
|
265
|
+
async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
|
|
246
266
|
const { from, to } = opts;
|
|
247
267
|
if (!Array.isArray(text)) {
|
|
248
268
|
text = [text];
|
|
@@ -269,9 +289,65 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
269
289
|
azure,
|
|
270
290
|
amazon,
|
|
271
291
|
baidu,
|
|
292
|
+
deepl,
|
|
272
293
|
xx
|
|
273
294
|
} as const;
|
|
274
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
|
+
|
|
275
351
|
- Build
|
|
276
352
|
```bash
|
|
277
353
|
pnpm build
|
package/README_zh-CN.md
CHANGED
|
@@ -34,6 +34,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
|
|
|
34
34
|
| azure translate | √ | 已投产,可以正常使用 |
|
|
35
35
|
| amazon translate | √ | 已投产,可以正常使用 |
|
|
36
36
|
| baidu | √ | 已投产,可以正常使用 |
|
|
37
|
+
| deepl | √ | 已投产,可以正常使用 |
|
|
37
38
|
| yandex | | 由于我没有平台支持的银行账号,所以未调通(欢迎有条件的朋友帮忙调通,感谢) |
|
|
38
39
|
|
|
39
40
|
|
|
@@ -93,13 +94,13 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
|
|
|
93
94
|
|
|
94
95
|
- `development`
|
|
95
96
|
```html
|
|
96
|
-
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.
|
|
97
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
|
|
97
98
|
```
|
|
98
99
|
|
|
99
100
|
- `production`
|
|
100
101
|
|
|
101
102
|
```html
|
|
102
|
-
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.
|
|
103
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.min.js"></script>
|
|
103
104
|
```
|
|
104
105
|
|
|
105
106
|
- example
|
|
@@ -110,7 +111,7 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
|
|
|
110
111
|
|
|
111
112
|
<head>
|
|
112
113
|
...
|
|
113
|
-
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.
|
|
114
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
|
|
114
115
|
</head>
|
|
115
116
|
|
|
116
117
|
<body>
|
|
@@ -141,7 +142,7 @@ class Translator {
|
|
|
141
142
|
use(engine: Engine) {
|
|
142
143
|
...
|
|
143
144
|
}
|
|
144
|
-
translate(text: string | string[], options: TranslateOptions) {
|
|
145
|
+
translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) {
|
|
145
146
|
...
|
|
146
147
|
}
|
|
147
148
|
}
|
|
@@ -152,9 +153,9 @@ class Translator {
|
|
|
152
153
|
给translator添加翻译引擎
|
|
153
154
|
|
|
154
155
|
```typescript
|
|
155
|
-
type Engine = {
|
|
156
|
+
export type Engine = {
|
|
156
157
|
name: string;
|
|
157
|
-
translate: (text: string | string[], opts: EngineTranslateOptions) => Promise<string[]>;
|
|
158
|
+
translate: <T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) => Promise<string[]>;
|
|
158
159
|
};
|
|
159
160
|
```
|
|
160
161
|
|
|
@@ -163,24 +164,32 @@ type Engine = {
|
|
|
163
164
|
可以传一个文本or传一个文本数组,将返回一个翻译后的Promise<string[]>
|
|
164
165
|
|
|
165
166
|
```typescript
|
|
166
|
-
translate(text: string | string[],
|
|
167
|
+
translate: <T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) => Promise<string[]>;
|
|
167
168
|
```
|
|
168
169
|
|
|
169
170
|
#### TranslateOptions
|
|
170
171
|
|
|
171
172
|
```typescript
|
|
172
|
-
export
|
|
173
|
-
from
|
|
174
|
-
to:
|
|
175
|
-
engine?:
|
|
176
|
-
|
|
173
|
+
export type TranslateOptions<T extends Engines> = {
|
|
174
|
+
from?: FromLanguage<T>;
|
|
175
|
+
to: ToLanguage<T>;
|
|
176
|
+
engine?: T;
|
|
177
|
+
/**
|
|
177
178
|
* Cache time in milliseconds
|
|
178
179
|
*/
|
|
179
|
-
cache_time?: number;
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
cache_time?: number | undefined;
|
|
181
|
+
/**
|
|
182
|
+
* Domain to use for translation
|
|
183
|
+
*/
|
|
184
|
+
domain?: string | undefined;
|
|
185
|
+
};
|
|
182
186
|
```
|
|
183
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
|
+
|
|
184
193
|
### 各翻译Engine的Option
|
|
185
194
|
|
|
186
195
|
#### BaseEngineOption
|
|
@@ -231,6 +240,18 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
231
240
|
|
|
232
241
|
- 相关文档:https://fanyi-api.baidu.com/product/121
|
|
233
242
|
|
|
243
|
+
#### DeeplEngineOption
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
export interface DeeplEngineOption {
|
|
247
|
+
key: string;
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
> 说明:option param 请从对应平台获取
|
|
252
|
+
|
|
253
|
+
- 相关文档:https://www.deepl.com/en/your-account/keys
|
|
254
|
+
|
|
234
255
|
## 🤝 贡献
|
|
235
256
|
|
|
236
257
|
> 特别注意:请基于master创建一个新分支,在新分支上开发,开发完后创建PR至master
|
|
@@ -253,8 +274,8 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
253
274
|
const { key } = options;
|
|
254
275
|
const base = "https://translate.yandex.net/api/v1.5/tr.json/translate";
|
|
255
276
|
return {
|
|
256
|
-
name: "
|
|
257
|
-
async translate(text: string | string[], opts: EngineTranslateOptions
|
|
277
|
+
name: "xx",
|
|
278
|
+
async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) {
|
|
258
279
|
const { from, to } = opts;
|
|
259
280
|
if (!Array.isArray(text)) {
|
|
260
281
|
text = [text];
|
|
@@ -275,15 +296,73 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
275
296
|
- 将插件添加至engines(位置:```/src/engines/index.ts```)
|
|
276
297
|
|
|
277
298
|
```typescript
|
|
278
|
-
import {
|
|
299
|
+
import { xxx } from "./xxx";
|
|
279
300
|
export const engines = {
|
|
280
301
|
google,
|
|
281
302
|
azure,
|
|
282
303
|
amazon,
|
|
283
304
|
baidu,
|
|
284
|
-
|
|
305
|
+
deepl,
|
|
306
|
+
xxx
|
|
285
307
|
} as const;
|
|
286
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,
|
|
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
|
+
|
|
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
|
+
|
|
287
366
|
- 打包
|
|
288
367
|
```bash
|
|
289
368
|
pnpm build
|