Skip to content

Commit 72f4a9b

Browse files
authored
docs: add composition api miguration from legacy api (#956)
1 parent 8790ed9 commit 72f4a9b

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

docs/guide/migration/ways.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,84 @@ About how to usage, See the [here](https://github.com/intlify/vue-i18n-composabl
230230
:::warning NOTICE
231231
`vue-i18n-composable` allows the main API of Vue I18n v8.x to work with the Composition API. All of Composition API provided in Vue I18n v9 are not available.
232232
:::
233+
234+
## Migration to Composition API from Legacy API
235+
236+
### Summary
237+
238+
Vue I18n supports both styles which are Legacy API mode and Composiyion API mode. Legacy API mode is Options API style, and Composition API mode support Vue Composition API that is able to compose with function.
239+
240+
Legacy API mode is almost compatible with legacy Vue I18n v8.x, making it relatively easy to migrate Vue applications to Vue 3. Vue 3 supports the Options API style, so existing Vue 2 applications will be cases where applications will be migrated to Vue 3.
241+
242+
Vue 3 allows you to make Vue applications using a mix of the Options API style and Composition API style, but Vue I18n has not allowed for a mix of these API styles since the v9 initial release, so you can use either one or the other API style only.
243+
244+
Developing a Vue application with a mix of Options API styles and Compostion API styles is not a desirable software development project from a maintenance standpoint. This is because the cost of maintaining such code is high. However, there are advantages to using both styles. In particular, API style migration is easier to migrate step-by-step, since it works even when implemented in both API styles.
245+
246+
From Vue I18n v9.2, the Legacy API mode can also be used with Composition API mode.
247+
248+
### Limitations
249+
250+
**The Composition API in Legacy API mode does not support SSR**, So You should understand as a limited feature for migration.
251+
252+
### How to migration
253+
254+
#### `createI18n`
255+
256+
You need specify `allowComposition: true` to `createI18n` otpions. the below example:
257+
258+
```js
259+
import { createI18n } from 'vue-i18n'
260+
261+
const i18n = createI18n({
262+
locale: 'en',
263+
allowCompositoin: true, // you need to specify that!
264+
messages: {
265+
en: {
266+
hello: 'hello!'
267+
},
268+
ja: {
269+
hello: 'こんにちは!'
270+
}
271+
}
272+
})
273+
274+
console.log(i18n.allowComposition) // output is true
275+
```
276+
277+
### `useI18n` in Vue Component
278+
#### `setup` option
279+
280+
```vue
281+
<script>
282+
import { defineComponent } from 'vue'
283+
import { useI18n } from 'vue-i18n'
284+
285+
export default defineComponent({
286+
name: 'Hello',
287+
setup() {
288+
const { t } = useI18n() // use as global scope
289+
return { t }
290+
}
291+
})
292+
</script>
293+
294+
<template>
295+
<p>{{ $t('hello') }}</p>
296+
<p>{{ t('hello') }}</p>
297+
</template>
298+
```
299+
300+
#### `<script setup>`
301+
302+
```vue
303+
<script setup>
304+
import { useI18n } from 'vue-i18n'
305+
306+
const { t } = useI18n() // use as global scope
307+
</script>
308+
309+
<template>
310+
<p>{{ $t('hello') }}</p>
311+
<p>{{ t('hello') }}</p>
312+
</template>
313+
```

docs/ja/guide/migration/ways.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,84 @@ About how to usage, See the [here](https://github.com/intlify/vue-i18n-composabl
199199
:::warning NOTICE
200200
`vue-i18n-composable` allows the main API of Vue I18n v8.x to work with the Composition API. All of Composition API provided in Vue I18n v9 are not available.
201201
:::
202+
203+
## Migration to Composition API from Legacy API
204+
205+
### Summary
206+
207+
Vue I18n supports both styles which are Legacy API mode and Composiyion API mode. Legacy API mode is Options API style, and Composition API mode support Vue Composition API that is able to compose with function.
208+
209+
Legacy API mode is almost compatible with legacy Vue I18n v8.x, making it relatively easy to migrate Vue applications to Vue 3. Vue 3 supports the Options API style, so existing Vue 2 applications will be cases where applications will be migrated to Vue 3.
210+
211+
Vue 3 allows you to make Vue applications using a mix of the Options API style and Composition API style, but Vue I18n has not allowed for a mix of these API styles since the v9 initial release, so you can use either one or the other API style only.
212+
213+
Developing a Vue application with a mix of Options API styles and Compostion API styles is not a desirable software development project from a maintenance standpoint. This is because the cost of maintaining such code is high. However, there are advantages to using both styles. In particular, API style migration is easier to migrate step-by-step, since it works even when implemented in both API styles.
214+
215+
From Vue I18n v9.2, the Legacy API mode can also be used with Composition API mode.
216+
217+
### Limitations
218+
219+
**The Composition API in Legacy API mode does not support SSR**, So You should understand as a limited feature for migration.
220+
221+
### How to migration
222+
223+
#### `createI18n`
224+
225+
You need specify `allowComposition: true` to `createI18n` otpions. the below example:
226+
227+
```js
228+
import { createI18n } from 'vue-i18n'
229+
230+
const i18n = createI18n({
231+
locale: 'en',
232+
allowCompositoin: true, // you need to specify that!
233+
messages: {
234+
en: {
235+
hello: 'hello!'
236+
},
237+
ja: {
238+
hello: 'こんにちは!'
239+
}
240+
}
241+
})
242+
243+
console.log(i18n.allowComposition) // output is true
244+
```
245+
246+
### `useI18n` in Vue Component
247+
#### `setup` option
248+
249+
```vue
250+
<script>
251+
import { defineComponent } from 'vue'
252+
import { useI18n } from 'vue-i18n'
253+
254+
export default defineComponent({
255+
name: 'Hello',
256+
setup() {
257+
const { t } = useI18n() // use as global scope
258+
return { t }
259+
}
260+
})
261+
</script>
262+
263+
<template>
264+
<p>{{ $t('hello') }}</p>
265+
<p>{{ t('hello') }}</p>
266+
</template>
267+
```
268+
269+
#### `<script setup>`
270+
271+
```vue
272+
<script setup>
273+
import { useI18n } from 'vue-i18n'
274+
275+
const { t } = useI18n() // use as global scope
276+
</script>
277+
278+
<template>
279+
<p>{{ $t('hello') }}</p>
280+
<p>{{ t('hello') }}</p>
281+
</template>
282+
```

0 commit comments

Comments
 (0)