Skip to content

Commit 688bf1b

Browse files
authored
docs: add how to use shared locale messages on composition api (#1964)
* docs: add how to use shared locale messages on composition api * chore: fix ci
1 parent 0878d89 commit 688bf1b

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ jobs:
110110
test-e2e:
111111
name: E2E test
112112

113+
if: github.event_name == 'push' &&
114+
!startsWith(github.event.head_commit.message, 'docs')
113115
needs:
114116
- build
115117

docs/guide/advanced/composition.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,11 @@ const { t, d, n, tm, locale } = useI18n({
377377
// Something to do here ...
378378
```
379379

380+
### Locale messages
380381

381-
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`.
382+
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`.
382383

383-
The following is an example of using i18n custom blocks and `useI18n` options:
384+
The following is an example of using i18n custom blocks and `useI18n` options:
384385

385386
```vue
386387
<script setup>
@@ -410,6 +411,56 @@ availableLocales.forEach(locale => {
410411
In this example, the definition of resources is separated from i18n custom blocks and the `messages` option of `useI18n`, but in local scope, resource messages are specified in the `messages` option in a lump sum for administrative purposes in resource messages or define all resource messages in the i18n custom blocks, which is preferable.
411412
:::
412413

414+
### Shared locale messages for components
415+
416+
In Leacy API mode, shared locale messages are used in components with the `sharedMessages` option.
417+
418+
In composition API mode, use `mergeLocaleMessage` exported by `useI18n`.
419+
420+
Common locale messages example:
421+
422+
```js
423+
export default {
424+
en: {
425+
buttons: {
426+
save: "Save",
427+
// ...
428+
}
429+
},
430+
ja: {
431+
buttons: {
432+
save: "保存",
433+
// ...
434+
}
435+
}
436+
}
437+
```
438+
439+
use `mergeLocaleMessage` on Components:
440+
441+
```vue
442+
<script setup>
443+
import { useI18n } from 'vue-i18n'
444+
import commonMessages from './locales/common'
445+
446+
const { t, mergeLocaleMessage } = useI18n({
447+
locale: 'en',
448+
messages: {
449+
en: {
450+
hello: 'Hello!'
451+
},
452+
ja: {
453+
hello: 'こんにちは!'
454+
}
455+
}
456+
})
457+
458+
for (const locale of ['en', 'ja']) {
459+
mergeLocaleMessage(locale, commonMessages[locale])
460+
}
461+
</script>
462+
```
463+
413464
## Locale Changing
414465

415466
### Global Scope

0 commit comments

Comments
 (0)