Update i18n example for 1.0

This commit is contained in:
Sébastien Chopin 2017-05-21 21:01:08 +02:00
parent 78cd3bae86
commit 893d275a78
3 changed files with 20 additions and 14 deletions

View File

@ -1,11 +1,11 @@
export default function ({ i18n, store, route, params, error, redirect }) {
export default function ({ app, store, route, params, error, redirect }) {
const locale = params.lang || 'en'
if (store.state.locales.indexOf(locale) === -1) {
return error({ message: 'This page could not be found.', statusCode: 404 })
}
// Set locale
store.commit('SET_LANG', locale)
i18n.locale = store.state.locale
app.i18n.locale = store.state.locale
// If route is /en/... -> redirect to /...
if (locale === 'en' && route.fullPath.indexOf('/en') === 0) {
redirect(route.fullPath.replace(/^\/en/, '/'))

View File

@ -1,16 +1,22 @@
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import store from '~store'
Vue.use(VueI18n)
const i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
'en': require('~/locales/en.json'),
'fr': require('~/locales/fr.json')
export default ({ isClient, app, store, route, error, redirect }) => {
console.log(route.path)
if (isClient && route.path === '/fr/about') {
return redirect('/about')
}
})
export default i18n
console.log(error)
// Set i18n instance on app
// This way we can use it in middleware and pages asyncData/fetch
app.i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
'en': require('~/locales/en.json'),
'fr': require('~/locales/fr.json')
}
})
}

View File

@ -1,7 +1,7 @@
export const state = {
export const state = () => ({
locales: ['en', 'fr'],
locale: 'en'
}
})
export const mutations = {
SET_LANG (state, locale) {