You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: add defaultSFCLocale and globalSFCScope to sfc (#555)
* docs: add `defaultSFCLocale` and `globalSFCScope` to sfc
* docs: add `defaultSFCLocale` hint and `globalSFCScope` warning
* docs: add warning and `WIP` for rollup and webpack plugins for `defaultSFCLocale` and `globalSFCScope` global options
* docs: remove warning from `globalSFCScope` on `rollup` and `webpack` plugins
Copy file name to clipboardExpand all lines: docs/guide/advanced/sfc.md
+192-2Lines changed: 192 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ export default {
36
36
37
37
In i18n custom blocks, the format of the locale messages resource is **json** format by default.
38
38
39
-
The locale messages defined by i18n custom blocks are used as the local scope in single file components.
39
+
The locale messages defined by i18n custom blocks, are used as the local scope in single file components.
40
40
41
41
If `$t('hello')` is used in the template, the `hello` key defined by `i18n` custom blocks is referred to.
42
42
@@ -68,6 +68,8 @@ npm i --save-dev @intlify/vite-plugin-vue-i18n
68
68
69
69
#### Configuration
70
70
71
+
See also [use global format](#use-global-format-with-vite-plugin) and [use global scope](#use-global-scope-with-vite-plugin).
72
+
71
73
vite config for example:
72
74
73
75
```ts
@@ -108,6 +110,8 @@ npm i --save-dev @intlify/vue-i18n-loader
108
110
109
111
#### Configuration
110
112
113
+
See also [use global format](#use-global-format-with-webpack-plugin) and [use global scope](#use-global-scope-with-webpack-plugin).
114
+
111
115
Webpack config for example:
112
116
113
117
```js
@@ -128,7 +132,7 @@ module.exports = {
128
132
path.resolve(__dirname, './src/locales'),
129
133
],
130
134
loader:'@intlify/vue-i18n-loader'
131
-
}
135
+
},
132
136
// for i18n custom block
133
137
{
134
138
resourceQuery:/blockType=i18n/,
@@ -162,6 +166,8 @@ npm i --save-dev @intlify/rollup-plugin-vue-i18n
162
166
163
167
#### Configuration
164
168
169
+
See also [use global format](#use-global-format-with-rollup-plugin) and [use global scope](#use-global-scope-with-rollup-plugin).
170
+
165
171
Rollup config for example:
166
172
167
173
```js
@@ -295,6 +301,93 @@ The `i18n` custom blocks below of `json5` format:
295
301
</i18n>
296
302
```
297
303
304
+
### Define global format
305
+
306
+
If you are using one of `@intlify/vite-plugin-vue-i18n`, `@intlify/vue-i18n-loader` or `@intlify/rollup-plugin-vue-i18n`
307
+
plugin on your project, you can also define the `lang` for all your inlined `i18n` custom blocks
308
+
on all your `SFC` using the `defaultSFCLocale` option.
309
+
310
+
:::warning:::
311
+
`@intlify/vue-i18n-loader` and `@intlify/rollup-plugin-vue-i18n` are currently in progress to add this feature.
312
+
313
+
On inlined `i18n` custom blocks that have specified the `lang` attribute, the `defaultSFCLang` is not applied.
314
+
315
+
For example, in order to configure `yml` format on all your inlined `i18n` custom blocks on all your `SFC`:
316
+
317
+
```html
318
+
<!-- custom block equivalent to <i18n lang="yml"> -->
319
+
<i18n>
320
+
en:
321
+
hello: Hello
322
+
es:
323
+
hello: Hola
324
+
</i18n>
325
+
```
326
+
327
+
just configure `defaultSFCLocale: "yml"` on your plugin configuration:
328
+
329
+
#### Use global format with vite plugin
330
+
331
+
```javascript
332
+
importvuefrom'@vitejs/plugin-vue'
333
+
importvueI18nfrom'@intlify/vite-plugin-vue-i18n'
334
+
335
+
plugins: [
336
+
vue(),
337
+
vueI18n({
338
+
defaultSFCLocale:"yml",
339
+
/* other options */
340
+
})
341
+
]
342
+
```
343
+
344
+
#### Use global format with webpack plugin (WIP)
345
+
346
+
```javascript
347
+
module.exports= {
348
+
// ...
349
+
module: {
350
+
rules: [
351
+
{
352
+
test:/\.vue$/,
353
+
loader:'vue-loader',
354
+
},
355
+
// for i18n resources (json/json5/yaml)
356
+
{
357
+
test:/\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files
358
+
type:'javascript/auto',
359
+
defaultSFCLocale:'yml',
360
+
/* other options */,
361
+
loader:'@intlify/vue-i18n-loader'
362
+
},
363
+
// for i18n custom block
364
+
{
365
+
resourceQuery:/blockType=i18n/,
366
+
type:'javascript/auto',
367
+
loader:'@intlify/vue-i18n-loader'
368
+
}
369
+
// ...
370
+
]
371
+
}
372
+
// ...
373
+
}
374
+
```
375
+
376
+
#### Use global format with rollup plugin (WIP)
377
+
378
+
```javascript
379
+
plugins: [
380
+
// set `customBlocks` opton to `rollup-plugin-vue`
381
+
VuePlugin({ customBlocks: ['i18n'] }),
382
+
// set `rollup-plugin-vue-i18n` after **`rollup-plugin-vue`**
383
+
VueI18nPlugin({
384
+
defaultSFCLocale:'yml',
385
+
/* other options */
386
+
}),
387
+
/* other plugins */
388
+
]
389
+
```
390
+
298
391
## Define locale messages for global scope
299
392
300
393
You can use define locale messages for global scope with `global` attribute:
@@ -311,6 +404,103 @@ You can use define locale messages for global scope with `global` attribute:
311
404
312
405
In the above example, since the `global` attribute is set, the locale messages defined in `i18n` custom blocks can be merged as a resource for locale messages of global scope.
313
406
407
+
### Define global scope
408
+
409
+
If you are using one of `@intlify/vite-plugin-vue-i18n`, `@intlify/vue-i18n-loader` or
410
+
`@intlify/rollup-plugin-vue-i18n`
411
+
plugin on your project, you can also define the `global` scope for all your `i18n` custom blocks
412
+
on all your `SFC` using the `globalSFCScope` option.
413
+
414
+
:::warning:::
415
+
`@intlify/vue-i18n-loader` and `@intlify/rollup-plugin-vue-i18n` are currently in progress to add this feature.
416
+
417
+
**Warning**: beware enabling `globalSFCScope: true`, all `i18n` custom blocks in all your `SFC` will be on `global` scope.
418
+
419
+
For example, in order to configure `global` scope on all your `i18n` custom blocks on all your `SFC`:
420
+
421
+
```html
422
+
<!-- custom block equivalent to <i18n lang="yml" global> -->
423
+
<i18nlang="yml">
424
+
en:
425
+
hello: Hello
426
+
es:
427
+
hello: Hola
428
+
</i18n>
429
+
```
430
+
431
+
or
432
+
433
+
```html
434
+
<!-- custom block equivalent to <i18n src="./locales/myMessages.json" global> -->
435
+
<i18nsrc="./locales/myMessages.json5">
436
+
</i18n>
437
+
```
438
+
439
+
just configure `globalSFCScope: true` on your plugin configuration:
440
+
441
+
#### Use global format with vite plugin
442
+
443
+
```javascript
444
+
importvuefrom'@vitejs/plugin-vue'
445
+
importvueI18nfrom'@intlify/vite-plugin-vue-i18n'
446
+
447
+
plugins: [
448
+
vue(),
449
+
vueI18n({
450
+
globalSFCScope:true,
451
+
/* other options */
452
+
})
453
+
]
454
+
```
455
+
456
+
#### Use global format with webpack plugin (WIP)
457
+
458
+
```javascript
459
+
module.exports= {
460
+
// ...
461
+
module: {
462
+
rules: [
463
+
{
464
+
test:/\.vue$/,
465
+
loader:'vue-loader',
466
+
},
467
+
// for i18n resources (json/json5/yaml)
468
+
{
469
+
test:/\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files
470
+
type:'javascript/auto',
471
+
globalSFCScope:true,
472
+
/* other options */,
473
+
loader:'@intlify/vue-i18n-loader'
474
+
},
475
+
// for i18n custom block
476
+
{
477
+
resourceQuery:/blockType=i18n/,
478
+
type:'javascript/auto',
479
+
loader:'@intlify/vue-i18n-loader'
480
+
}
481
+
// ...
482
+
]
483
+
}
484
+
// ...
485
+
}
486
+
```
487
+
488
+
#### Use global format with rollup plugin (WIP)
489
+
490
+
```javascript
491
+
plugins: [
492
+
// set `customBlocks` opton to `rollup-plugin-vue`
493
+
VuePlugin({ customBlocks: ['i18n'] }),
494
+
// set `rollup-plugin-vue-i18n` after **`rollup-plugin-vue`**
495
+
VueI18nPlugin({
496
+
globalSFCScope:true,
497
+
/* other options */
498
+
}),
499
+
/* other plugins */
500
+
]
501
+
```
502
+
503
+
314
504
:::warning NOTICE
315
505
The locale messages for global scope defined in i18n custom blocks are available **only composition API mode**. You need to run `useI18n` option to `useScope: 'global'` at `setup`. About details, see the [Composition API](./composition).
0 commit comments