diff --git a/examples/i18n/middleware/i18n.js b/examples/i18n/middleware/i18n.js index 10e437bcdb..b04fb950a3 100644 --- a/examples/i18n/middleware/i18n.js +++ b/examples/i18n/middleware/i18n.js @@ -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/, '/')) diff --git a/examples/i18n/plugins/i18n.js b/examples/i18n/plugins/i18n.js index 0bf5d875b6..02e20adf74 100644 --- a/examples/i18n/plugins/i18n.js +++ b/examples/i18n/plugins/i18n.js @@ -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') + } + }) +} diff --git a/examples/i18n/store/index.js b/examples/i18n/store/index.js index 0582e7bdb9..0c12e69efa 100644 --- a/examples/i18n/store/index.js +++ b/examples/i18n/store/index.js @@ -1,7 +1,7 @@ -export const state = { +export const state = () => ({ locales: ['en', 'fr'], locale: 'en' -} +}) export const mutations = { SET_LANG (state, locale) {