@@ -20,7 +20,7 @@ import {
20
20
} from 'vue-i18n-routing'
21
21
import { joinURL , isEqual } from 'ufo'
22
22
import { isString , isFunction , isArray , isObject } from '@intlify/shared'
23
- import { navigateTo , useRoute , useState } from '#imports'
23
+ import { navigateTo , useNuxtApp , useRoute , useRuntimeConfig , useState } from '#imports'
24
24
import { nuxtI18nInternalOptions , nuxtI18nOptionsDefault , NUXT_I18N_MODULE_ID , isSSG } from '#build/i18n.options.mjs'
25
25
import {
26
26
detectBrowserLanguage ,
@@ -90,7 +90,6 @@ export async function finalizePendingLocaleChange(i18n: I18n) {
90
90
91
91
export async function loadAndSetLocale < Context extends NuxtApp = NuxtApp > (
92
92
newLocale : string ,
93
- context : Context ,
94
93
localeMessages : Record < Locale , LocaleInternalLoader [ ] > ,
95
94
i18n : I18n ,
96
95
{
@@ -106,6 +105,7 @@ export async function loadAndSetLocale<Context extends NuxtApp = NuxtApp>(
106
105
cacheMessages ?: Map < string , LocaleMessages < DefineLocaleMessage > >
107
106
} = { }
108
107
) : Promise < [ boolean , string ] > {
108
+ const nuxtApp = useNuxtApp ( )
109
109
let ret = false
110
110
const oldLocale = getLocale ( i18n )
111
111
__DEBUG__ && console . log ( 'setLocale: new -> ' , newLocale , ' old -> ' , oldLocale , ' initial -> ' , initial )
@@ -123,7 +123,7 @@ export async function loadAndSetLocale<Context extends NuxtApp = NuxtApp>(
123
123
}
124
124
125
125
// call onBeforeLanguageSwitch
126
- const localeOverride = await onBeforeLanguageSwitch ( i18n , oldLocale , newLocale , initial , context )
126
+ const localeOverride = await onBeforeLanguageSwitch ( i18n , oldLocale , newLocale , initial , nuxtApp )
127
127
const localeCodes = getLocaleCodes ( i18n )
128
128
if ( localeOverride && localeCodes && localeCodes . includes ( localeOverride ) ) {
129
129
if ( localeOverride === oldLocale ) {
@@ -162,7 +162,6 @@ type LocaleLoader = () => Locale
162
162
163
163
export function detectLocale < Context extends NuxtApp = NuxtApp > (
164
164
route : string | Route | RouteLocationNormalized | RouteLocationNormalizedLoaded ,
165
- context : any ,
166
165
routeLocaleGetter : ReturnType < typeof createLocaleFromRouteGetter > ,
167
166
nuxtI18nOptions : DeepRequired < NuxtI18nOptions < Context > > ,
168
167
vueI18nOptions : I18nOptions ,
@@ -188,7 +187,6 @@ export function detectLocale<Context extends NuxtApp = NuxtApp>(
188
187
} = nuxtI18nOptions . detectBrowserLanguage
189
188
? detectBrowserLanguage (
190
189
route ,
191
- context ,
192
190
nuxtI18nOptions ,
193
191
nuxtI18nInternalOptions ,
194
192
vueI18nOptions ,
@@ -239,7 +237,7 @@ export function detectLocale<Context extends NuxtApp = NuxtApp>(
239
237
nuxtI18nOptions . detectBrowserLanguage
240
238
)
241
239
if ( ! finalLocale && nuxtI18nOptions . detectBrowserLanguage && nuxtI18nOptions . detectBrowserLanguage . useCookie ) {
242
- finalLocale = getLocaleCookie ( context , { ...nuxtI18nOptions . detectBrowserLanguage , localeCodes } ) || ''
240
+ finalLocale = getLocaleCookie ( { ...nuxtI18nOptions . detectBrowserLanguage , localeCodes } ) || ''
243
241
}
244
242
245
243
__DEBUG__ && console . log ( 'detectLocale: finalLocale last (finalLocale, defaultLocale) -' , finalLocale , defaultLocale )
@@ -253,7 +251,6 @@ export function detectLocale<Context extends NuxtApp = NuxtApp>(
253
251
254
252
export function detectRedirect < Context extends NuxtApp = NuxtApp > ( {
255
253
route,
256
- context,
257
254
targetLocale,
258
255
routeLocaleGetter,
259
256
nuxtI18nOptions,
@@ -263,12 +260,12 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>({
263
260
to : Route | RouteLocationNormalized | RouteLocationNormalizedLoaded
264
261
from ?: Route | RouteLocationNormalized | RouteLocationNormalizedLoaded
265
262
}
266
- context : Context
267
263
targetLocale : Locale
268
264
routeLocaleGetter : ReturnType < typeof createLocaleFromRouteGetter >
269
265
nuxtI18nOptions : DeepRequired < NuxtI18nOptions < Context > >
270
266
calledWithRouting ?: boolean
271
267
} ) : string {
268
+ const nuxtApp = useNuxtApp ( )
272
269
const { strategy, differentDomains } = nuxtI18nOptions
273
270
__DEBUG__ && console . log ( 'detectRedirect: targetLocale -> ' , targetLocale )
274
271
__DEBUG__ && console . log ( 'detectRedirect: route -> ' , route )
@@ -292,7 +289,7 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>({
292
289
routeLocaleGetter ( route . to ) !== targetLocale
293
290
) {
294
291
// the current route could be 404 in which case attempt to find matching route using the full path
295
- const routePath = context . $switchLocalePath ( targetLocale ) || context . $localePath ( toFullPath , targetLocale )
292
+ const routePath = nuxtApp . $switchLocalePath ( targetLocale ) || nuxtApp . $localePath ( toFullPath , targetLocale )
296
293
__DEBUG__ && console . log ( 'detectRedirect: calculate routePath -> ' , routePath , toFullPath )
297
294
if ( isString ( routePath ) && routePath && ! isEqual ( routePath , toFullPath ) && ! routePath . startsWith ( '//' ) ) {
298
295
/**
@@ -314,9 +311,9 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>({
314
311
* let it be processed by the route of the router middleware.
315
312
*/
316
313
const switchLocalePath = useSwitchLocalePath ( {
317
- i18n : getComposer ( context . $i18n ) ,
314
+ i18n : getComposer ( nuxtApp . $i18n ) ,
318
315
route : route . to ,
319
- router : context . $router
316
+ router : nuxtApp . $router
320
317
} )
321
318
const routePath = switchLocalePath ( targetLocale )
322
319
__DEBUG__ && console . log ( 'detectRedirect: calculate domain or ssg routePath -> ' , routePath )
@@ -443,12 +440,11 @@ export function extendPrefixable(differentDomains: boolean) {
443
440
// override switch locale path intercepter, support domain
444
441
export function extendSwitchLocalePathIntercepter (
445
442
differentDomains : boolean ,
446
- normalizedLocales : LocaleObject [ ] ,
447
- nuxt : NuxtApp
443
+ normalizedLocales : LocaleObject [ ]
448
444
) : SwitchLocalePathIntercepter {
449
445
return ( path : string , locale : Locale ) : string => {
450
446
if ( differentDomains ) {
451
- const domain = getDomainFromLocale ( locale , normalizedLocales , nuxt )
447
+ const domain = getDomainFromLocale ( locale , normalizedLocales )
452
448
__DEBUG__ && console . log ( 'extendSwitchLocalePathIntercepter: domain -> ' , domain , ' path -> ' , path )
453
449
if ( domain ) {
454
450
return joinURL ( domain , path )
@@ -461,32 +457,33 @@ export function extendSwitchLocalePathIntercepter(
461
457
}
462
458
}
463
459
464
- export function extendBaseUrl < Context extends NuxtApp = NuxtApp > (
465
- baseUrl : string | BaseUrlResolveHandler < Context > ,
466
- options : Pick < Required < NuxtI18nOptions < Context > > , 'differentDomains' > & {
467
- nuxt ?: Context
460
+ export function extendBaseUrl < Context = NuxtApp > (
461
+ baseUrl : string | BaseUrlResolveHandler < NuxtApp > ,
462
+ options : Pick < Required < NuxtI18nOptions < NuxtApp > > , 'differentDomains' > & {
468
463
localeCodeLoader : Locale | LocaleLoader
469
464
normalizedLocales : LocaleObject [ ]
470
465
}
471
466
) : BaseUrlResolveHandler < Context > {
472
- return ( context : Context ) : string => {
467
+ return ( ) : string => {
468
+ const ctx = useNuxtApp ( )
469
+ const runtimeConfig = useRuntimeConfig ( )
473
470
if ( isFunction ( baseUrl ) ) {
474
- const baseUrlResult = baseUrl ( context )
471
+ const baseUrlResult = baseUrl ( ctx )
475
472
__DEBUG__ && console . log ( 'baseUrl: using localeLoader function -' , baseUrlResult )
476
473
return baseUrlResult
477
474
}
478
475
479
476
const { differentDomains, localeCodeLoader, normalizedLocales } = options
480
477
const localeCode = isFunction ( localeCodeLoader ) ? localeCodeLoader ( ) : localeCodeLoader
481
478
if ( differentDomains && localeCode ) {
482
- const domain = getDomainFromLocale ( localeCode , normalizedLocales , options . nuxt )
479
+ const domain = getDomainFromLocale ( localeCode , normalizedLocales )
483
480
if ( domain ) {
484
481
__DEBUG__ && console . log ( 'baseUrl: using differentDomains -' , domain )
485
482
return domain
486
483
}
487
484
}
488
485
489
- const config = context . $config ?. public ?. i18n as { baseUrl ?: string }
486
+ const config = runtimeConfig ?. public ?. i18n as { baseUrl ?: string }
490
487
if ( config ?. baseUrl ) {
491
488
__DEBUG__ && console . log ( 'baseUrl: using runtimeConfig -' , config . baseUrl )
492
489
return config . baseUrl
0 commit comments