rsbuild-auto-i18n-plugin 1.1.10

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-en.md ADDED
@@ -0,0 +1,602 @@
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
+ ## ❓ Common Issues
41
+
42
+ [Documentation](https://juejin.cn/post/7483435518526062626).
43
+
44
+ ---
45
+
46
+ ## 🌟 Quick Start 🚀
47
+
48
+ ### 1️⃣ Install Plugin 📦
49
+
50
+ #### **🌐 Vite Project:**
51
+
52
+ ```bash
53
+ npm install vite-auto-i18n-plugin --save-dev
54
+ # or
55
+ yarn add vite-auto-i18n-plugin --dev
56
+ ```
57
+
58
+ #### **🛠️ Webpack Project:**
59
+
60
+ ```bash
61
+ npm install webpack-auto-i18n-plugin --save-dev
62
+ # or
63
+ yarn add webpack-auto-i18n-plugin --dev
64
+ ```
65
+
66
+ ---
67
+
68
+ ### 2️⃣ Language Switching 🌐
69
+
70
+ #### ⚙️ Basic Switching
71
+
72
+ ```js
73
+ window.localStorage.setItem('lang', value)
74
+ window.location.reload()
75
+
76
+ // value is the key of the language mapping object
77
+ // The mapping object is located in the lang/index.js file by default. You can check this part of the code inside.
78
+ const langMap = {
79
+ 'en': (),
80
+ 'zhcn': ()
81
+ }
82
+ ```
83
+
84
+ #### 📲 Instant Language Switching
85
+
86
+ 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.
87
+
88
+ ```js
89
+ window.$changeLang('en')
90
+ ```
91
+
92
+ For example, in Vue2, you can switch languages without refreshing the page in the following way:
93
+
94
+ ```Html
95
+ <template>
96
+ <div id="app" v-if="isShow">
97
+ <button @click="changeLang('en')">Switch to English</button>
98
+ <button @click="changeLang('zh-cn')">Switch to Chinese</button>
99
+ <router-view></router-view>
100
+ </div>
101
+ </template>
102
+ <script>
103
+ export default {
104
+ data() {
105
+ return {
106
+ isShow: true
107
+ }
108
+ },
109
+ methods: {
110
+ changeLang(lang) {
111
+ window.$changeLang(lang)
112
+ this.isShow = false
113
+ this.$nextTick(() => {
114
+ this.isShow = true
115
+ })
116
+ }
117
+ }
118
+ }
119
+ </script>
120
+ ```
121
+
122
+ #### 🔄 Replace Language Packs
123
+
124
+ If you want to modify the built - in generated language packs, you can directly modify the `langMap` in the global object. For example:
125
+
126
+ ```js
127
+ window.langMap = {
128
+ en: {
129
+ zccsau6: 'hello'
130
+ },
131
+ 'zh-cn': {
132
+ zccsau6: '你好'
133
+ }
134
+ }
135
+ ```
136
+
137
+ Then you can refresh the page by using the above **Instant Language Switching** method.
138
+
139
+ ---
140
+
141
+ ### 3️⃣ Basic Configuration 🔧
142
+
143
+ #### **Vite Configuration Example** (vite.config.js):
144
+
145
+ ```javascript
146
+ import vitePluginsAutoI18n, { YoudaoTranslator } from 'vite-auto-i18n-plugin'
147
+ import vue from '@vitejs/plugin-vue'
148
+ import { defineConfig } from 'vite'
149
+
150
+ export default defineConfig({
151
+ plugins: [
152
+ vue({
153
+ template: {
154
+ compilerOptions: {
155
+ hoistStatic: false,
156
+ cacheHandlers: false
157
+ }
158
+ }
159
+ }),
160
+ vitePluginsAutoI18n({
161
+ translator: new YoudaoTranslator({
162
+ appId: '4cdb9baea8066fef',
163
+ appKey: 'ONI6AerZnGRyDqr3w7UM730mPuF8mB3j'
164
+ })
165
+ })
166
+ ]
167
+ })
168
+ ```
169
+
170
+ #### **Webpack Configuration Example** (webpack.config.js):
171
+
172
+ ```javascript
173
+ const webpackPluginsAutoI18n = require('webpack-auto-i18n-plugin')
174
+ const { YoudaoTranslator } = require('webpack-auto-i18n-plugin')
175
+
176
+ const i18nPlugin = new webpackPluginsAutoI18n.default({
177
+ translator: new YoudaoTranslator({
178
+ appId: '4cdb9baea8066fef',
179
+ appKey: 'ONI6AerZnGRyDqr3w7UM730mPuF8mB3j'
180
+ })
181
+ })
182
+
183
+ module.exports = {
184
+ plugins: [
185
+ new VueLoaderPlugin(),
186
+ new HtmlWebpackPlugin({
187
+ template: './public/index.html'
188
+ }),
189
+ i18nPlugin
190
+ ]
191
+ }
192
+ ```
193
+
194
+ ---
195
+
196
+ ### 4️⃣ Translator Configuration Examples 🛠️
197
+
198
+ 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.
199
+
200
+ The following examples use `vite` as an example, and `webpack` is similar.
201
+
202
+ #### **Using Google Translate (Default)**
203
+
204
+ ```javascript
205
+ import { GoogleTranslator } from 'vite-auto-i18n-plugin'
206
+
207
+ ...
208
+ translator: new GoogleTranslator({
209
+ proxyOption: {
210
+ host: '127.0.0.1',
211
+ port: 7890,
212
+ headers: {
213
+ 'User-Agent': 'Node'
214
+ }
215
+ }
216
+ })
217
+ ...
218
+ ```
219
+
220
+ #### **Using Youdao Translation**
221
+
222
+ You need to apply for an API. [API documentation](https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html).
223
+
224
+ ```javascript
225
+ import { YoudaoTranslator } from 'vite-auto-i18n-plugin'
226
+
227
+ ...
228
+ translator: new YoudaoTranslator({
229
+ appId: 'The appId you applied for',
230
+ appKey: 'The appKey you applied for'
231
+ })
232
+ ...
233
+ ```
234
+
235
+ #### **Baidu Translator**
236
+
237
+ You need to apply for an API. [API documentation](https://api.fanyi.baidu.com/product/113).
238
+
239
+ ```javascript
240
+ import { BaiduTranslator } from 'vite-auto-i18n-plugin'
241
+
242
+ ...
243
+ translator: new BaiduTranslator({
244
+ appId: 'The appId you applied for', // Baidu Translate AppId
245
+ appKey: 'The appKey you applied for' // Baidu Translate AppKey
246
+ })
247
+ ...
248
+ ```
249
+
250
+ #### **Volcengine AI Translator**
251
+
252
+ 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.
253
+ Introduction to Volcengine large models: https://www.volcengine.com/docs/82379/1099455.
254
+ You need to activate the large model service and apply for an API. [API documentation](https://www.volcengine.com/docs/82379/1298454).
255
+
256
+ ```javascript
257
+ import { VolcengineTranslator } from 'vite-auto-i18n-plugin'
258
+
259
+ ...
260
+ translator: new VolcengineTranslator({
261
+ apiKey: 'The apiKey you applied for',
262
+ 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.'
263
+ })
264
+ ...
265
+ ```
266
+
267
+ #### **Empty Translator**
268
+
269
+ If you only need to scan the target language without performing translation, this translator will generate a JSON file.
270
+
271
+ ```javascript
272
+ import { EmptyTranslator } from 'vite-auto-i18n-plugin'
273
+
274
+ ...
275
+ translator: new EmptyTranslator()
276
+ ...
277
+ ```
278
+
279
+ #### **Custom Translator**
280
+
281
+ If you have a personal translation API, you can customize a translator in the following ways:
282
+
283
+ The simplest way is to define a translator instance using the `Translator` base class.
284
+
285
+ ```javascript
286
+ import { Translator } from 'vite-auto-i18n-plugin'
287
+ import axios from 'axios'
288
+
289
+ ...
290
+ translator: new Translator({
291
+ name: 'My Translator',
292
+ // Translation method
293
+ fetchMethod: (str, fromKey, toKey, _separator) => {
294
+ // 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
295
+ const myApi = 'https://www.my-i18n.cn/api/translate?from=${fromKey}&to=${toKey}&t={+new Date}'
296
+ return axios.post(myApi, { str })
297
+ .then(res => res.data)
298
+ },
299
+ // Interface trigger interval. Some interfaces may be blocked if triggered frequently. Please set a reasonable interface trigger interval according to the actual situation.
300
+ interval: 1000
301
+ })
302
+ ...
303
+ ```
304
+
305
+ If you need more advanced features, you can use inheritance. However, there is currently no relevant scenario.
306
+
307
+ ```javascript
308
+ import { Translator } from 'vite-auto-i18n-plugin'
309
+
310
+ class CustomTranslator extends Translator {
311
+ constructor () {
312
+ super({
313
+ name: 'My Translator',
314
+ ...
315
+ })
316
+ }
317
+ }
318
+
319
+ ...
320
+ translator: new CustomTranslator()
321
+ ...
322
+ ```
323
+
324
+ ---
325
+
326
+ ### 5️⃣ Project Entry Configuration 🏗️
327
+
328
+ Please import the language configuration file at the top of the **project entry file** (e.g., `main.js`):
329
+
330
+ ```javascript
331
+ 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.
332
+ ```
333
+
334
+ ---
335
+
336
+ ## ⚙️ Configuration Parameters
337
+
338
+ | Parameter | Type | Required | Default | Description |
339
+ | -------------------- | ---------- | -------- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
340
+ | enabled | boolean | ❌ | `true` | Whether to trigger translation. |
341
+ | translateType | string | ❌ | `full-auto` | Translation mode: `full-auto` or `semi-auto` |
342
+ | translateKey | string | ✅ | `$t` | Translation function name (e.g., `$t`) |
343
+ | excludedCall | string[] | ❌ | `['$i8n', 'require', …]` | Function calls to exclude from translation |
344
+ | excludedPattern | RegExp[] | ❌ | `[/\.\w+$/]` | Patterns to exclude (e.g., file extensions) |
345
+ | excludedPath | string[] | ❌ | `['node_modules']` | Directories to exclude (e.g., `node_modules`) |
346
+ | includePath | RegExp[] | ❌ | `[/src\//]` | Whitelist of directories to include (default: `src`) |
347
+ | globalPath | string | ❌ | `'./lang'` | Path for translation config files |
348
+ | distPath | string | ✅ | `''` | Output path for built files |
349
+ | distKey | string | ✅ | `'index'` | Main translation file name |
350
+ | namespace | string | ✅ | `lang` | Project namespace for distinguishing translation configs |
351
+ | originLang | string | ✅ | `'zh-cn'` | Source language |
352
+ | targetLangList | string[] | ✅ | `['en']` | Target languages |
353
+ | buildToDist | boolean | ❌ | `false` | Whether to bundle translation files into main build |
354
+ | translator | Translator | ❌ | `GoogleTranslator` | Translator instance |
355
+ | translatorOption | object | ❌ | `{}` | Translator options (lower priority than `translator`) |
356
+ | rewriteConfig | boolean | ❌ | `true` | Whether to rewrite config file on each plugin run |
357
+ | deepScan | boolean | ❌ | `false` | Experimental: Whether to perform deep string scanning |
358
+ | commonTranslateKey | string | ❌ | `''` | General translation key |
359
+ | insertFileExtensions | string[] | ❌ | `[]` | List of file extensions to insert translation code into |
360
+ | isClear | boolean | ❌ | `false` | Whether to clear content not in context (clear source language key-value pairs not in context), only supported in build mode |
361
+ | isClearSpace | boolean | ❌ | `false` | Whether to remove whitespace from both ends of source strings, whitespace is preserved by default |
362
+
363
+ ---
364
+
365
+ ## 🔍 What does `deepScan` do?
366
+
367
+ `deepScan` is an experimental property controlling whether the plugin performs deep string scanning.
368
+
369
+ By default, the plugin scans strings/template strings - if any target language text is found, the entire string is included:
370
+
371
+ ```js
372
+ ;`<div>
373
+ <p>你好</p>
374
+ </div>`
375
+ ```
376
+
377
+ 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:
378
+
379
+ ```js
380
+ ;`<div>
381
+ <p>${$t('你好')}</p>
382
+ </div>`
383
+ ```
384
+
385
+ Now only '你好' gets translated, not the entire string.
386
+
387
+ ---
388
+
389
+ ## 👋 `translateType` Functionality
390
+
391
+ Added in v1.0.11, with two options:
392
+
393
+ `full-auto`: Fully automatic translation (default supports Chinese, Japanese, Korean, Russian)
394
+
395
+ `semi-auto`: Semi-automatic translation (supports all source languages)
396
+
397
+ In `semi-auto` mode, users must wrap target text with `translateKey` (e.g., `$t('hello')`), which the plugin will then translate.
398
+
399
+ Example:
400
+
401
+ ```js
402
+ const HelloWorld: React.FC<HelloWorldProps> = ({ name = 'World' }) => {
403
+ return (
404
+ <div className="hello-world">
405
+ <h1>
406
+ {$t('Hello,')} {name}!
407
+ </h1>
408
+ <p>{$t('Welcome to our application')}</p>
409
+ </div>
410
+ )
411
+ }
412
+ ```
413
+
414
+ ---
415
+
416
+ ## ❓ Why `buildToDist`?
417
+
418
+ 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.
419
+
420
+ ---
421
+
422
+ ## 🔄 How to Update Translations?
423
+
424
+ After running the plugin, two files are generated in `\lang`:
425
+
426
+ - **index.js**: Translation function logic
427
+ - **index.json**: Translation content
428
+
429
+ To update translations, simply modify `index.json`.
430
+
431
+ ---
432
+
433
+ ## ⚠️ Usage Notes
434
+
435
+ 1. **Proxy Requirements**
436
+
437
+ - In China, **Youdao Translate** is strongly recommended
438
+ - Google Translate requires proxy configuration
439
+ - Default proxy port: **7890** (customizable via `proxyOption`)
440
+
441
+ 2. **Translation Frequency**
442
+
443
+ - Google Translate is free but may limit frequent requests 🔒
444
+ - Set reasonable intervals between requests 💡
445
+
446
+ 3. **Translation Updates**
447
+ - `index.json` in `globalPath` is the core translation file
448
+ - Changes take effect immediately after saving
449
+
450
+ ---
451
+
452
+ ## 📦 User Group
453
+
454
+ QQ Group
455
+
456
+ ![qq](./qq.jpg)
457
+
458
+ ---
459
+
460
+ ## 📦 Example Project
461
+
462
+ Example project: [example](./example) (click to view)
463
+
464
+ ## 📜 License
465
+
466
+ MIT License 🪪 - Free to use and contribute!
467
+
468
+ May this plugin make your i18n development easier and more efficient! 🌍✨
469
+
470
+ ---
471
+
472
+ ## ✨ Update
473
+
474
+ Since v1.0.5, simply import the generated `index.js` in your entry file - no need to manually create language switching functions. 👏
475
+
476
+ ---
477
+
478
+ ## 🎆 Authors
479
+
480
+ Original authors: wenps, xu-code, Caleb-Xu, Winfans
481
+
482
+ ## Changelog
483
+
484
+ ### v1.1.9 (Recommended Version)
485
+
486
+ - Fixed issue where deepScan string splitting wasn't handling line breaks and other special characters
487
+
488
+ ### v1.1.8 (Recommended Version)
489
+
490
+ - Renamed configuration for controlling leading/trailing whitespace removal
491
+
492
+ ### v1.1.7 (Recommended Version)
493
+
494
+ - Added compatibility for enable feature and translation initialization completion
495
+ - Added configuration option to control whether to remove leading/trailing whitespace during scanning
496
+ - Fixed issue with abnormal triggering of new translations
497
+ - Fixed issue where cleanup configuration was not taking effect
498
+
499
+ ### v1.1.6 (Recommended Version)
500
+
501
+ - Fixed issue where clearing whitespace before/after template strings caused mismatch between JSON and translation functions
502
+ - Fixed issue where Chinese brackets in interpolation caused interpolation to fail
503
+ - Added configuration cleanup functionality
504
+
505
+ ### v1.1.5 (Recommended Version)
506
+
507
+ - Added support for interpolation translation
508
+
509
+ ### v1.1.4 (Recommended Version)
510
+
511
+ - Fixed template string processing exception in exclude functions
512
+ - Added interval configuration for Google Translate
513
+ - Added support for custom interface parameters in translators
514
+
515
+ ### v1.1.3 (Recommended Version)
516
+
517
+ - Added the ability to update languages without refreshing and best practices
518
+
519
+ ### v1.1.2
520
+
521
+ - Fixed AI translation exceptions
522
+
523
+ ### v1.1.1
524
+
525
+ - New translation disable function
526
+
527
+ ### v1.1.0
528
+
529
+ - New AI translator
530
+ - New Vue2 extension plugin mechanism for translation plugins
531
+
532
+ ### v1.0.26 (Recommended Version)
533
+
534
+ - Added custom extension arrays
535
+
536
+ ### v1.0.25 (Recommended Version)
537
+
538
+ - Added a universal translation key
539
+
540
+ ### v1.0.24 (Recommended Version)
541
+
542
+ - Fixed the exception issue in semi-automatic mode
543
+
544
+ ### v1.0.23 (Recommended Version)
545
+
546
+ - Fixed a major bug in packaging and writing.
547
+
548
+ ### v1.0.22
549
+
550
+ - Added scan translator
551
+
552
+ ### v1.0.21
553
+
554
+ - Added deep scanning
555
+
556
+ ### v1.0.20
557
+
558
+ - Fixed filter function issues and added config overwrite option
559
+
560
+ ### v1.0.19
561
+
562
+ - Backward compatibility for config files
563
+
564
+ ### v1.0.18
565
+
566
+ - Fixed optional chaining issues in older Node versions
567
+
568
+ ### v1.0.17
569
+
570
+ - Basic SSR support (experimental)
571
+
572
+ ### v1.0.16
573
+
574
+ - Fixed Vue3 comment node issues
575
+
576
+ ### v1.0.15
577
+
578
+ - Added Baidu Translate
579
+
580
+ ### v1.0.14
581
+
582
+ - Fixed new language type segmentation
583
+ - Added Japanese, Korean, Russian support
584
+
585
+ ### v1.0.13
586
+
587
+ - Bug fixes
588
+
589
+ ### v1.0.12
590
+
591
+ - Type optimizations
592
+
593
+ ### v1.0.11
594
+
595
+ - Bug fixes
596
+ - Added `translateType` option for semi-auto mode
597
+
598
+ ```js
599
+ // Users can wrap text with translateKey
600
+ // e.g., $t('hello') - plugin will scan and translate these
601
+ $t('hello')
602
+ ```