i18next-vue

5.4.0 · active · verified Sun Apr 19

i18next-vue is the official integration library for using the i18next internationalization framework with Vue 3 applications. It simplifies the process of translating content by providing a Vue plugin (`I18NextVue`) and a Composition API hook (`useTranslation`), along with global properties like `$t` and `$i18next` for easy access within components. The current stable version is 5.4.0, with regular updates that include performance improvements, bug fixes, and minor features, alongside occasional breaking changes for major version bumps (e.g., v3, v4, v5) to align with i18next or Vue ecosystem changes. It differentiates itself by offering robust TypeScript support and seamless reactivity for language switching and dynamic content, leveraging Vue's application context for broader compatibility, including Pinia stores.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize i18next, integrate i18next-vue as a plugin, and use both the Composition API's `useTranslation` hook and the global `$t` function for translations. It also shows language switching and rich text interpolation with the `<i18next>` component.

import { createApp, defineComponent } from 'vue';
import i18next from 'i18next';
import I18NextVue from 'i18next-vue';
import { useTranslation } from 'i18next-vue';

// 1. Initialize i18next (before mounting Vue app)
i18next.init({
  lng: 'en',
  fallbackLng: 'en',
  resources: {
    en: {
      translation: {
        'welcome': 'Welcome to our app!',
        'greeting': 'Hello, {{name}}!',
        'dynamic_link': 'Visit the {{link}} page.',
        'home': 'Home'
      }
    },
    de: {
      translation: {
        'welcome': 'Willkommen in unserer App!',
        'greeting': 'Hallo, {{name}}!',
        'dynamic_link': 'Besuchen Sie die {{link}} Seite.',
        'home': 'Startseite'
      }
    }
  }
});

// 2. Define your root Vue component
const App = defineComponent({
  setup() {
    const { i18next, t } = useTranslation();
    const changeLanguage = (lang: string) => {
      i18next.changeLanguage(lang);
    };
    return { i18next, t, changeLanguage };
  },
  template: `
    <div>
      <h1>{{ t('welcome') }}</h1>
      <p>{{ t('greeting', { name: 'World' }) }}</p>
      <p>Current language: {{ i18next.language }}</p>
      <button @click="changeLanguage('en')">English</button>
      <button @click="changeLanguage('de')">Deutsch</button>
      
      <h2>Using $t in template:</h2>
      <p>{{ $t('greeting', { name: 'Vue User' }) }}</p>

      <h2>Using the <i18next> component for rich text:</h2>
      <i18next :translation="t('dynamic_link')">
        <template #link>
          <a href="/home">{{ t('home') }}</a>
        </template>
      </i18next>
    </div>
  `
});

// 3. Create and mount the Vue app with the i18next-vue plugin
const app = createApp(App);
app.use(I18NextVue, { i18next });
app.mount('#app');

view raw JSON →