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
When using `useI18n`, the `useScope` option has implicit resolution behavior that you should be aware of:
381
+
382
+
:::info Default Behavior
383
+
If you don't explicitly specify `useScope`, Vue I18n will implicitly determine the scope based on whether the component has an i18n block:
384
+
-**With i18n block**: Defaults to `local` scope
385
+
-**Without i18n block**: Defaults to `global` scope
386
+
:::
387
+
388
+
```js
389
+
// In a component WITH i18n block
390
+
391
+
// This implicitly uses local scope
392
+
const { t } =useI18n() // same as useI18n({ useScope: 'local' })
393
+
394
+
395
+
// In a component WITHOUT i18n block (e.g., composables, stores)
396
+
397
+
// This implicitly uses global scope
398
+
const { t } =useI18n() // same as useI18n({ useScope: 'global' })
399
+
```
400
+
401
+
This explicit approach prevents unexpected behavior and makes your code more maintainable.
402
+
403
+
### Avoiding Multiple useI18n Calls
404
+
405
+
:::warning IMPORTANT
406
+
**Do not call `useI18n` with local scope multiple times within the same component.** When you call `useI18n` with local scope more than once in the same component, it will not work properly and Vue I18n will emit a warning.
407
+
:::
408
+
409
+
#### Bad: Multiple calls to useI18n with local scope
410
+
411
+
```js
412
+
exportdefault {
413
+
setup() {
414
+
// First call - creates a new Composer instance
415
+
const { t } =useI18n({
416
+
locale:'en',
417
+
messages: {
418
+
en: { hello:'Hello' },
419
+
ja: { hello:'こんにちは' }
420
+
}
421
+
})
422
+
423
+
// Second call - creates another Composer instance (triggers warning)
424
+
const { locale } =useI18n({
425
+
locale:'en',
426
+
messages: {
427
+
en: { world:'World' },
428
+
ja: { world:'世界' }
429
+
}
430
+
})
431
+
432
+
// These instances are not synchronized!
433
+
return { t, locale }
434
+
}
435
+
}
436
+
```
437
+
438
+
#### Good: Single call to useI18n
439
+
440
+
```js
441
+
exportdefault {
442
+
setup() {
443
+
// Destructure all needed properties from a single call
444
+
const { t, locale, tm, d, n } =useI18n({
445
+
locale:'en',
446
+
messages: {
447
+
en: {
448
+
hello:'Hello',
449
+
world:'World'
450
+
},
451
+
ja: {
452
+
hello:'こんにちは',
453
+
world:'世界'
454
+
}
455
+
}
456
+
})
457
+
458
+
return { t, locale }
459
+
}
460
+
}
461
+
```
462
+
463
+
When you violate this rule, you'll see the following warning in development mode:
464
+
465
+
```
466
+
[Vue I18n warn]: Duplicate `useI18n` calling by local scope. Please don't call it on local scope, due to it does not work properly in component.
467
+
```
468
+
469
+
If you need to use i18n features in multiple places within your component, destructure all the needed properties from a single `useI18n` call or store the returned object and access its properties as needed.
470
+
471
+
378
472
### Locale messages
379
473
380
474
If you use i18n custom blocks in SFC as i18n resource of locale messages, it will be merged with the locale messages specified by the `messages` option of `useI18n`.
0 commit comments