mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Merge pull request #2920 from phivk/patch-1
fetch default locale from i18n.fallbackLocale in i18n example
This commit is contained in:
commit
6494873f70
@ -1,16 +1,22 @@
|
|||||||
export default function ({ isHMR, app, store, route, params, error, redirect }) {
|
export default function ({ isHMR, app, store, route, params, error, redirect }) {
|
||||||
|
const defaultLocale = app.i18n.fallbackLocale;
|
||||||
// If middleware is called from hot module replacement, ignore it
|
// If middleware is called from hot module replacement, ignore it
|
||||||
if (isHMR) return
|
if (isHMR) return
|
||||||
// Get locale from params
|
// Get locale from params
|
||||||
const locale = params.lang || 'en'
|
const locale = params.lang || defaultLocale
|
||||||
if (store.state.locales.indexOf(locale) === -1) {
|
if (store.state.locales.indexOf(locale) === -1) {
|
||||||
return error({ message: 'This page could not be found.', statusCode: 404 })
|
return error({ message: 'This page could not be found.', statusCode: 404 })
|
||||||
}
|
}
|
||||||
// Set locale
|
// Set locale
|
||||||
store.commit('SET_LANG', locale)
|
store.commit('SET_LANG', locale)
|
||||||
app.i18n.locale = store.state.locale
|
app.i18n.locale = store.state.locale
|
||||||
// If route is /en/... -> redirect to /...
|
// If route is /<defaultLocale>/... -> redirect to /...
|
||||||
if (locale === 'en' && route.fullPath.indexOf('/en') === 0) {
|
if (locale === defaultLocale && route.fullPath.indexOf('/'+defaultLocale) === 0) {
|
||||||
return redirect(route.fullPath.replace(/^\/en/, '/'))
|
|
||||||
|
var toReplace = '^/'+defaultLocale
|
||||||
|
var re = new RegExp(toReplace);
|
||||||
|
return redirect(
|
||||||
|
route.fullPath.replace(re, '/')
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user