@@ -4,17 +4,24 @@ import { match as matchLocale } from "@formatjs/intl-localematcher";
44import Negotiator from "negotiator" ;
55
66function getLocale ( request : NextRequest ) : string | undefined {
7- // Negotiator expects plain object so we need to transform headers
8- const negotiatorHeaders : Record < string , string > = { } ;
9- request . headers . forEach ( ( value , key ) => ( negotiatorHeaders [ key ] = value ) ) ;
7+ try {
8+ // Negotiator expects plain object so we need to transform headers
9+ const negotiatorHeaders : Record < string , string > = { } ;
10+ request . headers . forEach ( ( value , key ) => ( negotiatorHeaders [ key ] = value ) ) ;
1011
11- // Use negotiator and intl-localematcher to get best locale
12- const languages = new Negotiator ( { headers : negotiatorHeaders } ) . languages ( ) ;
13- const locales = [ ...i18n . locales ] ;
14- const locale = matchLocale ( languages , locales , i18n . defaultLocale ) ;
15- return ( [ ...i18n . locales ] as string [ ] ) . includes ( locale )
16- ? locale
17- : i18n . defaultLocale ;
12+ // Use negotiator and intl-localematcher to get best locale
13+ const languages = new Negotiator ( {
14+ headers : negotiatorHeaders ,
15+ } ) . languages ( ) ;
16+ const locales = [ ...i18n . locales ] ;
17+ const locale = matchLocale ( languages , locales , i18n . defaultLocale ) ;
18+ return ( [ ...i18n . locales ] as string [ ] ) . includes ( locale )
19+ ? locale
20+ : i18n . defaultLocale ;
21+ } catch ( e ) {
22+ console . error ( e ) ;
23+ return i18n . defaultLocale ;
24+ }
1825}
1926
2027export async function middleware ( request : NextRequest ) {
0 commit comments