auto-i18n-plugin-core 1.1.1 → 1.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-i18n-plugin-core",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "vite, webpack automatic translation plugin core code.",
5
5
  "keywords": [
6
6
  "i18n",
package/readme-en.md ADDED
@@ -0,0 +1,482 @@
1
+ <div align="center">
2
+ <img src="./logo.svg" alt="auto-i18n-translation-plugins" width="300">
3
+ </div>
4
+
5
+ # 🚀 auto-i18n-translation-plugins
6
+
7
+ ## 🍉 Plugin Introduction
8
+
9
+ A 🎉 **frontend auto-translation plugin** that supports all JavaScript-based frontend frameworks (like Vue2/3 and React). No source code modification needed! Easily achieve multi-language support with one click 🌐🚀. Comes with Youdao and Google translation services by default, while supporting custom translators. Compatible with major build tools like Webpack, Vite, and Rollup.
10
+
11
+ ### 🎯 Key Features:
12
+
13
+ - 🛠️ **No source code changes** - Quick multi-language translation with one click
14
+ - 🌐 **Supports multiple translation services** (Google, Youdao, and custom translators)
15
+ - 🔍 **Smart detection** of text needing translation
16
+ - 🔧 **Flexible configuration options** for different project needs
17
+
18
+ Note: Youdao translation requires users to apply for their translation service - the demo keys have been exhausted.
19
+
20
+ ---
21
+
22
+ ## 📚 Plugin Debugging
23
+
24
+ ```bash
25
+ pnpm install
26
+ pnpm run build
27
+ pnpm run preview // Don't select React, contains too much English text
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 📖 Supported Features
33
+
34
+ - **Frameworks**: All JavaScript-based frontend frameworks (Vue2/3, React, etc.)
35
+ - **Build Tools**: Fully compatible with Webpack, Vite, and Rollup 🚀
36
+ - **Translation Services**: Default support for **Youdao** and **Google** translation, plus custom translators
37
+
38
+ ---
39
+
40
+ ## 🌟 Quick Start
41
+
42
+ ### 1️⃣ Install Plugin
43
+
44
+ #### **Vite Projects:**
45
+
46
+ ```bash
47
+ npm install vite-auto-i18n-plugin --save-dev
48
+ # or
49
+ yarn add vite-auto-i18n-plugin --dev
50
+ ```
51
+
52
+ #### **Webpack Projects:**
53
+
54
+ ```bash
55
+ npm install webpack-auto-i18n-plugin --save-dev
56
+ # or
57
+ yarn add webpack-auto-i18n-plugin --dev
58
+ ```
59
+
60
+ ---
61
+
62
+ ### 2️⃣ Basic Configuration
63
+
64
+ #### **Vite Configuration Example** (vite.config.js):
65
+
66
+ ```javascript
67
+ import vitePluginsAutoI18n, { YoudaoTranslator } from 'vite-auto-i18n-plugin'
68
+ import vue from '@vitejs/plugin-vue'
69
+ import { defineConfig } from 'vite'
70
+
71
+ export default defineConfig({
72
+ plugins: [
73
+ vue({
74
+ template: {
75
+ compilerOptions: {
76
+ hoistStatic: false,
77
+ cacheHandlers: false
78
+ }
79
+ }
80
+ }),
81
+ vitePluginAutoI18n({
82
+ translator: new YoudaoTranslator({
83
+ appId: '4cdb9baea8066fef',
84
+ appKey: 'ONI6AerZnGRyDqr3w7UM730mPuF8mB3j'
85
+ })
86
+ })
87
+ ]
88
+ })
89
+ ```
90
+
91
+ #### **Webpack Configuration Example** (webpack.config.js):
92
+
93
+ ```javascript
94
+ const webpackPluginsAutoI18n = require('webpack-auto-i18n-plugin')
95
+ const { YoudaoTranslator } = require('webpack-auto-i18n-plugin')
96
+
97
+ const i18nPlugin = new webpackPluginsAutoI18n.default({
98
+ translator: new YoudaoTranslator({
99
+ appId: '4cdb9baea8066fef',
100
+ appKey: 'ONI6AerZnGRyDqr3w7UM730mPuF8mB3j'
101
+ })
102
+ })
103
+
104
+ module.exports = {
105
+ plugins: [
106
+ new VueLoaderPlugin(),
107
+ new HtmlWebpackPlugin({
108
+ template: './public/index.html'
109
+ }),
110
+ i18nPlugin
111
+ ]
112
+ }
113
+ ```
114
+
115
+ ---
116
+
117
+ ### 3️⃣ Translator Configuration Examples
118
+
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.
120
+
121
+ #### **Using Google Translate (default)**
122
+
123
+ ```javascript
124
+ import { GoogleTranslator } from 'vite-auto-i18n-plugin'
125
+
126
+ ...
127
+ translator: new GoogleTranslator({
128
+ proxyOption: {
129
+ host: '127.0.0.1',
130
+ port: 7890,
131
+ headers: {
132
+ 'User-Agent': 'Node'
133
+ }
134
+ }
135
+ })
136
+ ...
137
+ ```
138
+
139
+ #### **Using Youdao Translate**
140
+
141
+ Requires API application: [API Docs](https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html)
142
+
143
+ ```javascript
144
+ import { YoudaoTranslator } from 'vite-auto-i18n-plugin'
145
+
146
+ ...
147
+ translator: new YoudaoTranslator({
148
+ appId: 'your-app-id',
149
+ appKey: 'your-app-key'
150
+ })
151
+ ...
152
+ ```
153
+
154
+ #### **Baidu Translator**
155
+
156
+ Requires API application: [API Docs](https://api.fanyi.baidu.com/product/113)
157
+
158
+ ```javascript
159
+ import { BaiduTranslator } from 'vite-auto-i18n-plugin'
160
+
161
+ ...
162
+ translator: new BaiduTranslator({
163
+ appId: 'your-app-id', // Baidu AppId
164
+ appKey: 'your-app-key' // Baidu AppKey
165
+ })
166
+ ...
167
+ ```
168
+
169
+ #### **Volcengine AI Translator**
170
+
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
+
175
+ ```javascript
176
+ import { VolcengineTranslator } from 'vite-auto-i18n-plugin'
177
+
178
+ ...
179
+ 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'
182
+ })
183
+ ...
184
+ ```
185
+
186
+ ````
187
+
188
+ #### **Empty Translator**
189
+
190
+ If you only need to scan target language without translation, this translator will generate JSON files.
191
+
192
+ ```javascript
193
+ import { EmptyTranslator } from 'vite-auto-i18n-plugin'
194
+
195
+ ...
196
+ translator: new EmptyTranslator()
197
+ ...
198
+ ````
199
+
200
+ #### **Custom Translator**
201
+
202
+ If you have your own translation API, you can create a custom translator:
203
+
204
+ Simplest way is using the base `Translator` class:
205
+
206
+ ```javascript
207
+ import { Translator } from 'vite-auto-i18n-plugin'
208
+ import axios from 'axios'
209
+
210
+ ...
211
+ translator: new Translator({
212
+ name: 'My Translator',
213
+ // Translation method
214
+ fetchMethod: (str, fromKey, toKey, _separator) => {
215
+ // Actual API calls may be more complex than this example
216
+ const myApi = 'https://www.my-i18n.cn/api/translate?from=${fromKey}&to=${toKey}&t={+new Date}'
217
+ return axios.post(myApi, { str })
218
+ .then(res => res.data)
219
+ },
220
+ // API call interval (some APIs may block frequent requests)
221
+ interval: 1000
222
+ })
223
+ ...
224
+ ```
225
+
226
+ For advanced functionality, you can extend the class (though no current use cases):
227
+
228
+ ```javascript
229
+ import { Translator } from 'vite-auto-i18n-plugin'
230
+
231
+ class CustomTranslator extends Translator {
232
+ constructor () {
233
+ super({
234
+ name: 'My Translator',
235
+ ...
236
+ })
237
+ }
238
+ }
239
+
240
+ ...
241
+ translator: new CustomTranslator()
242
+ ...
243
+ ```
244
+
245
+ ---
246
+
247
+ ### 4️⃣ Project Entry Configuration 🏗️
248
+
249
+ Add this at the top of your **project entry file** (e.g., `main.js`):
250
+
251
+ ```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
253
+ ```
254
+
255
+ ---
256
+
257
+ ## ⚙️ Configuration Parameters
258
+
259
+ | Parameter | Type | Required | Default | Description |
260
+ | -------------------- | ---------- | -------- | ------------------------ | -------------------------------------------------------- |
261
+ | enabled | boolean | ❌ | `true` | Whether to trigger translation. |
262
+ | translateType | string | ❌ | `full-auto` | Translation mode: `full-auto` or `semi-auto` |
263
+ | translateKey | string | ✅ | `$t` | Translation function name (e.g., `$t`) |
264
+ | excludedCall | string[] | ❌ | `['$i8n', 'require', …]` | Function calls to exclude from translation |
265
+ | excludedPattern | RegExp[] | ❌ | `[/\.\w+$/]` | Patterns to exclude (e.g., file extensions) |
266
+ | excludedPath | string[] | ❌ | `['node_modules']` | Directories to exclude (e.g., `node_modules`) |
267
+ | includePath | RegExp[] | ❌ | `[/src\//]` | Whitelist of directories to include (default: `src`) |
268
+ | globalPath | string | ❌ | `'./lang'` | Path for translation config files |
269
+ | distPath | string | ✅ | `''` | Output path for built files |
270
+ | distKey | string | ✅ | `'index'` | Main translation file name |
271
+ | namespace | string | ✅ | `lang` | Project namespace for distinguishing translation configs |
272
+ | originLang | string | ✅ | `'zh-cn'` | Source language |
273
+ | targetLangList | string[] | ✅ | `['en']` | Target languages |
274
+ | buildToDist | boolean | ❌ | `false` | Whether to bundle translation files into main build |
275
+ | translator | Translator | ❌ | `GoogleTranslator` | Translator instance |
276
+ | translatorOption | object | ❌ | `{}` | Translator options (lower priority than `translator`) |
277
+ | rewriteConfig | boolean | ❌ | `true` | Whether to rewrite config file on each plugin run |
278
+ | deepScan | boolean | ❌ | `false` | Experimental: Whether to perform deep string scanning |
279
+ | commonTranslateKey | string | ❌ | `''` | General translation key |
280
+ | insertFileExtensions | string[] | ❌ | `[]` | List of file extensions to insert translation code into |
281
+
282
+ ---
283
+
284
+ ## 🔍 What does `deepScan` do?
285
+
286
+ `deepScan` is an experimental property controlling whether the plugin performs deep string scanning.
287
+
288
+ By default, the plugin scans strings/template strings - if any target language text is found, the entire string is included:
289
+
290
+ ```js
291
+ ;`<div>
292
+ <p>你好</p>
293
+ </div>`
294
+ ```
295
+
296
+ Since there's Chinese text, the whole string would be included, potentially causing inaccurate translation. With `deepScan` enabled, the plugin splits and reconstructs template strings, only translating matching text:
297
+
298
+ ```js
299
+ ;`<div>
300
+ <p>${$t('你好')}</p>
301
+ </div>`
302
+ ```
303
+
304
+ Now only '你好' gets translated, not the entire string.
305
+
306
+ ---
307
+
308
+ ## 👋 `translateType` Functionality
309
+
310
+ Added in v1.0.11, with two options:
311
+
312
+ `full-auto`: Fully automatic translation (default supports Chinese, Japanese, Korean, Russian)
313
+
314
+ `semi-auto`: Semi-automatic translation (supports all source languages)
315
+
316
+ In `semi-auto` mode, users must wrap target text with `translateKey` (e.g., `$t('hello')`), which the plugin will then translate.
317
+
318
+ Example:
319
+
320
+ ```js
321
+ const HelloWorld: React.FC<HelloWorldProps> = ({ name = 'World' }) => {
322
+ return (
323
+ <div className="hello-world">
324
+ <h1>
325
+ {$t('Hello,')} {name}!
326
+ </h1>
327
+ <p>{$t('Welcome to our application')}</p>
328
+ </div>
329
+ )
330
+ }
331
+ ```
332
+
333
+ ---
334
+
335
+ ## ❓ Why `buildToDist`?
336
+
337
+ In Vite, the plugin only generates translation config files. Without `buildToDist`, these files won't be bundled. ⚠️ Enabling this bundles them but may create duplicate config files.
338
+
339
+ ---
340
+
341
+ ## 🔄 How to Update Translations?
342
+
343
+ After running the plugin, two files are generated in `\lang`:
344
+
345
+ - **index.js**: Translation function logic
346
+ - **index.json**: Translation content
347
+
348
+ To update translations, simply modify `index.json`.
349
+
350
+ ---
351
+
352
+ ## ⚠️ Usage Notes
353
+
354
+ 1. **Proxy Requirements**
355
+
356
+ - In China, **Youdao Translate** is strongly recommended
357
+ - Google Translate requires proxy configuration
358
+ - Default proxy port: **7890** (customizable via `proxyOption`)
359
+
360
+ 2. **Translation Frequency**
361
+
362
+ - Google Translate is free but may limit frequent requests 🔒
363
+ - Set reasonable intervals between requests 💡
364
+
365
+ 3. **Translation Updates**
366
+ - `index.json` in `globalPath` is the core translation file
367
+ - Changes take effect immediately after saving
368
+
369
+ ---
370
+
371
+ ## 📦 User Group
372
+
373
+ WeChat Group
374
+
375
+ ![wx](./wx.jpg)
376
+
377
+ ---
378
+
379
+ ## 📦 Example Project
380
+
381
+ Example project: [example](./example) (click to view)
382
+
383
+ ## 📜 License
384
+
385
+ MIT License 🪪 - Free to use and contribute!
386
+
387
+ May this plugin make your i18n development easier and more efficient! 🌍✨
388
+
389
+ ---
390
+
391
+ ## ✨ Update
392
+
393
+ Since v1.0.5, simply import the generated `index.js` in your entry file - no need to manually create language switching functions. 👏
394
+
395
+ ---
396
+
397
+ ## 🎆 Authors
398
+
399
+ Original authors: wenps, xu-code, Caleb-Xu, Winfans
400
+
401
+ ## Changelog
402
+
403
+ ### v1.1.1
404
+
405
+ - New translation disable function
406
+
407
+ ### v1.1.0
408
+
409
+ - New AI translator
410
+ - New Vue2 extension plugin mechanism for translation plugins
411
+
412
+ ### v1.0.26 (Recommended Version)
413
+
414
+ - Added custom extension arrays
415
+
416
+ ### v1.0.25 (Recommended Version)
417
+
418
+ - Added a universal translation key
419
+
420
+ ### v1.0.24 (Recommended Version)
421
+
422
+ - Fixed the exception issue in semi-automatic mode
423
+
424
+ ### v1.0.23 (Recommended Version)
425
+
426
+ - Fixed a major bug in packaging and writing.
427
+
428
+ ### v1.0.22
429
+
430
+ - Added scan translator
431
+
432
+ ### v1.0.21
433
+
434
+ - Added deep scanning
435
+
436
+ ### v1.0.20
437
+
438
+ - Fixed filter function issues and added config overwrite option
439
+
440
+ ### v1.0.19
441
+
442
+ - Backward compatibility for config files
443
+
444
+ ### v1.0.18
445
+
446
+ - Fixed optional chaining issues in older Node versions
447
+
448
+ ### v1.0.17
449
+
450
+ - Basic SSR support (experimental)
451
+
452
+ ### v1.0.16
453
+
454
+ - Fixed Vue3 comment node issues
455
+
456
+ ### v1.0.15
457
+
458
+ - Added Baidu Translate
459
+
460
+ ### v1.0.14
461
+
462
+ - Fixed new language type segmentation
463
+ - Added Japanese, Korean, Russian support
464
+
465
+ ### v1.0.13
466
+
467
+ - Bug fixes
468
+
469
+ ### v1.0.12
470
+
471
+ - Type optimizations
472
+
473
+ ### v1.0.11
474
+
475
+ - Bug fixes
476
+ - Added `translateType` option for semi-auto mode
477
+
478
+ ```js
479
+ // Users can wrap text with translateKey
480
+ // e.g., $t('hello') - plugin will scan and translate these
481
+ $t('hello')
482
+ ```