Skip to content

Commit 9f379cf

Browse files
authored
breacking: change globalInjection default disable (#204)
1 parent f1e94dc commit 9f379cf

File tree

7 files changed

+69
-61
lines changed

7 files changed

+69
-61
lines changed

docs-old/.vuepress/config.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ module.exports = {
1515
lang: 'ru-RU',
1616
title: 'Vue I18n',
1717
description: 'Vue I18n — плагин для интернационализации во Vue.js'
18-
},
18+
}
1919
},
20-
head: [
21-
['meta', { name: 'theme-color', content: '#3eaf7c' }],
22-
],
20+
head: [['meta', { name: 'theme-color', content: '#3eaf7c' }]],
2321
serviceWorker: false,
2422
themeConfig: {
2523
repo: 'kazupon/vue-i18n',
@@ -70,11 +68,13 @@ module.exports = {
7068
items: [
7169
{
7270
text: 'BabelEdit',
73-
link: 'https://www.codeandweb.com/babeledit?utm_campaign=vue-i18n-2019-01'
71+
link:
72+
'https://www.codeandweb.com/babeledit?utm_campaign=vue-i18n-2019-01'
7473
},
7574
{
7675
text: 'i18n Ally',
77-
link: 'https://marketplace.visualstudio.com/items?itemName=antfu.i18n-ally'
76+
link:
77+
'https://marketplace.visualstudio.com/items?itemName=antfu.i18n-ally'
7878
}
7979
]
8080
}
@@ -116,10 +116,7 @@ module.exports = {
116116
{
117117
title: 'Legacy',
118118
collapsable: false,
119-
children: [
120-
'/legacy/',
121-
'/legacy/v5'
122-
]
119+
children: ['/legacy/', '/legacy/v5']
123120
}
124121
]
125122
},
@@ -131,7 +128,7 @@ module.exports = {
131128
nav: [
132129
{
133130
text: '指南',
134-
link: '/zh/guide/formatting',
131+
link: '/zh/guide/formatting'
135132
},
136133
{
137134
text: 'API',
@@ -166,7 +163,8 @@ module.exports = {
166163
items: [
167164
{
168165
text: 'BabelEdit',
169-
link: 'https://www.codeandweb.com/babeledit?utm_campaign=vue-i18n-2019-01'
166+
link:
167+
'https://www.codeandweb.com/babeledit?utm_campaign=vue-i18n-2019-01'
170168
}
171169
]
172170
}
@@ -208,10 +206,7 @@ module.exports = {
208206
{
209207
title: 'Legacy',
210208
collapsable: false,
211-
children: [
212-
'/zh/legacy/',
213-
'/zh/legacy/v5'
214-
]
209+
children: ['/zh/legacy/', '/zh/legacy/v5']
215210
}
216211
]
217212
},
@@ -223,7 +218,7 @@ module.exports = {
223218
nav: [
224219
{
225220
text: 'Руководство',
226-
link: '/ru/guide/formatting',
221+
link: '/ru/guide/formatting'
227222
},
228223
{
229224
text: 'Справочник API',
@@ -258,11 +253,13 @@ module.exports = {
258253
items: [
259254
{
260255
text: 'BabelEdit',
261-
link: 'https://www.codeandweb.com/babeledit?utm_campaign=vue-i18n-2019-01'
256+
link:
257+
'https://www.codeandweb.com/babeledit?utm_campaign=vue-i18n-2019-01'
262258
},
263259
{
264260
text: 'i18n Ally',
265-
link: 'https://marketplace.visualstudio.com/items?itemName=antfu.i18n-ally'
261+
link:
262+
'https://marketplace.visualstudio.com/items?itemName=antfu.i18n-ally'
266263
}
267264
]
268265
}
@@ -304,14 +301,10 @@ module.exports = {
304301
{
305302
title: 'Устаревшие версии',
306303
collapsable: false,
307-
children: [
308-
'/ru/legacy/',
309-
'/ru/legacy/v5'
310-
]
304+
children: ['/ru/legacy/', '/ru/legacy/v5']
311305
}
312306
]
313-
},
307+
}
314308
}
315309
}
316310
}
317-

docs/advanced/composition.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ In the Composition API mode, plural translations have been integrated into `t`.
179179

