vue-i18n-composable
vue-i18n-composable is a library that brings the Composition API pattern for internationalization (i18n) to Vue 2.x applications, specifically integrating with the popular `vue-i18n` library. Its current stable version is `2.0.0`, released in July 2022, which introduced direct support for Vue 2.7 without requiring the `@vue/composition-api` plugin. Previous versions, such as `v1.0.0`, targeted Vue 2.5 and 2.6 and necessitated the `@vue/composition-api` plugin. The package does not adhere to a strict time-based release cadence, with major versions often tied to critical Vue lifecycle updates or feature enhancements. It differentiates itself by allowing developers to leverage modern `setup` functions and `useI18n` composables within Vue 2 projects, offering a forward-looking development experience without the need for a full migration to Vue 3. This is particularly useful for large legacy Vue 2 applications aiming to incrementally modernize their codebase, and it is an official part of the Intlify ecosystem, which also maintains `vue-i18n` itself.
Common errors
-
Cannot read properties of undefined (reading 'mount')
cause Attempting to use `createApp` from `@vue/composition-api` for mounting a Vue 2.7+ application without correctly initializing Vue.fixFor Vue 2.7+, use `const app = new Vue({ ... }); app.$mount('#app');`. The `createApp` utility is only relevant for Vue 2.5-2.6 when using `@vue/composition-api`. -
TypeError: Cannot read properties of undefined (reading 't')
cause The `useI18n()` composable was called outside of a Vue component's `setup` function or an equivalent Composition API context, or the `i18n` instance was not properly provided to the Vue app.fixEnsure `useI18n()` is called within a `setup` function and that the `i18n` instance created by `createI18n` is passed to the root Vue instance configuration (`new Vue({ i18n, ... })`). -
Error: [vue-i18n] 'messages' property is missing in 'createI18n' options.
cause The `messages` property, which defines the translation dictionary, was not provided in the options object passed to `createI18n`.fixEnsure the `createI18n` function receives an options object with at least an empty `messages` property, e.g., `createI18n({ locale: 'en', messages: {} })`.
Warnings
- breaking Vue version compatibility changed between v1 and v2. v1.x supports Vue 2.5-2.6, requiring `@vue/composition-api`. v2.x supports Vue 2.7+, which has native Composition API support and does not require the plugin.
- gotcha When using `vue-i18n-composable` with Vue 2.5 or 2.6, you must explicitly install and `Vue.use()` the `@vue/composition-api` plugin before creating your Vue app.
- gotcha The `defineComponent` import source varies based on your Vue 2.x version. For Vue 2.7+, it's imported from 'vue'. For Vue 2.5-2.6, it comes from '@vue/composition-api'.
Install
-
npm install vue-i18n-composable -
yarn add vue-i18n-composable -
pnpm add vue-i18n-composable
Imports
- createI18n
const { createI18n } = require('vue-i18n-composable')import { createI18n } from 'vue-i18n-composable' - useI18n
import useI18n from 'vue-i18n-composable'
import { useI18n } from 'vue-i18n-composable' - defineComponent
import { defineComponent } from '@vue/composition-api'import { defineComponent } from 'vue'
Quickstart
import Vue from 'vue'
import { createI18n } from 'vue-i18n-composable'
import { defineComponent } from 'vue' // For Vue 2.7+
const i18n = createI18n({
locale: 'en',
messages: {
en: {
greeting: 'Hello from Composition API!',
language: 'English'
},
ja: {
greeting: 'コンポジションAPIからのこんにちは!',
language: '日本語'
}
}
})
const App = defineComponent({
template: `
<div>
<h1>{{ t('greeting') }}</h1>
<p>Current Language: {{ t('language') }}</p>
<button @click="setLocale('ja')">Switch to Japanese</button>
<button @click="setLocale('en')">Switch to English</button>
</div>
`,
setup() {
const { t, locale } = useI18n()
const setLocale = (lang) => {
locale.value = lang
}
return {
t,
setLocale
}
}
})
const app = new Vue({
render: h => h(App),
i18n
})
app.$mount('#app')