@@ -2,10 +2,9 @@ import { isString } from '@intlify/shared'
22import { useCookie , useRequestHeader , useRuntimeConfig } from '#imports'
33import { localeCodes , normalizedLocales } from '#build/i18n.options.mjs'
44import { findBrowserLocale , parseAcceptLanguage } from '#i18n-kit/browser'
5- import { createLogger } from '#nuxt-i18n/logger'
65
76import type { DetectBrowserLanguageOptions , I18nPublicRuntimeConfig } from '#internal-i18n-types'
8- import type { CookieOptions , CookieRef } from 'nuxt/app'
7+ import type { CookieRef } from 'nuxt/app'
98import type { CompatRoute } from './types'
109
1110export function getCompatRoutePath ( route : string | CompatRoute ) {
@@ -25,52 +24,28 @@ export function getBrowserLocale(): string | undefined {
2524 )
2625}
2726
28- export function createI18nCookie ( ) {
29- const detect = ( useRuntimeConfig ( ) . public . i18n as I18nPublicRuntimeConfig ) . detectBrowserLanguage
30- const cookieKey = ( detect && detect . cookieKey ) || __DEFAULT_COOKIE_KEY__
27+ export function createI18nCookie ( runtimeI18n = useRuntimeConfig ( ) . public . i18n as I18nPublicRuntimeConfig ) {
28+ const { cookieCrossOrigin, cookieDomain, cookieSecure, cookieKey } = runtimeI18n . detectBrowserLanguage || { }
3129 const date = new Date ( )
32- const cookieOptions : CookieOptions < string | undefined > & { readonly : false } = {
30+ return useCookie < string | undefined > ( cookieKey || __DEFAULT_COOKIE_KEY__ , {
3331 path : '/' ,
3432 readonly : false ,
3533 expires : new Date ( date . setDate ( date . getDate ( ) + 365 ) ) ,
36- sameSite : detect && detect . cookieCrossOrigin ? 'none' : 'lax' ,
37- domain : ( detect && detect . cookieDomain ) || undefined ,
38- secure : ( detect && detect . cookieCrossOrigin ) || ( detect && detect . cookieSecure )
39- }
40- return useCookie ( cookieKey , cookieOptions )
34+ sameSite : cookieCrossOrigin ? 'none' : 'lax' ,
35+ domain : cookieDomain || undefined ,
36+ secure : cookieCrossOrigin || cookieSecure
37+ } )
4138}
4239
43- // TODO: remove side-effects
4440export function getLocaleCookie (
4541 cookieRef : CookieRef < string | undefined > ,
46- detect : false | DetectBrowserLanguageOptions ,
47- defaultLocale : string
42+ detect : false | DetectBrowserLanguageOptions
4843) : string | undefined {
49- const { useCookie, cookieKey } = detect || { }
50- const logger = /*#__PURE__*/ createLogger ( `getLocaleCookie:${ import . meta. client ? 'client' : 'server' } ` )
51- __DEBUG__ && logger . log ( { useCookie, cookieKey, localeCodes } )
52-
53- if ( ! useCookie ) {
54- return
55- }
56-
57- const localeCode : string | undefined = cookieRef . value ?? undefined
58- if ( localeCode == null ) {
59- __DEBUG__ && logger . log ( `none` )
44+ if ( ! detect || ! detect . useCookie || cookieRef . value == null ) {
6045 return
6146 }
6247
63- if ( localeCodes . includes ( localeCode ) ) {
64- __DEBUG__ && logger . log ( `locale from cookie: ` , localeCode )
65- return localeCode
66- }
67-
68- if ( defaultLocale ) {
69- __DEBUG__ && logger . log ( `unknown locale cookie (${ localeCode } ), setting to defaultLocale (${ defaultLocale } )` )
70- cookieRef . value = defaultLocale
71- return defaultLocale
48+ if ( localeCodes . includes ( cookieRef . value ) ) {
49+ return cookieRef . value
7250 }
73-
74- __DEBUG__ && logger . log ( `unknown locale cookie (${ localeCode } ), unsetting cookie` )
75- cookieRef . value = undefined
7651}
0 commit comments