@yxw007/translate 0.0.1-alpha.2 → 0.0.1
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 +6 -0
- package/README_zh-CN.md +48 -15
- package/dist/{index.cjs → browser/index.cjs} +6 -1
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.min.cjs +2 -0
- package/dist/browser/index.min.cjs.map +1 -0
- package/dist/{index.umd.js → browser/index.umd.js} +6 -1
- package/dist/{index.cjs.map → browser/index.umd.js.map} +1 -1
- package/dist/browser/index.umd.min.js +2 -0
- package/dist/browser/index.umd.min.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/node/index.cjs +13497 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.js +13490 -0
- package/dist/node/index.js.map +1 -0
- package/dist/package.json +16 -5
- package/package.json +16 -5
- package/dist/index.js +0 -6849
- package/dist/index.js.map +0 -1
- package/dist/index.umd.js.map +0 -1
package/README.md
CHANGED
|
@@ -253,6 +253,12 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
253
253
|
|
|
254
254
|
> **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🙏**
|
|
255
255
|
|
|
256
|
+
|
|
257
|
+
## 🌹 Thanks
|
|
258
|
+
- [franciscop/translate](https://github.com/franciscop/translate.git)
|
|
259
|
+
|
|
260
|
+
> 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.🙏
|
|
261
|
+
|
|
256
262
|
## 📄 License
|
|
257
263
|
|
|
258
264
|
Translate is released under the MIT license. See the [`LICENSE`](./LICENSE) file.
|
package/README_zh-CN.md
CHANGED
|
@@ -59,11 +59,18 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
|
|
|
59
59
|
|
|
60
60
|
## 📖 使用
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
### Browser
|
|
63
63
|
|
|
64
|
+
- ESM
|
|
64
65
|
```typescript
|
|
65
66
|
import { translator, engines } from "@yxw007/translate"
|
|
66
|
-
|
|
67
|
+
```
|
|
68
|
+
- Commonjs
|
|
69
|
+
```typescript
|
|
70
|
+
const { translator, engines } = required("@yxw007/translate")
|
|
71
|
+
```
|
|
72
|
+
- example
|
|
73
|
+
```typescript
|
|
67
74
|
translator.use(engines.google());
|
|
68
75
|
const res1 = await translator.translate("hello", { from: "en", to: "zh" });
|
|
69
76
|
console.log(res1);
|
|
@@ -78,24 +85,45 @@ Translate 是一个支持多翻译引擎的翻译工具库,它提供了一套
|
|
|
78
85
|
["你好", "好的"]
|
|
79
86
|
```
|
|
80
87
|
|
|
81
|
-
|
|
88
|
+
### Browser
|
|
82
89
|
|
|
83
|
-
|
|
84
|
-
const { translator, engines } = required("@yxw007/translate")
|
|
85
|
-
|
|
86
|
-
translator.use(engines.google());
|
|
87
|
-
const res1 = await translator.translate("hello", { from: "en", to: "zh" });
|
|
88
|
-
console.log(res1);
|
|
90
|
+
使用 jsDelivr CDN
|
|
89
91
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
- `development`
|
|
93
|
+
```html
|
|
94
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.1-alpha.3/dist/browser/index.umd.js"></script>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- `production`
|
|
92
98
|
|
|
99
|
+
```html
|
|
100
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.1-alpha.3/dist/browser/index.umd.min.js"></script>
|
|
93
101
|
```
|
|
94
102
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
- example
|
|
104
|
+
|
|
105
|
+
```html
|
|
106
|
+
<!DOCTYPE html>
|
|
107
|
+
...
|
|
108
|
+
|
|
109
|
+
<head>
|
|
110
|
+
...
|
|
111
|
+
<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.1-alpha.3/dist/browser/index.umd.js"></script>
|
|
112
|
+
</head>
|
|
113
|
+
|
|
114
|
+
<body>
|
|
115
|
+
<script>
|
|
116
|
+
(async () => {
|
|
117
|
+
const { engines, translator } = translate;
|
|
118
|
+
translator.use(engines.google());
|
|
119
|
+
const res = await translator.translate("hello", { from: "en", to: "zh" });
|
|
120
|
+
console.log(res);
|
|
121
|
+
})();
|
|
122
|
+
</script>
|
|
123
|
+
</body>
|
|
124
|
+
|
|
125
|
+
</html>
|
|
126
|
+
|
|
99
127
|
```
|
|
100
128
|
|
|
101
129
|
## 📚 API
|
|
@@ -268,6 +296,11 @@ export interface BaiduEngineOption extends BaseEngineOption {
|
|
|
268
296
|
如果你感兴趣,特别欢迎你的加入,让我们一起完善好这个工具。
|
|
269
297
|
帮忙点个star⭐,让更多人知道这个工具,感谢大家🙏**
|
|
270
298
|
|
|
299
|
+
## 🌹 特别致谢
|
|
300
|
+
- [franciscop/translate](https://github.com/franciscop/translate.git)
|
|
301
|
+
|
|
302
|
+
> 说明:感谢[franciscop/translate](https://github.com/franciscop/translate.git)为我快速实现这个库提供了思路,同时也间接了他的部分代码。非常感谢🙏
|
|
303
|
+
|
|
271
304
|
## 📄 许可证
|
|
272
305
|
|
|
273
306
|
Translate 是在 MIT 许可证下发布的。详情请见 [`LICENSE`](./LICENSE) 文件。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// translate v0.0.1 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
|
|
1
2
|
'use strict';
|
|
2
3
|
|
|
3
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -6820,7 +6821,7 @@ class Translator {
|
|
|
6820
6821
|
this.engines.set(engine.name, engine);
|
|
6821
6822
|
}
|
|
6822
6823
|
translate(text, options) {
|
|
6823
|
-
const { from, to, engine = "google", cache_time = 60 } = options;
|
|
6824
|
+
const { from, to, engine = "google", cache_time = 60 * 1000 } = options;
|
|
6824
6825
|
//1. Check if engine exists
|
|
6825
6826
|
if (!this.engines.has(engine)) {
|
|
6826
6827
|
throw new Error(`Engine ${engine} not found`);
|
|
@@ -6833,6 +6834,10 @@ class Translator {
|
|
|
6833
6834
|
throw new Error(`Language ${to} not found`);
|
|
6834
6835
|
}
|
|
6835
6836
|
const key = `${from}:${to}:${engine}:${text}`;
|
|
6837
|
+
//3. If the cache is matched, the cache is used directly
|
|
6838
|
+
if (cache.get(key)) {
|
|
6839
|
+
return cache.get(key)?.value;
|
|
6840
|
+
}
|
|
6836
6841
|
const engineInstance = this.engines.get(engine);
|
|
6837
6842
|
if (!engineInstance) {
|
|
6838
6843
|
throw new Error(`Engine ${engine} not found`);
|