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
Copy file name to clipboardExpand all lines: docs/advanced/composition.md
+31-17Lines changed: 31 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -179,9 +179,9 @@ In the Composition API mode, plural translations have been integrated into `t`.
179
179
180
180
## Datetime Formatting
181
181
182
-
In the Legacy API mode, Datetime value was localized using `$d` or the VueI18n instance of `d`.
182
+
In the Legacy API mode, Datetime value was formatted using `$d` or the VueI18n instance of `d`.
183
183
184
-
In the Compostion API mode, it uses the `d` of the Composer instance to localize:
184
+
In the Compostion API mode, it uses the `d` of the Composer instance to format:
185
185
186
186
```html
187
187
<template>
@@ -227,9 +227,9 @@ For more details of `d`, see the [API Reference](../api/composition#d-value).
227
227
228
228
## Number Formatting
229
229
230
-
In the Legacy API mode, Number value is localized using `$n` or the `n` of the VueI18n instance.
230
+
In the Legacy API mode, Number value is formatted using `$n` or the `n` of the VueI18n instance.
231
231
232
-
In the Compostion API mode, it is localized using the `n` of the Composer instance:
232
+
In the Compostion API mode, it is formatted using the `n` of the Composer instance:
233
233
234
234
```html
235
235
<template>
@@ -303,7 +303,9 @@ Then you can compose using the functions and properties exposed from the Compose
303
303
304
304
Another way to refer a global scope Composer instance is through properties and functions implicitly injected into the component.
305
305
306
-
If you use Compostion API mode with the `legacy: false` option for `createI18n`, Vue I18n defaults to the following properties and functions, which injected to the component via `app.config.globalProperties`:
306
+
You need to specify **`globalInjection: true`** together with `legacy: false` as an option for `createI18n`, because disabled by default.
307
+
308
+
This allows Vue I18n to inject the following properties and functions into the components:
307
309
308
310
-`$i18n`: An object wrapped with the following global scope Composer instance properties
309
311
-`locale`
@@ -328,8 +330,11 @@ You’ve noticed that the ones listed above are prifixed with `$`. The reason fo
328
330
329
331
By doing so, the user is made aware that they are special properties and functions.
330
332
331
-
With the previously explained explicit way, you had to run `useI18n` in `setup` every time. **If your Vue application doesn’t use local scope and does everything i18n in global scope**, it is very convenient because you don’t have to run `useI18n` in the `setup` every time.
333
+
:::warning NOTICE
334
+
If your Vue application doesn’t use local scope and does everything i18n in global scope, this is very useful as it does not need to run `useI18n` in the `setup` for each component. However, this way has the problem with global variables of the same nature. You should be used with caution, especially for large Vue applications.
332
335
336
+
If you use it once and stop using it, you must change all the properties or functions used in the templates to those of the setup context returned with the `setup` using the `useI18n` with `useScope: 'global'` option.
337
+
:::
333
338
334
339
## Local scope
335
340
@@ -423,16 +428,7 @@ In this example, the definition of resources is separated from i18n custom block
423
428
424
429
### Global Scope
425
430
426
-
Changing the locale in the global scope, i.e., Composer instance `locale` property referenced by i18n instance `global` property, you can change with `$i18n.locale`.
427
-
428
-
```html
429
-
<selectv-model="$i18n.locale">
430
-
<optionvalue="en">en</option>
431
-
<optionvalue="ja">ja</option>
432
-
</select>
433
-
```
434
-
435
-
If you want to change the locale with `setup`, just get a global Composer with `useI18n` and change it using the `locale` property of the instance.
431
+
You want to change the locale with `setup`, just get a global Composer with `useI18n` and change it using the `locale` property of the instance.
436
432
437
433
```js
438
434
setup() {
@@ -444,11 +440,29 @@ setup() {
444
440
}
445
441
```
446
442
443
+
And you can also use the setup context in the template, which can be changed as follows:
444
+
445
+
```html
446
+
<selectv-model="locale">
447
+
<optionvalue="en">en</option>
448
+
<optionvalue="ja">ja</option>
449
+
</select>
450
+
```
451
+
447
452
When you change the locale of the global scope, components that depend on the global scope, such as `t` translation API can work reactive and switch the display messages to those of the target locale.
448
453
454
+
If you are using the [implicit way](composition#implicit-with-injected-properties-and-functions), you can also change it in template with `$i18n.locale`, as follows:
455
+
456
+
```html
457
+
<selectv-model="$i18n.locale">
458
+
<optionvalue="en">en</option>
459
+
<optionvalue="ja">ja</option>
460
+
</select>
461
+
```
462
+
449
463
### Local Scope
450
464
451
-
The local scope locales, that is, the Composer instance `locale` property returned by `useI18n`, are inherited from the global scope, as is the Legacy API. Therefore, when you change the locale at global scope, the inherited local scope locale is also changed. If you want to switch the locale for the whole application, you need to do so via`$i18n.locale`.
465
+
The local scope locales, that is, the Composer instance `locale` property returned by `useI18n`, are inherited from the global scope, as is the Legacy API. Therefore, when you change the locale at global scope, the inherited local scope locale is also changed. If you want to switch the locale for the whole application, you can use the `locale` returned by `useI18n({ useScope: 'global' })` or, if you use [implicit way](composition#implicit-with-injected-properties-and-functions), you can use`$i18n.locale`.
452
466
453
467
:::tip NOTE
454
468
If you do not want to inherit the locale from the global scope, the `inheritLocale` option of `useI18n` must be `false`.
In [CompostionAPImode](general#i18nmode), the `$d` isinjectedby `app.config.globalProperties`. Theinput / outputisthesameasforComposerinstance, anditworkon **globalscope**. Aboutthatdetails, see [Composer#d](composition#d-value).
780
-
781
779
In [CompostionAPImode](general#i18nmode), theinput / outputisthesameasforVueI18ninstance. Aboutdetails, see [VueI18n#d](legacy#d-value).
782
780
781
+
In [CompostionAPImode](general#i18nmode), the `$d` isinjectedby `app.config.globalProperties`. Theinput / outputisthesameasforComposerinstance, anditworkon **globalscope**. Aboutthatdetails, see [Composer#d](composition#d-value).
If this is used in a reactive context, it will re-evaluate once the locale changes.
971
971
972
-
In [Compostion API mode](general#i18nmode), the `$n` is injected by `app.config.globalProperties`. The input / output is the same as for Composer instance, and it work on **global scope**. About that details, see [Composer#n](composition#n-value).
973
-
974
972
In [Legacy API mode](general#i18nmode), the input / output is the same as for VueI18n instance. About details, see [VueI18n#n](legacy#n-value).
975
973
974
+
In [Compostion API mode](general#i18nmode), the `$n` is injected by `app.config.globalProperties`. The input / output is the same as for Composer instance, and it work on **global scope**. About that details, see [Composer#n](composition#n-value).
* If this is used in a reactive context, it will re-evaluate once the locale changes.
57
57
*
58
+
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#t | `VueI18n#t`}.
59
+
*
58
60
* In {@link I18nMode | Compostion API mode}, the `$t` is injected by `app.config.globalProperties`.
59
61
* the input / output is the same as for Composer, and it work on **global scope**. About that details, see {@link Composer#t | `Composer#t` }.
60
62
*
61
-
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#t | `VueI18n#t`}.
* If this is used in a reactive context, it will re-evaluate once the locale changes.
429
429
*
430
+
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#d | `VueI18n#d` }.
431
+
*
430
432
* In {@link I18nMode | Compostion API mode}, the `$d` is injected by `app.config.globalProperties`.
431
433
* the input / output is the same as for Composer instance, and it work on **global scope**. About that details, see {@link Composer#d | `Composer#d` }.
432
434
*
433
-
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#d | `VueI18n#d` }.
434
-
*
435
435
* @param value - A value, timestamp number or `Date` instance
* If this is used in a reactive context, it will re-evaluate once the locale changes.
533
533
*
534
+
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#n | `VueI18n.n` }.
535
+
*
534
536
* In {@link I18nMode | Compostion API mode}, the `$n` is injected by `app.config.globalProperties`.
535
537
* the input / output is the same as for Composer instance, and it work on **global scope**. About that details, see {@link Composer#n | `Composer.n` }.
536
538
*
537
-
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#n | `VueI18n.n` }.
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#tm | `VueI18n#tm` }.
633
+
*
632
634
* @remarks
633
635
* In {@link I18nMode | Compostion API mode}, the `$tm` is injected by `app.config.globalProperties`.
634
636
* the input / output is the same as for Composer instance, and it work on **global scope**. About that details, see {@link Composer#tm | `Composer.tm` }.
635
637
*
636
-
* In {@link I18nMode | Legacy API mode}, the input / output is the same as for VueI18n instance. About that details, see {@link VueI18n#tm | `VueI18n#tm` }.
0 commit comments