Skip to content

Commit

Permalink
fix: Revert "feat: i18n.locale property changes when route changed"
Browse files Browse the repository at this point in the history
This reverts commit 2f2f284.
  • Loading branch information
paulgv committed Oct 12, 2018
1 parent fba9278 commit 9e04b00
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
6 changes: 0 additions & 6 deletions src/plugins/main.js
Expand Up @@ -56,7 +56,6 @@ export default async ({ app, route, store, req }) => {
app.i18n.routesNameSeparator = '<%= options.routesNameSeparator %>'
app.i18n.beforeLanguageSwitch = <%= options.beforeLanguageSwitch %>
app.i18n.onLanguageSwitched = <%= options.onLanguageSwitched %>
app.i18n.onRouteChanged = (to, from) => {}

if (store && store.state.localeDomains) {
app.i18n.locales.forEach(locale => {
Expand All @@ -75,7 +74,6 @@ export default async ({ app, route, store, req }) => {
}

app.i18n.locale = locale
app.i18n.nextLocale = locale

// Lazy-load translations
if (lazy) {
Expand All @@ -87,8 +85,4 @@ export default async ({ app, route, store, req }) => {
// Sync Vuex
syncVuex(locale, app.i18n.getLocaleMessage(locale))
}

if (!req) {
app.router.afterEach((to, from) => app.i18n.onRouteChanged(to, from))
}
}
18 changes: 5 additions & 13 deletions src/templates/middleware.js
Expand Up @@ -107,25 +107,17 @@ middleware['i18n'] = async ({ app, req, res, route, store, redirect, isHMR }) =>

const oldLocale = app.i18n.locale
app.i18n.beforeLanguageSwitch(oldLocale, locale)

let messages = ''
// Lazy-loading enabled
if (lazy) {
const { loadLanguageAsync } = require('./utils')
messages = await loadLanguageAsync(app.i18n, locale)
const messages = await loadLanguageAsync(app.i18n, locale)
app.i18n.locale = locale
app.i18n.onLanguageSwitched(oldLocale, locale)
syncVuex(locale, messages)
} else {
// Lazy-loading disabled
messages = app.i18n.getLocaleMessage(locale)
}

// NOTE: `app.i18n.locale` property changes when route changed.
// but, `fetch` and `asyncData` methods is called before it is changed.
// if you using this property use `app.i18n.nextLocale` instead.
app.i18n.nextLocale = locale

app.i18n.onRouteChanged = (to, from) => {
app.i18n.locale = locale
app.i18n.onLanguageSwitched(oldLocale, locale)
syncVuex(locale, messages)
syncVuex(locale, app.i18n.getLocaleMessage(locale))
}
}

0 comments on commit 9e04b00

Please sign in to comment.