|
1 | 1 | import { unref } from 'vue' |
2 | | -import { hasPages } from '#build/i18n.options.mjs' |
3 | 2 | import { addRouteMiddleware, defineNuxtPlugin, defineNuxtRouteMiddleware, useNuxtApp } from '#imports' |
4 | 3 | import { createLogger } from '#nuxt-i18n/logger' |
5 | 4 | import { makeFallbackLocaleCodes } from '../messages' |
@@ -41,26 +40,26 @@ export default defineNuxtPlugin({ |
41 | 40 | await handleRouteDetect(currentRoute.value) |
42 | 41 |
|
43 | 42 | // app has no pages - do not register route middleware |
44 | | - if (!hasPages) { |
45 | | - return |
46 | | - } |
47 | | - |
48 | | - const localeChangeMiddleware = defineNuxtRouteMiddleware(async (to, from) => { |
49 | | - __DEBUG__ && logger.log('locale-changing middleware', to, from) |
| 43 | + if (!__HAS_PAGES__) return |
50 | 44 |
|
51 | | - const locale = await nuxt.runWithContext(() => handleRouteDetect(to)) |
| 45 | + addRouteMiddleware( |
| 46 | + 'locale-changing', |
| 47 | + defineNuxtRouteMiddleware(async (to, from) => { |
| 48 | + __DEBUG__ && logger.log('locale-changing middleware', to, from) |
52 | 49 |
|
53 | | - const redirectPath = await nuxt.runWithContext(() => |
54 | | - detectRedirect({ to, from, locale, routeLocale: nuxt._vueI18n.__localeFromRoute(to) }, true) |
55 | | - ) |
| 50 | + const locale = await nuxt.runWithContext(() => handleRouteDetect(to)) |
56 | 51 |
|
57 | | - nuxt._vueI18n.__firstAccess = false |
| 52 | + const redirectPath = await nuxt.runWithContext(() => |
| 53 | + detectRedirect({ to, from, locale, routeLocale: nuxt._vueI18n.__localeFromRoute(to) }, true) |
| 54 | + ) |
58 | 55 |
|
59 | | - __DEBUG__ && logger.log('redirectPath on locale-changing middleware', redirectPath) |
| 56 | + nuxt._vueI18n.__firstAccess = false |
60 | 57 |
|
61 | | - return await nuxt.runWithContext(() => navigate({ nuxt, redirectPath, locale, route: to })) |
62 | | - }) |
| 58 | + __DEBUG__ && logger.log('redirectPath on locale-changing middleware', redirectPath) |
63 | 59 |
|
64 | | - addRouteMiddleware('locale-changing', localeChangeMiddleware, { global: true }) |
| 60 | + return await nuxt.runWithContext(() => navigate({ nuxt, redirectPath, locale, route: to })) |
| 61 | + }), |
| 62 | + { global: true } |
| 63 | + ) |
65 | 64 | } |
66 | 65 | }) |
0 commit comments