From 893d275a78db49ed876a85f276bb6df214c75fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sun, 21 May 2017 21:01:08 +0200 Subject: [PATCH] Update i18n example for 1.0 --- examples/i18n/middleware/i18n.js | 4 ++-- examples/i18n/plugins/i18n.js | 26 ++++++++++++++++---------- examples/i18n/store/index.js | 4 ++-- 3 files changed, 20 insertions(+), 14 deletions(-) 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) {