24
loading...
This website collects cookies to deliver better user experience
Internationalization means supporting your app or site for multiple languages via translations.
npm install vue-i18n
yarn add vue-i18n
main.js
file you'll need to add the following, which will import the plugin across your whole application so you don't need to import the plugin on every page of your application.import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
// You can set a default language like this:
const i18n = new VueI18n({
locale: "English",
fallbackLocale: "English"
});
new Vue({
//...
i18n,
render: h => h(App)
}).$mount("#app");
Hello.vue
:<template>
<h1>Hello!</h1>
</template>
Hello
with {{ $t("hello") }}
- which will look for the specified "hello"
"variable" for the selected language, and return it to the user. We specify our i18n variables inside of an i18n
tag that sits at the same level as our template
, script
and style
tags.<template>
<h1>{{ $t("hello") }}!</h1>
</template>
<i18n>
{
"English": {
"hello": "Hello",
},
"Español": {
"hello": "Hola",
},
"Deutsche": {
"hello": "Hallo",
},
"Français": {
"hello": "Bonjour",
}
}
</i18n>
// LangChanger.vue
<template>
<div>
<select
name="LanguageSelection"
v-model="$root.$i18n.locale"
>
<option
v-for="(lang, i) in langs"
:key="`Lang${i}`"
:value="lang"
>
{{ lang }}
</option>
</select>
</div>
</template>
<script>
export default {
name: "locale-changer",
data() {
// 👇 These are the languages that are available for users to select from.
// They need to match the values in the <i18n> block in the previous code sample.
return { langs: [
"English",
"Español",
"Deutsche",
"Français"
]};
}
};
</script>