@@ -25,6 +25,7 @@ import { initCommonComposableOptions, type CommonComposableOptions } from './uti
2525import type { Locale } from 'vue-i18n'
2626import type { DetectBrowserLanguageOptions , LocaleObject } from '#build/i18n.options.mjs'
2727import type { RouteLocationNormalized , RouteLocationNormalizedLoaded } from 'vue-router'
28+ import type { CookieRef } from 'nuxt/app'
2829
2930export function formatMessage ( message : string ) {
3031 return NUXT_I18N_MODULE_ID + ' ' + message
@@ -93,7 +94,25 @@ export function getBrowserLocale(): string | undefined {
9394 return ret
9495}
9596
96- export function getLocaleCookie ( ) : string | undefined {
97+ export function getI18nCookie ( ) {
98+ const detect = nuxtI18nOptions . detectBrowserLanguage
99+ const cookieKey = ( detect && detect . cookieKey ) || nuxtI18nOptionsDefault . detectBrowserLanguage . cookieKey
100+ const date = new Date ( )
101+ const cookieOptions : Record < string , any > = {
102+ expires : new Date ( date . setDate ( date . getDate ( ) + 365 ) ) ,
103+ path : '/' ,
104+ sameSite : detect && detect . cookieCrossOrigin ? 'none' : 'lax' ,
105+ secure : ( detect && detect . cookieCrossOrigin ) || ( detect && detect . cookieSecure )
106+ }
107+
108+ if ( detect && detect . cookieDomain ) {
109+ cookieOptions . domain = detect . cookieDomain
110+ }
111+
112+ return useNuxtCookie < string | undefined > ( cookieKey , cookieOptions )
113+ }
114+
115+ export function getLocaleCookie ( cookieRef : CookieRef < string | undefined > ) : string | undefined {
97116 const detect = nuxtI18nOptions . detectBrowserLanguage
98117
99118 __DEBUG__ &&
@@ -107,37 +126,22 @@ export function getLocaleCookie(): string | undefined {
107126 return
108127 }
109128
110- const localeCookie = useNuxtCookie ( detect . cookieKey )
111- const localeCode : string | undefined = localeCookie . value ?? undefined
129+ const localeCode : string | undefined = cookieRef . value ?? undefined
112130 __DEBUG__ && console . log ( `getLocaleCookie cookie (${ process . client ? 'client' : 'server' } ) -` , localeCode )
113131
114132 if ( localeCode && localeCodes . includes ( localeCode ) ) {
115133 return localeCode
116134 }
117135}
118136
119- export function setLocaleCookie ( locale : string ) {
120- const { useCookie, cookieKey, cookieDomain, cookieSecure, cookieCrossOrigin } =
121- nuxtI18nOptions . detectBrowserLanguage || nuxtI18nOptionsDefault . detectBrowserLanguage
137+ export function setLocaleCookie ( cookieRef : CookieRef < string | undefined > , locale : string ) {
138+ const { useCookie } = nuxtI18nOptions . detectBrowserLanguage || nuxtI18nOptionsDefault . detectBrowserLanguage
122139
123140 if ( ! useCookie ) {
124141 return
125142 }
126143
127- const date = new Date ( )
128- const cookieOptions : Record < string , any > = {
129- expires : new Date ( date . setDate ( date . getDate ( ) + 365 ) ) ,
130- path : '/' ,
131- sameSite : cookieCrossOrigin ? 'none' : 'lax' ,
132- secure : cookieCrossOrigin || cookieSecure
133- }
134-
135- if ( cookieDomain ) {
136- cookieOptions . domain = cookieDomain
137- }
138-
139- const localeCookie = useNuxtCookie ( cookieKey , cookieOptions )
140- localeCookie . value = locale
144+ cookieRef . value = locale
141145}
142146
143147export type DetectBrowserLanguageNotDetectReason =
@@ -160,6 +164,7 @@ export type DetectLocaleContext = {
160164 ssg : DetectLocaleForSSGStatus
161165 callType : DetectLocaleCallType
162166 firstAccess : boolean
167+ localeCookie : CookieRef < string | undefined >
163168}
164169
165170export const DefaultDetectBrowserLanguageFromResult : DetectBrowserLanguageFromResult = {
@@ -176,7 +181,7 @@ export function detectBrowserLanguage(
176181 locale : Locale = ''
177182) : DetectBrowserLanguageFromResult {
178183 const { strategy } = nuxtI18nOptions
179- const { ssg, callType, firstAccess } = detectLocaleContext
184+ const { ssg, callType, firstAccess, localeCookie } = detectLocaleContext
180185 __DEBUG__ && console . log ( 'detectBrowserLanguage: (ssg, callType, firstAccess) - ' , ssg , callType , firstAccess )
181186
182187 // browser detection is ignored if it's a nuxt generate.
@@ -223,7 +228,7 @@ export function detectBrowserLanguage(
223228
224229 // get preferred language from cookie if present and enabled
225230 if ( useCookie ) {
226- matchedLocale = cookieLocale = getLocaleCookie ( )
231+ matchedLocale = cookieLocale = localeCookie . value
227232 localeFrom = 'cookie'
228233 __DEBUG__ && console . log ( 'detectBrowserLanguage: cookieLocale' , cookieLocale )
229234 }
0 commit comments