180180
## Datetime Formatting
181181

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`.
183183

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:
185185

186186
```html
187187
<template>
@@ -227,9 +227,9 @@ For more details of `d`, see the [API Reference](../api/composition#d-value).
227227

228228
## Number Formatting
229229

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.
231231

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:
233233

234234
```html
235235
<template>
@@ -303,7 +303,9 @@ Then you can compose using the functions and properties exposed from the Compose
303303

304304
Another way to refer a global scope Composer instance is through properties and functions implicitly injected into the component.
305305

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:
307309

308310
- `$i18n`: An object wrapped with the following global scope Composer instance properties
309311
- `locale`
@@ -328,8 +330,11 @@ You’ve noticed that the ones listed above are prifixed with `$`. The reason fo
328330

329331
By doing so, the user is made aware that they are special properties and functions.
330332

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.
332335

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+
:::
333338

334339
## Local scope
335340

@@ -423,16 +428,7 @@ In this example, the definition of resources is separated from i18n custom block
423428

424429
### Global Scope
425430

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-
<select v-model="$i18n.locale">
430-
<option value="en">en</option>
431-
<option value="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.
436432

437433
```js
438434
setup() {
@@ -444,11 +440,29 @@ setup() {
444440
}
445441
```
446442

443+
And you can also use the setup context in the template, which can be changed as follows:
444+
445+
```html
446+
<select v-model="locale">
447+
<option value="en">en</option>
448+
<option value="ja">ja</option>
449+
</select>
450+
```
451+
447452
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.
448453

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+
<select v-model="$i18n.locale">
458+
<option value="en">en</option>
459+
<option value="ja">ja</option>
460+
</select>
461+
```
462+
449463
### Local Scope
450464

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`.
452466

453467
:::tip NOTE
454468
If you do not want to inherit the locale from the global scope, the `inheritLocale` option of `useI18n` must be `false`.

docs/api/injection.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,10 @@ $d(value: number | Date): DateTimeFormatResult;
776776

777777
If this is used in a reactive context, it will re-evaluate once the locale changes.
778778

779-
In [Compostion API mode](general#i18nmode), the `$d` 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#d](composition#d-value).
780-
781779
In [Compostion API mode](general#i18nmode), the input / output is the same as for VueI18n instance. About details, see [VueI18n#d](legacy#d-value).
782780

781+
In [Compostion API mode](general#i18nmode), the `$d` 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#d](composition#d-value).
782+
783783
**See Also**
784784
- [Datetime Formatting](../essentials/datetime)
785785
- [Scope and Locale Changing](../essentials/scope)
@@ -969,10 +969,10 @@ $n(value: number): NumberFormatResult;
969969

970970
If this is used in a reactive context, it will re-evaluate once the locale changes.
971971

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-
974972
In [Legacy API mode](general#i18nmode), the input / output is the same as for VueI18n instance. About details, see [VueI18n#n](legacy#n-value).
975973

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).
975+
976976
**See Also**
977977
- [Number Formatting](../essentials/number)
978978
- [Scope and Locale Changing](../essentials/scope)

examples/composition/formatting/linked.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<body>
1010
<div id="app">
1111
<form>
12-
<label>{{ $t('message.language') }}</label>
13-
<select v-model="$i18n.locale">
12+
<label>{{ t('message.language') }}</label>
13+
<select v-model="locale">
1414
<option value="en">en</option>
1515
<option value="ja">ja</option>
1616
</select>
@@ -64,12 +64,12 @@
6464

6565
const app = createApp({
6666
setup() {
67-
const { t } = useI18n()
67+
const { t, locale } = useI18n()
6868

6969
// Something to do ...
7070
//
7171

72-
return { t }
72+
return { t, locale }
7373
}
7474
})
7575
app.use(i18n)

examples/composition/scope/global.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ <h1>Child</h1>
7676

7777
const i18n = createI18n({
7878
legacy: false,
79+
globalInjection: true,
7980
locale: 'ja',
8081
messages: {
8182
en: {

src/i18n.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ export interface I18nAdditionalOptions {
8484
* Whether Whether to inject global properties & functions into for each component.
8585
*
8686
* @remarks
87-
* If set to `false`, then properties and methods prefixed with `$` are injected into Vue Component.
87+
* If set to `true`, then properties and methods prefixed with `$` are injected into Vue Component.
8888
*
8989
* @VueI18nSee [Implicit with injected properties and functions](../advanced/composition#implicit-with-injected-properties-and-functions)
9090
* @VueI18nSee [ComponentCustomProperties](injection#componentcustomproperties)
9191
*
92-
* @defaultValue `true`
92+
* @defaultValue `false`
9393
*/
9494
globalInjection?: boolean
9595
}
@@ -315,7 +315,7 @@ export function createI18n<
315315
const __legacyMode = __FEATURE_LEGACY_API__ && isBoolean(options.legacy)
316316
? options.legacy
317317
: true
318-
const __globalInjection = !options.globalInjection
318+
const __globalInjection = !!options.globalInjection
319319
const __instances = new Map<
320320
ComponentInternalInstance,
321321
VueI18n<Messages> | Composer<Messages>

src/vue.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ declare module '@vue/runtime-core' {
5555
* @remarks
5656
* If this is used in a reactive context, it will re-evaluate once the locale changes.
5757
*
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+
*
5860
* In {@link I18nMode | Compostion API mode}, the `$t` is injected by `app.config.globalProperties`.
5961
* the input / output is the same as for Composer, and it work on **global scope**. About that details, see {@link Composer#t | `Composer#t` }.
6062
*
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`}.
62-
*
6363
* @param key - A target locale message key
6464
*
6565
* @returns translation message
@@ -427,11 +427,11 @@ declare module '@vue/runtime-core' {
427427
* @remarks
428428
* If this is used in a reactive context, it will re-evaluate once the locale changes.
429429
*
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+
*
430432
* In {@link I18nMode | Compostion API mode}, the `$d` is injected by `app.config.globalProperties`.
431433
* 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` }.
432434
*
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-
*
435435
* @param value - A value, timestamp number or `Date` instance
436436
*
437437
* @returns formatted value
@@ -531,11 +531,11 @@ declare module '@vue/runtime-core' {
531531
* @remarks
532532
* If this is used in a reactive context, it will re-evaluate once the locale changes.
533533
*
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+
*
534536
* In {@link I18nMode | Compostion API mode}, the `$n` is injected by `app.config.globalProperties`.
535537
* 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` }.
536538
*
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` }.
538-
*
539539
* @param value - A number value
540540
*
541541
* @returns formatted value
@@ -629,12 +629,12 @@ declare module '@vue/runtime-core' {
629629
/**
630630
* Locale messages getter
631631
*
632+
* 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+
*
632634
* @remarks
633635
* In {@link I18nMode | Compostion API mode}, the `$tm` is injected by `app.config.globalProperties`.
634636
* 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` }.
635637
*
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` }.
637-
*
638638
* @param key - A target locale message key
639639
*
640640
* @return locale messages

0 commit comments

Comments
 (0)