vite-auto-i18n-plugin 1.1.2 → 1.1.3-beta1

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 (3) hide show
  1. package/package.json +2 -2
  2. package/readme-en.md +112 -40
  3. package/readme.md +96 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-auto-i18n-plugin",
3
- "version": "1.1.2",
3
+ "version": "1.1.3-beta1",
4
4
  "description": "Vite automatic translation plugin compatible with Rollup",
5
5
  "keywords": [
6
6
  "i18n",
@@ -33,7 +33,7 @@
33
33
  "@babel/core": "^7.23.2",
34
34
  "@babel/preset-env": "^7.23.2",
35
35
  "@babel/preset-typescript": "^7.23.2",
36
- "auto-i18n-plugin-core": "1.1.2"
36
+ "auto-i18n-plugin-core": "1.1.3-beta1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@babel/types": "^7.23.0",
package/readme-en.md CHANGED
@@ -37,11 +37,11 @@ pnpm run preview // Don't select React, contains too much English text
37
37
 
38
38
  ---
39
39
 
40
- ## 🌟 Quick Start
40
+ ## 🌟 Quick Start 🚀
41
41
 
42
- ### 1️⃣ Install Plugin
42
+ ### 1️⃣ Install Plugin 📦
43
43
 
44
- #### **Vite Projects:**
44
+ #### **🌐 Vite Project:**
45
45
 
46
46
  ```bash
47
47
  npm install vite-auto-i18n-plugin --save-dev
@@ -49,7 +49,7 @@ npm install vite-auto-i18n-plugin --save-dev
49
49
  yarn add vite-auto-i18n-plugin --dev
50
50
  ```
51
51
 
52
- #### **Webpack Projects:**
52
+ #### **🛠️ Webpack Project:**
53
53
 
54
54
  ```bash
55
55
  npm install webpack-auto-i18n-plugin --save-dev
@@ -59,7 +59,78 @@ yarn add webpack-auto-i18n-plugin --dev
59
59
 
60
60
  ---
61
61
 
62
- ### 2️⃣ Basic Configuration
62
+ ### 2️⃣ Language Switching 🌐
63
+
64
+ #### ⚙️ Basic Switching
65
+
66
+ ```js
67
+ window.localStorage.setItem('lang', value)
68
+ window.location.reload()
69
+
70
+ // value is the key of the language mapping object
71
+ // The mapping object is located in the lang/index.js file by default. You can check this part of the code inside.
72
+ const langMap = {
73
+ 'en': (),
74
+ 'zhcn': ()
75
+ }
76
+ ```
77
+
78
+ #### 📲 Instant Language Switching
79
+
80
+ If you don't want to switch languages by refreshing the page, you can directly modify the language via `$changeLang` and then re - render the corresponding components.
81
+ ```js
82
+ window.$changeLang('en')
83
+ ```
84
+
85
+ For example, in Vue2, you can switch languages without refreshing the page in the following way:
86
+
87
+ ```Html
88
+ <template>
89
+ <div id="app" v-if="isShow">
90
+ <button @click="changeLang('en')">Switch to English</button>
91
+ <button @click="changeLang('zh-cn')">Switch to Chinese</button>
92
+ <router-view></router-view>
93
+ </div>
94
+ </template>
95
+ <script>
96
+ export default {
97
+ data() {
98
+ return {
99
+ isShow: true
100
+ }
101
+ },
102
+ methods: {
103
+ changeLang(lang) {
104
+ window.$changeLang(lang)
105
+ this.isShow = false
106
+ this.$nextTick(() => {
107
+ this.isShow = true
108
+ })
109
+ }
110
+ }
111
+ }
112
+ </script>
113
+ ```
114
+ #### 🔄 Replace Language Packs
115
+
116
+ If you want to modify the built - in generated language packs, you can directly modify the `langMap` in the global object. For example:
117
+
118
+ ```js
119
+ window.langMap = {
120
+ 'en': {
121
+ 'zccsau6': 'hello'
122
+ },
123
+ 'zh-cn': {
124
+ 'zccsau6': '你好'
125
+ }
126
+ }
127
+ ```
128
+
129
+ Then you can refresh the page by using the above **Instant Language Switching** method.
130
+
131
+ ---
132
+
133
+ ### 3️⃣ Basic Configuration 🔧
63
134
 
64
135
  #### **Vite Configuration Example** (vite.config.js):
65
136
 
@@ -78,7 +149,7 @@ export default defineConfig({
78
149
  }
79
150
  }
80
151
  }),
81
- vitePluginAutoI18n({
152
+ vitePluginsAutoI18n({
82
153
  translator: new YoudaoTranslator({
83
154
  appId: '4cdb9baea8066fef',
84
155
  appKey: 'ONI6AerZnGRyDqr3w7UM730mPuF8mB3j'
@@ -87,7 +158,6 @@ export default defineConfig({
87
158
  ]
88
159
  })
89
160
  ```
90
-
91
161
  #### **Webpack Configuration Example** (webpack.config.js):
92
162
 
93
163
  ```javascript
@@ -113,12 +183,13 @@ module.exports = {
113
183
  ```
114
184
 
115
185
  ---
186
+ ### 4️⃣ Translator Configuration Examples 🛠️
116
187
 
117
- ### 3️⃣ Translator Configuration Examples
188
+ The plugin uses Google Translate by default (with a default proxy port of 7890). When the network does not support accessing Google, we recommend using **Youdao Translate** ✨, which has excellent translation quality. Currently, the plugin has built - in support for Google, Youdao, and Baidu translation services. If you need to customize a translator, you can refer to the examples below.
118
189
 
119
- The plugin uses Google Translate by default (with a default proxy port of 7890). When your network does not support accessing Google, we recommend choosing **Youdao Translate** ✨, which offers excellent translation quality. Currently, the plugin has built - in translation functions for Google, Youdao, and Baidu. If you need a custom translator, you can refer to the examples below.
190
+ The following examples use `vite` as an example, and `webpack` is similar.
120
191
 
121
- #### **Using Google Translate (default)**
192
+ #### **Using Google Translate (Default)**
122
193
 
123
194
  ```javascript
124
195
  import { GoogleTranslator } from 'vite-auto-i18n-plugin'
@@ -135,59 +206,54 @@ translator: new GoogleTranslator({
135
206
  })
136
207
  ...
137
208
  ```
209
+ #### **Using Youdao Translation**
138
210
 
139
- #### **Using Youdao Translate**
140
-
141
- Requires API application: [API Docs](https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html)
142
-
211
+ You need to apply for an API. [API documentation](https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html).
143
212
  ```javascript
144
213
  import { YoudaoTranslator } from 'vite-auto-i18n-plugin'
145
214
 
146
215
  ...
147
216
  translator: new YoudaoTranslator({
148
- appId: 'your-app-id',
149
- appKey: 'your-app-key'
217
+ appId: 'The appId you applied for',
218
+ appKey: 'The appKey you applied for'
150
219
  })
151
220
  ...
152
221
  ```
153
222
 
154
223
  #### **Baidu Translator**
155
224
 
156
- Requires API application: [API Docs](https://api.fanyi.baidu.com/product/113)
225
+ You need to apply for an API. [API documentation](https://api.fanyi.baidu.com/product/113).
157
226
 
158
227
  ```javascript
159
228
  import { BaiduTranslator } from 'vite-auto-i18n-plugin'
160
229
 
161
230
  ...
162
231
  translator: new BaiduTranslator({
163
- appId: 'your-app-id', // Baidu AppId
164
- appKey: 'your-app-key' // Baidu AppKey
232
+ appId: 'The appId you applied for', // Baidu Translate AppId
233
+ appKey: 'The appKey you applied for' // Baidu Translate AppKey
165
234
  })
166
235
  ...
167
236
  ```
168
237
 
169
238
  #### **Volcengine AI Translator**
170
239
 
171
- Supports calling `doubao` or `deepseek` for translation. AI model translation provides more accurate results than traditional API translation, but takes longer to process.
172
- Volcengine AI model introduction: https://www.volcengine.com/docs/82379/1099455.
173
- Requires enabling the AI service and applying for API, [API documentation](https://www.volcengine.com/docs/82379/1298454).
174
-
240
+ It supports translation using `doubao` or `deepseek`. The translation effect of large AI models is more accurate than traditional API translation, but it takes longer.
241
+ Introduction to Volcengine large models: https://www.volcengine.com/docs/82379/1099455.
242
+ You need to activate the large model service and apply for an API. [API documentation](https://www.volcengine.com/docs/82379/1298454).
175
243
  ```javascript
176
244
  import { VolcengineTranslator } from 'vite-auto-i18n-plugin'
177
245
 
178
246
  ...
179
247
  translator: new VolcengineTranslator({
180
- apiKey: 'your-api-key',
181
- model: 'model-to-call, e.g. `doubao-1-5-pro-32k-250115`, please ensure the model has been enabled in console before using'
248
+ apiKey: 'The apiKey you applied for',
249
+ model: 'The model you want to call, for example: `doubao-1-5-pro-32k-250115`. Please ensure that the corresponding model has been activated in the console before use.'
182
250
  })
183
251
  ...
184
252
  ```
185
253
 
186
- ````
187
-
188
254
  #### **Empty Translator**
189
255
 
190
- If you only need to scan target language without translation, this translator will generate JSON files.
256
+ If you only need to scan the target language without performing translation, this translator will generate a JSON file.
191
257
 
192
258
  ```javascript
193
259
  import { EmptyTranslator } from 'vite-auto-i18n-plugin'
@@ -195,13 +261,12 @@ import { EmptyTranslator } from 'vite-auto-i18n-plugin'
195
261
  ...
196
262
  translator: new EmptyTranslator()
197
263
  ...
198
- ````
199
-
264
+ ```
200
265
  #### **Custom Translator**
201
266
 
202
- If you have your own translation API, you can create a custom translator:
267
+ If you have a personal translation API, you can customize a translator in the following ways:
203
268
 
204
- Simplest way is using the base `Translator` class:
269
+ The simplest way is to define a translator instance using the `Translator` base class.
205
270
 
206
271
  ```javascript
207
272
  import { Translator } from 'vite-auto-i18n-plugin'
@@ -212,19 +277,17 @@ translator: new Translator({
212
277
  name: 'My Translator',
213
278
  // Translation method
214
279
  fetchMethod: (str, fromKey, toKey, _separator) => {
215
- // Actual API calls may be more complex than this example
280
+ // The actual API call may be more complex than the example. For details, please refer to the implementation of YoudaoTranslator in the source code. Path: packages\autoI18nPluginCore\src\translators\youdao.ts
216
281
  const myApi = 'https://www.my-i18n.cn/api/translate?from=${fromKey}&to=${toKey}&t={+new Date}'
217
282
  return axios.post(myApi, { str })
218
283
  .then(res => res.data)
219
284
  },
220
- // API call interval (some APIs may block frequent requests)
285
+ // Interface trigger interval. Some interfaces may be blocked if triggered frequently. Please set a reasonable interface trigger interval according to the actual situation.
221
286
  interval: 1000
222
287
  })
223
288
  ...
224
289
  ```
225
-
226
- For advanced functionality, you can extend the class (though no current use cases):
227
-
290
+ If you need more advanced features, you can use inheritance. However, there is currently no relevant scenario.
228
291
  ```javascript
229
292
  import { Translator } from 'vite-auto-i18n-plugin'
230
293
 
@@ -244,12 +307,12 @@ translator: new CustomTranslator()
244
307
 
245
308
  ---
246
309
 
247
- ### 4️⃣ Project Entry Configuration 🏗️
310
+ ### 5️⃣ Project Entry Configuration 🏗️
248
311
 
249
- Add this at the top of your **project entry file** (e.g., `main.js`):
312
+ Please import the language configuration file at the top of the **project entry file** (e.g., `main.js`):
250
313
 
251
314
  ```javascript
252
- import '../lang/index.js' // 📍 Must be imported first in entry file. This file is auto-generated when running the plugin, default location is lang folder at same level as build config directory
315
+ import '../lang/index.js' // 📍 Must be imported on the first line of the entry file. The file will be automatically generated when the plugin is run. By default, it is located in the 'lang' folder at the same level as the packaging configuration directory. The 'index.js' inside is the configuration file.
253
316
  ```
254
317
 
255
318
  ---
@@ -400,6 +463,15 @@ Original authors: wenps, xu-code, Caleb-Xu, Winfans
400
463
 
401
464
  ## Changelog
402
465
 
466
+
467
+ ### v1.1.3 (Recommended Version)
468
+
469
+ - Added the ability to update languages without refreshing and best practices
470
+
471
+ ### v1.1.2
472
+
473
+ - Fixed AI translation exceptions
474
+
403
475
  ### v1.1.1
404
476
 
405
477
  - New translation disable function
package/readme.md CHANGED
@@ -39,45 +39,104 @@ tips:有道翻译需要用户自己去申请有道的翻译服务,demo里面
39
39
 
40
40
  ---
41
41
 
42
- ## 🌟 快速开始
42
+ ## 🌟 快速开始 🚀
43
43
 
44
- ### 切换语言
44
+ ### 1️⃣ 安装插件 📦
45
+
46
+ #### **🌐 Vite 项目:**
47
+
48
+ ```bash
49
+ npm install vite-auto-i18n-plugin --save-dev
50
+ # 或
51
+ yarn add vite-auto-i18n-plugin --dev
52
+ ```
53
+
54
+ #### **🛠️ Webpack 项目:**
55
+
56
+ ```bash
57
+ npm install webpack-auto-i18n-plugin --save-dev
58
+ # 或
59
+ yarn add webpack-auto-i18n-plugin --dev
60
+ ```
61
+
62
+ ---
63
+
64
+ ### 2️⃣ 语言切换 🌐
65
+
66
+ #### ⚙️ 基础切换
45
67
 
46
68
  ```js
47
69
  window.localStorage.setItem('lang', value)
48
70
  window.location.reload()
49
71
 
50
72
  // value 是 语言映射对象 的 key 值
51
- // 映射对象默认存在于 lang/index.js 文件下
73
+ // 映射对象默认存在于 lang/index.js 文件下,可以看里面的这部分代码
52
74
  const langMap = {
53
75
  'en': (),
54
76
  'zhcn': ()
55
77
  }
56
78
  ```
57
79
 
58
- ### 1️⃣ 安装插件
80
+ #### 📲 即时切换语言
59
81
 
60
- #### **Vite 项目:**
82
+ 如果不想通过页面刷新来切换语言,可以直接通过 `$changeLang` 来修改语言,再重新渲染对应组件
61
83
 
62
- ```bash
63
- npm install vite-auto-i18n-plugin --save-dev
64
- # 或
65
- yarn add vite-auto-i18n-plugin --dev
84
+ ```js
85
+ window.$changeLang('en')
66
86
  ```
67
87
 
68
- #### **Webpack 项目:**
88
+ 例如在 Vue2 中,可以通过以下方式进行语言无刷切换:
89
+
90
+ ```Html
91
+ <template>
92
+ <div id="app" v-if="isShow">
93
+ <button @click="changeLang('en')">切换到英文</button>
94
+ <button @click="changeLang('zh-cn')">切换到中文</button>
95
+ <router-view></router-view>
96
+ </div>
97
+ </template>
98
+ <script>
99
+ export default {
100
+ data() {
101
+ return {
102
+ isShow: true
103
+ }
104
+ },
105
+ methods: {
106
+ changeLang(lang) {
107
+ window.$changeLang(lang)
108
+ this.isShow = false
109
+ this.$nextTick(() => {
110
+ this.isShow = true
111
+ })
112
+ }
113
+ }
114
+ }
115
+ </script>
116
+ ```
69
117
 
70
- ```bash
71
- npm install webpack-auto-i18n-plugin --save-dev
72
- # 或
73
- yarn add webpack-auto-i18n-plugin --dev
118
+ #### 🔄 替换语言包
119
+
120
+ 如果想修改内置生成的语言包,可以直接修改全局对象里面的 langMap,例如:
121
+
122
+ ```js
123
+ window.langMap = {
124
+ 'en': {
125
+ 'zccsau6': 'hello'
126
+ },
127
+ 'zh-cn': {
128
+ 'zccsau6': '你好'
129
+ }
130
+ }
74
131
  ```
75
132
 
133
+ 然后再通过上述的**即时切换语言**的方式进行刷新即可。
134
+
76
135
  ---
77
136
 
78
- ### 2️⃣ 基础配置
137
+ ### 3️⃣ 基础配置 🔧
79
138
 
80
- #### **Vite 配置示例** (vite.config.js):
139
+ #### **Vite 配置示例** (vite.config.js):
81
140
 
82
141
  ```javascript
83
142
  import vitePluginsAutoI18n, { YoudaoTranslator } from 'vite-auto-i18n-plugin'
@@ -104,7 +163,7 @@ export default defineConfig({
104
163
  })
105
164
  ```
106
165
 
107
- #### **Webpack 配置示例** (webpack.config.js):
166
+ #### **Webpack 配置示例** (webpack.config.js):
108
167
 
109
168
  ```javascript
110
169
  const webpackPluginsAutoI18n = require('webpack-auto-i18n-plugin')
@@ -130,11 +189,11 @@ module.exports = {
130
189
 
131
190
  ---
132
191
 
133
- ### 3️⃣ 翻译器配置示例
192
+ ### 4️⃣ 翻译器配置示例 🛠️
134
193
 
135
- 插件默认使用谷歌翻译(默认配置代理端口7890)。在网络不支持访问谷歌的情况下,我们建议优先选择 **有道翻译** ✨,翻译效果优秀。目前插件已经内置谷歌、有道和百度翻译功能。如果需要自定义翻译器,可参考下方的示例。
194
+ 插件默认使用谷歌翻译(默认配置代理端口7890)。在网络不支持访问谷歌的情况下,我们推荐使用 **有道翻译** ✨,其翻译效果优秀。目前插件已经内置谷歌、有道和百度翻译功能。如果需要自定义翻译器,可参考下方的示例。
136
195
 
137
- 下方的示例以`vite`为例,`webpack`与其类似。
196
+ 以下示例以 `vite` 为例,`webpack` 与其类似。
138
197
 
139
198
  #### **使用谷歌翻译(默认)**
140
199
 
@@ -184,11 +243,11 @@ translator: new BaiduTranslator({
184
243
  ...
185
244
  ```
186
245
 
187
- #### **火山引擎ai翻译器**
246
+ #### **火山引擎AI翻译器**
188
247
 
189
- 支持调用`doubao`或`deepseek`进行翻译,ai大模型的翻译效果会比传统的api翻译更准确,但缺点在于耗时更长。
248
+ 支持调用 `doubao` 或 `deepseek` 进行翻译,AI大模型的翻译效果会比传统的API翻译更准确,但耗时较长。
190
249
  火山引擎大模型介绍:https://www.volcengine.com/docs/82379/1099455。
191
- 需要开通大模型服务并申请api,[api文档](https://www.volcengine.com/docs/82379/1298454)。
250
+ 需要开通大模型服务并申请API,[api文档](https://www.volcengine.com/docs/82379/1298454)。
192
251
 
193
252
  ```javascript
194
253
  import { VolcengineTranslator } from 'vite-auto-i18n-plugin'
@@ -203,7 +262,7 @@ translator: new VolcengineTranslator({
203
262
 
204
263
  #### **空翻译器**
205
264
 
206
- 如果只需要扫描目标语言,不翻译的话,该翻译器会生成json文件.
265
+ 如果只需要扫描目标语言,不进行翻译,该翻译器会生成 JSON 文件。
207
266
 
208
267
  ```javascript
209
268
  import { EmptyTranslator } from 'vite-auto-i18n-plugin'
@@ -217,7 +276,7 @@ translator: new EmptyTranslator()
217
276
 
218
277
  如果你有一个自用的翻译接口,可以通过以下方式自定义翻译器——
219
278
 
220
- 最简单的就是直接用`Translator`基类定义翻译器实例。
279
+ 最简单的方式是使用 `Translator` 基类定义翻译器实例。
221
280
 
222
281
  ```javascript
223
282
  import { Translator } from 'vite-auto-i18n-plugin'
@@ -239,7 +298,7 @@ translator: new Translator({
239
298
  ...
240
299
  ```
241
300
 
242
- 如果要实现更高阶的功能,可以使用继承,不过目前还没有对应的场景。
301
+ 如果需要更高阶的功能,可以使用继承,不过目前无相关场景。
243
302
 
244
303
  ```javascript
245
304
  import { Translator } from 'vite-auto-i18n-plugin'
@@ -260,12 +319,12 @@ translator: new CustomTranslator()
260
319
 
261
320
  ---
262
321
 
263
- ### 4️⃣ 项目入口配置 🏗️
322
+ ### 5️⃣ 项目入口配置 🏗️
264
323
 
265
- 请在 **项目入口文件**(如 `main.js`)的顶部引入语言配置文件:
324
+ 请在**项目入口文件**(如 `main.js`)的顶部引入语言配置文件:
266
325
 
267
326
  ```javascript
268
- import '../lang/index.js' // 📍 必须在入口文件中第一行引入,这个文件会在运行插件时自动生成,默认在打包配置目录的同一层的lang文件夹中,其中的index.js 就是配置文件了
327
+ import '../lang/index.js' // 📍 必须在入口文件中第一行引入,文件会在运行插件时自动生成,默认位于打包配置目录同层的lang文件夹中,其中的index.js就是配置文件
269
328
  ```
270
329
 
271
330
  ---
@@ -418,6 +477,14 @@ const HelloWorld: React.FC<HelloWorldProps> = ({ name = 'World' }) => {
418
477
 
419
478
  ## 更新日志
420
479
 
480
+ ### v1.1.3 (推荐版本)
481
+
482
+ - 新增无刷更新语言能力及最佳实践
483
+
484
+ ### v1.1.2
485
+
486
+ - 修复ai翻译异常
487
+
421
488
  ### v1.1.1
422
489
 
423
490
  - 新增翻译禁用功能