Skip to content

Commit 1ae02a4

Browse files
committed
update docs
1 parent b6680a6 commit 1ae02a4

File tree

6 files changed

+138
-11
lines changed

6 files changed

+138
-11
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ If you use stable Vue I18n version, see this [repository](https://github.com/kaz
3030

3131
## :star: New Features
3232

33-
- `Composable API`: new style API for Vue 3
34-
- See the following docs:
35-
- [createI18n](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.createi18ncomposer.md)
36-
- [I18nComposerOptions](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.i18ncomposeroptions.md)
37-
- [I18nComposer](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.i18ncomposer.md)
38-
- [useI18n](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.usei18n.md)
33+
### Composable API
3934

35+
New style API for Vue 3. See the following docs:
36+
37+
- [createI18nComposer](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.createi18ncomposer.md)
38+
- [I18nComposerOptions](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.i18ncomposeroptions.md)
39+
- [I18nComposer](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.i18ncomposer.md)
40+
- [useI18n](https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.usei18n.md)
4041

4142

4243
## :lollipop: Examples
@@ -48,12 +49,12 @@ The examples are offered that use the following two API styles:
4849
- composable
4950
- new vue-i18n API optimized for Vue 3. details about API
5051
- legacy
51-
- vue-i18n API almost compatible with vue-i18n@8.x
52+
- vue-i18n API almost compatible with vue-i18n v8.x
5253

5354

5455
## :heavy_exclamation_mark: Known issues
5556

56-
### :boom: Breaking changes compared to vue-i18n@8.x
57+
### :boom: Breaking changes compared to vue-i18n v8.x
5758

5859
- API
5960
- The return value of `$t` and `t` methods is **string** only. object and array values ​​are no longer returned.

docs/vue-i18n.createi18ncomposer.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@ export declare function createI18nComposer(options?: I18nComposerOptions): I18nC
2222

2323
`I18nComposer`
2424

25+
## Example
26+
27+
case: Global resource base localization
28+
29+
```js
30+
import { createApp } from 'vue'
31+
import { createI18nComposer, useI18n } 'vue-i18n'
32+
33+
const i18n = createI18nComposer({
34+
locale: 'ja',
35+
messages: {
36+
en: { ... }
37+
ja: { ... }
38+
}
39+
})
40+
41+
const app = createApp({
42+
setup() {
43+
return useI18n()
44+
}
45+
})
46+
47+
app.use(i18n)
48+
app.mount('#app')
49+
50+
```
51+

docs/vue-i18n.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| --- | --- |
1111
| [createI18n(options)](./vue-i18n.createi18n.md) | createI18n factory<!-- -->This function is compatible with constructor of <code>VueI18n</code> class (offered with vue-i18n<!-- -->@<!-- -->8.x) like <code>new VueI18n(...)</code>. |
1212
| [createI18nComposer(options)](./vue-i18n.createi18ncomposer.md) | I18n Composer factory |
13-
| [useI18n(options)](./vue-i18n.usei18n.md) | |
13+
| [useI18n(options)](./vue-i18n.usei18n.md) | Enable vue-i18n composable API |
1414

1515
## Interfaces
1616

docs/vue-i18n.usei18n.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## useI18n() function
66

7+
Enable vue-i18n composable API
8+
79
<b>Signature:</b>
810

911
```typescript
@@ -20,3 +22,40 @@ export declare function useI18n(options?: I18nComposerOptions): I18nComposer;
2022

2123
`I18nComposer`
2224

25+
## Example
26+
27+
case: Component resource base localization
28+
29+
```html
30+
<template>
31+
<form>
32+
<label>{{ t('language') }}</label>
33+
<select v-model="locale">
34+
<option value="en">en</option>
35+
<option value="ja">ja</option>
36+
</select>
37+
</form>
38+
<p>message: {{ t('hello') }}</p>
39+
</template>
40+
41+
<script>
42+
import { useI18n } from 'vue-i18n'
43+
44+
export default {
45+
setup() {
46+
const { t, locale } = useI18n({
47+
locale: 'ja',
48+
messages: {
49+
en: { ... },
50+
ja: { ... }
51+
}
52+
})
53+
// Something to do ...
54+
55+
return { ..., t, locale }
56+
}
57+
}
58+
</script>
59+
60+
```
61+

src/composer.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,31 @@ function getLocaleMessages(
186186
}
187187

188188
/**
189-
* I18n Composer factory
189+
* I18n Composer factory
190+
*
191+
* @example
192+
* case: Global resource base localization
193+
* ```js
194+
* import { createApp } from 'vue'
195+
* import { createI18nComposer, useI18n } 'vue-i18n'
196+
*
197+
* const i18n = createI18nComposer({
198+
* locale: 'ja',
199+
* messages: {
200+
* en: { ... }
201+
* ja: { ... }
202+
* }
203+
* })
204+
*
205+
* const app = createApp({
206+
* setup() {
207+
* return useI18n()
208+
* }
209+
* })
210+
*
211+
* app.use(i18n)
212+
* app.mount('#app')
213+
* ```
190214
*/
191215
export function createI18nComposer(
192216
options: I18nComposerOptions = {}

src/use.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,43 @@ const providers: Map<
2323
const generateSymbolID = (): string =>
2424
`vue-i18n-${new Date().getUTCMilliseconds().toString()}`
2525

26-
// enable composable API via I18n Composer
26+
/**
27+
* Enable vue-i18n composable API
28+
*
29+
* @example
30+
* case: Component resource base localization
31+
* ```html
32+
* <template>
33+
* <form>
34+
* <label>{{ t('language') }}</label>
35+
* <select v-model="locale">
36+
* <option value="en">en</option>
37+
* <option value="ja">ja</option>
38+
* </select>
39+
* </form>
40+
* <p>message: {{ t('hello') }}</p>
41+
* </template>
42+
*
43+
* <script>
44+
* import { useI18n } from 'vue-i18n'
45+
*
46+
* export default {
47+
* setup() {
48+
* const { t, locale } = useI18n({
49+
* locale: 'ja',
50+
* messages: {
51+
* en: { ... },
52+
* ja: { ... }
53+
* }
54+
* })
55+
* // Something to do ...
56+
*
57+
* return { ..., t, locale }
58+
* }
59+
* }
60+
* </script>
61+
* ```
62+
*/
2763
export function useI18n(options?: I18nComposerOptions): I18nComposer {
2864
const globalComposer = inject(GlobalI18nSymbol)
2965
if (!globalComposer) throw new Error('TODO') // TODO:

0 commit comments

Comments
 (0)