File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 11import type { Config } from 'next-i18n-router/dist/types'
2+ import type { NextRequest } from 'next/server'
23
34export const LOCALE_EN_KEY = 'en'
45export const LOCALE_ES_KEY = 'es'
@@ -17,6 +18,25 @@ const i18nConfig: Config = {
1718 maxAge : 31536000 ,
1819 secure : true , // Required when using SameSite=None
1920 } ,
21+ localeDetector : ( request : NextRequest , config : Config ) : string => {
22+ // Check for NEXT_LOCALE cookie
23+ const nextLocale = request . cookies . get ( NEXT_LOCALE_COOKIE_NAME ) ?. value
24+ if ( nextLocale && config . locales . includes ( nextLocale ) ) {
25+ return nextLocale
26+ }
27+
28+ // Check browser language
29+ const acceptLanguage = request . headers . get ( 'accept-language' )
30+ if ( acceptLanguage ) {
31+ const browserLang = acceptLanguage . split ( ',' ) [ 0 ] . split ( '-' ) [ 0 ]
32+ if ( config . locales . includes ( browserLang ) ) {
33+ return browserLang
34+ }
35+ }
36+
37+ // Return default locale
38+ return config . defaultLocale
39+ } ,
2040}
2141
2242export default i18nConfig
You can’t perform that action at this time.
0 commit comments