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,10 +1,17 @@
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import store from '~store'
Vue.use(VueI18n)
const i18n = new VueI18n({
export default ({ isClient, app, store, route, error, redirect }) => {
console.log(route.path)
if (isClient && route.path === '/fr/about') {
return redirect('/about')
}
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: {
@ -12,5 +19,4 @@ const i18n = new VueI18n({
'fr': require('~/locales/fr.json')
}
})
export default i18n
}

View File

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