i18next integration for Vue
npm install i18next-vueThis library is a simple wrapper for i18next, simplifying its use in Vue 3.
There is also a Vue 2 version of this package.
In the documentation, you can find information on how to upgrade from @panter/vue-i18next, from i18next-vue 3.x or earlier versions.
``bash`
npm install i18next-vue
`typescript
import Vue from "vue";
import i18next from "i18next";
import I18NextVue from "i18next-vue";
import App from "./App.vue";
/const i18nInitialized = /i18next.init({ ... });
createApp(App).use(I18NextVue, { i18next }).mount('#app');
// to wait for loading the translations first, do this instead:
// i18nInitialized.then(() => createApp(App).use(I18NextVue, { i18next }).mount('#app'));
`
Use the $t translation function, which works like (and uses) the versatile t function from i18next.
There is a full tutorial for setting up i18next-vue. You can check out the live demo version version of it, too.
To learn about more options, check out the full documentation.
Given the i18next translations
`js`
i18next.init({
// ...
resources: {
en: {
// language
translation: {
// the default namespace
insurance: "Insurance",
},
},
de: {
// language
translation: {
// the default namespace
insurance: "Versicherung",
},
},
},
});
You can use
` {{ $t("insurance") }}vue`
A test in {{ $i18next.language }}
$t() works both in Options API and Composition API components.
Using the useTranslation() composition function you can access the i18next instance and t() in the setup part, and e.g. get a t() functions for a specific namespace.
`vue
inline: {{ t("insurance") }} inline with $t: {{ $t("insurance") }} computed: {{ term }}
A test in {{ i18next.language }}
`
i18next-vue provides a translation component
In this example {faq-link} will be replaced by the faq-link slot, i.e. by the router link. You can move {faq-link} around in the translation, so it makes sense for the target language.
`js`
i18next.init({
// ...
resources: {
en: {
translation: {
"message": "Open the {faq-link} page."
"faq": "Frequently Asked Questions"
}
},
fr: {
// ...
}
}
})
`vue`
{{ $t("faq") }}
#### Custom slot values
Custom slot values may be useful when the default braces ({ and }) are wrongly treated by the
Locize service or don't satisfy other needs.
Use custom values for recognizing start and end of the insertion points of the /TranslationComponent
inside the localization term:
`js
// main.js
i18next.init({
// ...
resources: {
en: {
translation: {
"message": "Open the
"faq": "FAQ"
}
},
de: {
// ...
}
}
})
app.use(I18NextVue, {
i18next,
slotStart: '
slotEnd: '
});
`
`vue``
{{ $t("faq") }}
- Node.js >= v20