Vue CLI i18n Plugin
The `vue-cli-plugin-i18n` package is a Vue CLI plugin designed to integrate `vue-i18n` into new or existing Vue projects, streamlining the setup of internationalization. As of version 2.3.2, its latest release in late 2021, the plugin provides scaffolding for `vue-i18n`'s basic configuration, support for locale messages within Single File Components via `vue-i18n-loader`, and experimental reporting features for missing or unused locale keys. It supports both Vue 2 and Vue 3 projects, offering distinct options like `enableInSFC` for Vue 2 (related to `vue-i18n` v8.x) and `enableLegacy` or `runtimeOnly` for Vue 3 (related to `vue-i18n` v9+). Additionally, it includes a `enableBridge` option for Vue 2 projects to facilitate migration to `vue-i18n@v9.x`. The plugin is maintained by the Intlify organization, known for the `vue-i18n` library itself, ensuring close compatibility and up-to-date best practices.
Common errors
-
Error: Failed to resolve entry for package "vue-i18n". The package may have an incorrect main/module field.
cause Incorrect Vue I18n build chosen for the project context, often related to runtime-only vs. full build.fixEnsure `runtimeOnly: false` (or omit) if you need the full build, or `runtimeOnly: true` if you intend to use the runtime-only version, configured in `vue.config.js` under `pluginOptions.i18n`. -
[vue-i18n] Not found i18n instance. Please make sure that you install the plugin in the Vue app with `app.use(i18n)`.
cause The `i18n` instance was not correctly provided to the Vue application.fixVerify that `app.use(i18n)` is called in your `main.js` (or `main.ts`) file after creating the Vue app, and that `i18n` is correctly imported from the generated `src/i18n.js`. -
[vue-i18n] Fallback locale `en` is not registered. Please register it before use.
cause The fallback locale specified in `vue.config.js` or `.env` is not present in your locale message files.fixEnsure that a locale messages file (e.g., `en.json`) exists in your `localeDir` and contains valid internationalization messages for the `fallbackLocale`.
Warnings
- breaking When migrating a Vue 2 project to Vue 3, `vue-cli-plugin-i18n` v2.x requires configuring `enableLegacy: true` in `vue.config.js` to use the legacy Vue I18n API or refactoring to the Composition API and `createI18n`.
- gotcha Using the `enableBridge` option in Vue 2 to prepare for Vue I18n v9.x migration requires manual installation of `@vue/composition-api` if you intend to use the Composition API.
- gotcha The `enableInSFC` option, which allows locale messages in Single File Components, is only supported for Vue 2 projects using Vue I18n v8.x.
- gotcha The `vue-cli-service i18n:report` command has limitations and cannot detect missing or unused keys from locale messages defined within i18n custom blocks in Single File Components.
- deprecated The `vue-cli-plugin-i18n` is tied to Vue CLI, which is moving towards maintenance mode as newer build tools like Vite gain prominence. While still functional, new Vue projects may favor alternative i18n integration methods.
Install
-
npm install vue-cli-plugin-i18n -
yarn add vue-cli-plugin-i18n -
pnpm add vue-cli-plugin-i18n
Imports
- createI18n
import VueI18n from 'vue-i18n'
import { createI18n } from 'vue-i18n' - i18n instance
import { i18n } from './i18n'import i18n from './i18n'
- useI18n
this.$i18n
import { useI18n } from 'vue-i18n'
Quickstart
vue create my-vue-app
cd my-vue-app
vue add i18n
// After installation, a src/i18n.js (or .ts) file will be generated.
// You can then use it in your main.js (or main.ts):
// src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import i18n from './i18n' // Auto-generated by the plugin
createApp(App).use(i18n).mount('#app')
// To report missing/unused keys (experimental):
// vue-cli-service i18n:report