Vue I18n
Vue I18n is the official and widely adopted internationalization plugin for Vue.js, providing comprehensive features to localize Vue 3 applications. The current stable version is 11.3.2, with the project maintaining an active and frequent release cadence focused on bug fixes and incremental feature enhancements, as evidenced by its recent rapid succession of patch releases. Key differentiators include its deep and seamless integration with Vue's reactivity system, support for both the Options API and Composition API, sophisticated pluralization rules, robust date and number formatting capabilities, and a flexible message syntax that handles interpolation, linked messages, and even HTML content. It provides various optimized build artifacts (e.g., global, esm-browser, esm-bundler, cjs, node.mjs) to cater to diverse deployment environments, from direct CDN usage in browsers to modern bundler-driven setups and server-side rendering with Node.js. It's built on the Intlify project, ensuring a consistent and performant i18n experience for the Vue ecosystem.
Common errors
-
[vue-i18n] Not found locale messages in "en"
cause The i18n instance was initialized, but the messages for the specified locale (e.g., 'en') or a configured fallback locale are either missing or were not correctly provided to `createI18n`.fixVerify that your `messages` object contains entries for all active and fallback locales, and that they are correctly structured and passed into the `createI18n` function. -
Property '$t' does not exist on type 'ComponentPublicInstance<...>' or 'Cannot read properties of undefined (reading '$t')'
cause This typically occurs when trying to access global i18n properties like `$t` (Options API) or the `t` function (Composition API via `useI18n`) without correctly installing the `i18n` instance on the Vue application or outside an appropriate component context.fixFor Options API, ensure `app.use(i18n)` is called in your main application entry point. For Composition API, ensure `const { t } = useI18n()` is called within a component's `setup` function or a setup-like context, and that `legacy: false` is set in `createI18n`. -
You must set 'legacy: false' option to use the Composition API
cause You are attempting to use Vue I18n's Composition API features (e.g., `useI18n`) with an i18n instance configured in 'legacy' mode (which is often the default or implied if not explicitly set to `false`).fixWhen initializing your i18n instance with `createI18n`, explicitly include `legacy: false` in the options object to enable Composition API support: `createI18n({ legacy: false, ... })`.
Warnings
- breaking Vue I18n v9+ is designed exclusively for Vue 3. Projects migrating from Vue 2, which typically used vue-i18n v8 or earlier, will encounter significant breaking changes requiring a full re-configuration of the internationalization setup due to API changes and the shift to Vue 3's plugin system.
- gotcha Using the runtime-only build (e.g., `vue-i18n.runtime.esm-bundler.js`) requires all locale messages to be pre-compiled. Attempting to provide string templates for messages at runtime with this build will lead to errors, as it lacks the message compiler.
- deprecated The CommonJS (CJS) builds for Node.js (e.g., `vue-i18n.cjs.js`) are considered deprecated as of v9.3+ in favor of ES Modules (ESM) builds (e.g., `vue-i18n.node.mjs`). While still functional, future versions may remove or limit support for CJS, making ESM the recommended approach for Node.js environments.
- gotcha The `esm-bundler` builds expose global feature flags like `__VUE_I18N_FULL_INSTALL__` that are intended to be replaced by bundlers. If these flags are not correctly configured during the build process (e.g., via a `DefinePlugin` in webpack), it can lead to unexpected behavior, including tree-shaking issues or incorrect feature activation.
Install
-
npm install vue-i18n -
yarn add vue-i18n -
pnpm add vue-i18n
Imports
- createI18n
const { createI18n } = require('vue-i18n')import { createI18n } from 'vue-i18n' - useI18n
import { useI18n } from 'vue-i18n' - I18nOptions
import type { I18nOptions } from 'vue-i18n' - vI18n
import { i18nDirective } from 'vue-i18n'import { vI18n } from 'vue-i18n'
Quickstart
import { createApp, ref } from 'vue'
import { createI18n, useI18n } from 'vue-i18n'
// Root Vue app component (e.g., App.vue)
const App = {
template: `
<div>
<h1>{{ t('greeting', { name: 'World' }) }}</h1>
<p>{{ t('welcome') }}</p>
<p>{{ t('pluralExample', { count: messageCount }) }}</p>
<p>{{ t('dynamicMessage', { type: 'dynamic' }) }}</p>
<button @click="changeLocale('en')">English</button>
<button @click="changeLocale('fr')">Français</button>
</div>
`,
setup() {
// Use the Composition API hook
const { t, locale } = useI18n({ useScope: 'global' }) // 'global' scope is necessary for root app messages
const messageCount = ref(1)
const changeLocale = (lang: string) => {
locale.value = lang // Reactively change the current locale
messageCount.value = lang === 'en' ? 5 : 1 // Example for pluralization demo
}
return { t, messageCount, changeLocale }
}
}
// Locale messages definitions
const messages = {
en: {
greeting: 'Hello, {name}!',
welcome: 'Welcome to our site.',
pluralExample: 'You have {count} message | You have {count} messages.',
dynamicMessage: 'This is a {type} message.'
},
fr: {
greeting: 'Bonjour, {name} !',
welcome: 'Bienvenue sur notre site.',
pluralExample: 'Vous avez {count} message | Vous avez {count} messages.',
dynamicMessage: 'Ceci est un message {type}.'
}
}
// Create the i18n instance
const i18n = createI18n({
legacy: false, // Essential for using Composition API with Vue I18n v9+
locale: 'en', // Default locale
fallbackLocale: 'en', // Fallback locale if a message is not found
messages // Provide all locale messages
})
// Create the Vue app instance
const app = createApp(App)
// Install the i18n plugin
app.use(i18n)
// Mount the app to a DOM element
app.mount('#app')