37
loading...
This website collects cookies to deliver better user experience
Internationalization is the design and development of a product, application or document content that enables easy localization for target audiences that vary in culture, region, or language.
(Source: W3.org)
yarn add vue-i18n --save
Create a folder called i18n
in the src
directory of your project.
Within the i18n
folder, create an index.js
file and a folder called messages
.
index.js
file will look like:import Vue from 'vue'
import VueI18n from 'vue-i18n'
import messages from './messages'
Vue.use(VueI18n)
export default new VueI18n({
locale: 'en',
messages
})
messages
folder, create three folders named en
, es
and pt-BR
and, inside each one of them (that represents a different language), create two files: one named menu.js
and another named index.js
.// /src/i18n/messages/en/menu.js
export default {
home: 'Home',
about: 'About',
contact: 'Contact'
}
// /src/i18n/messages/en/index.js
import menu from './menu'
export default {
menu
}
// /src/i18n/messages/es/menu.js
export default {
home: 'Pagina de Inicio',
about: 'Acerca de',
contact: 'Contacto'
}
// /src/i18n/messages/es/index.js
import menu from './menu'
export default {
menu
}
// /src/i18n/messages/pt-BR/menu.js
export default {
home: 'Início',
about: 'Sobre',
contact: 'Contato'
}
// /src/i18n/messages/pt-BR/index.js
import menu from './menu'
export default {
menu
}
messages
object to organise them better. Like this:export default {
links: {
home: {
label: 'Home',
help: 'Click here to go to home page'
},
about: {
label: 'About',
help: 'Click here to know more about us'
},
contact: {
label: 'Contact',
help: 'Click here to go to reach out to us'
}
}
}
messages
folder, create an index.js
file like this:
import en from './en'
import es from './es'
import ptBR from './pt-BR'
export default {
en,
es,
'pt-BR': ptBR
}
main.js
file, import the i18n
package and set it to the Vue instance:
import App from './App.vue'
import i18n from './i18n'
new Vue({
i18n,
render: h => h(App)
}).$mount('#app')
Vuex Persist
in your project, run the following command in the root directory of your project:
yarn add vuex-persistedstate --save
Create a file named index.js
and a folder named store
inside the src
directory.
Create folder named modules
within store
.
Create a file named locale.js
inside the modules
folder and implement it like this:
// src/store/modules/locale.js
export default {
namespaced: true,
state: {
locale: 'en'
},
mutations: {
setLocale(state, locale) {
state.locale = locale
}
}
}
store/index.js
will look like:// src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import locale from './modules/locale'
const persistedState = createPersistedState({
key: 'vuejs-vue-i18n',
paths: ['locale']
})
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
locale
},
plugins: [persistedState]
})
LanguageSwitch.vue
component. It will hold all of the available languages and it will use Vuex helpers functions to update the current language:
<!-- src/components/LanguageSwitcher.vue (template) -->
<template>
<b-nav-item-dropdown :text="currentLocale" right>
<b-dropdown-item
:disabled="isCurrentLocale('en')"
@click="onSetLocale('en')"
>
EN
</b-dropdown-item>
<b-dropdown-item
:disabled="isCurrentLocale('es')"
@click="onSetLocale('es')"
>
ES
</b-dropdown-item>
<b-dropdown-item
:disabled="isCurrentLocale('pt-BR')"
@click="onSetLocale('pt-BR')"
>
PT-BR</b-dropdown-item
>
</b-nav-item-dropdown>
</template>
// src/components/LanguageSwitcher.vue (script)
<script>
import { mapState, mapMutations } from 'vuex'
export default {
name: 'LanguageSwitcher',
computed: {
...mapState('locale', ['locale']),
currentLocale() {
return this.locale.toUpperCase()
}
},
created() {
this.$i18n.locale = this.locale
},
methods: {
...mapMutations('locale', ['setLocale']),
onSetLocale(locale) {
this.$i18n.locale = locale
this.setLocale(locale)
},
isCurrentLocale(locale) {
return this.locale === locale
}
}
}
</script>
Navbar.vue
component to put the recently created LanguageSwitcher
one.
Notice that, in this case, we use the global $t
helper provided by the Vue i18n plugin to get the proper translation we need to display according to the current locale.
It's very simple to use, all you need to do, it call it passing a translation key as argument.{{ $t('translation.key') }}
script
section of your components, if needed:{
computed: {
label() {
// For this work, you have to create a file named `common.js` inside the folder of each language and export it in its respective `index.js` file.
return this.$t('common.label')
}
},
methods: {
getTitle() {
return this.$t('common.title')
}
}
}
Navbar.vue
component will look like:<!-- src/components/Navbar.vue (template) -->
<template>
<b-navbar toggleable="lg" type="dark" variant="primary">
<b-navbar-brand>VueJS vue-i18n</b-navbar-brand>
<b-navbar-toggle target="nav-collapse" />
<b-collapse id="nav-collapse" is-nav>
<b-navbar-nav>
<b-nav-item :to="{ name: 'Home' }">
{{ $t('navbar.home') }}
</b-nav-item>
<b-nav-item :to="{ name: 'About' }">
{{ $t('navbar.about') }}
</b-nav-item>
<b-nav-item :to="{ name: 'Contact' }">
{{ $t('navbar.contact') }}
</b-nav-item>
</b-navbar-nav>
<b-navbar-nav class="ml-auto">
<LanguageSwitcher />
</b-navbar-nav>
</b-collapse>
</b-navbar>
</template>
<!-- src/components/Navbar.vue (script) -->
<script>
import LanguageSwitcher from '@/components/LanguageSwitcher/LanguageSwitcher'
export default {
name: 'Navbar',
components: {
LanguageSwitcher
}
}
</script>
Layout.vue
component that will hold the Navbar
and will be used within the Views we are going to create next:
<!-- src/views/Layout.vue (template) -->
<template>
<b-row>
<b-col>
<Navbar />
<b-container>
<slot />
</b-container>
</b-col>
</b-row>
</template>
// src/views/Layout.vue (script)
<script>
import Navbar from '@/components/Navbar'
export default {
name: 'Layout',
components: {
Navbar
}
}
</script>
Layout
component into them and add them to the router/index.js
file.
In this section, the most important thing is to use the global $t
helper, provided by the Vue i18n package <template>
<Layout>
<h1>{{ $t('navbar.home') }}</h1>
</Layout>
</template>
<script>
import Layout from './Layout'
export default {
name: 'HomeView',
components: {
Layout
}
}
</script>
<template>
<Layout>
<h1>{{ $t('navbar.about') }}</h1>
</Layout>
</template>
<script>
import Layout from './Layout'
export default {
name: 'AboutView',
components: {
Layout
}
}
</script>
<template>
<Layout>
<h1>{{ $t('navbar.contact') }}</h1>
</Layout>
</template>
<script>
import Layout from './Layout'
export default {
name: 'ContactView',
components: {
Layout
}
}
</script>
<template>
<Layout>
<h1>{{ $t('navbar.links.contact.label') }}</h1>
</Layout>
</template>
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: () => import('@/views/Home')
},
{
path: '/about',
name: 'About',
component: () => import('@/views/About')
},
{
path: '/contact',
name: 'Contact',
component: () => import('@/views/Contact')
}
]
const router = new VueRouter({
mode: 'history',
routes
})
export default router