Skip to content

Commit 7340248

Browse files
committed
Use a standard parser for accept-language
1 parent 4c45f50 commit 7340248

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"@mui/material": "latest",
99
"@mui/material-nextjs": "latest",
1010
"@mui/x-tree-view": "^7.23.2",
11+
"accept-language-parser": "^1.5.0",
1112
"cookies-next": "^5.0.2",
1213
"fast-xml-parser": "^4.5.1",
1314
"he": "^1.2.0",
1415
"lru-cache": "^11.0.2",
16+
"negotiator": "^1.0.0",
1517
"next": "^15.0.0",
1618
"next-intl": "^3.26.3",
1719
"react": "^19.0.0",
@@ -21,6 +23,7 @@
2123
},
2224
"devDependencies": {
2325
"@types/he": "^1.2.3",
26+
"@types/negotiator": "^0.6.3",
2427
"@types/node": "latest",
2528
"@types/react": "^19.0.0",
2629
"@types/react-dom": "^19.0.0",

src/i18n/request.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getRequestConfig } from "next-intl/server";
22
import { cookies, headers } from "next/headers";
33
import { defaultLocale, Locale, locales } from "./config";
4+
import Negotiator from "negotiator";
45
import { match } from "@formatjs/intl-localematcher";
56
import { addError } from "@/lib/errorLog";
67

@@ -9,19 +10,15 @@ async function getHeaderLocale(): Promise<string | undefined> {
910
if (!accepted_str) {
1011
return;
1112
}
12-
1313
console.log(`Accepted languages: "${accepted_str}"`);
1414

1515
try {
16-
const accepted = accepted_str.split(',').map((str) => {
17-
const [locale] = str.split(';q=');
18-
return locale;
19-
//return [locale, parseFloat(q || '1')];
20-
});
16+
const headers = { 'accept-language': accepted_str };
17+
const accepted = new Negotiator({ headers }).languages();
2118

2219
console.log("parsed", accepted);
2320

24-
const matched = match(accepted, locales, 'en');
21+
const matched = match(accepted, locales, defaultLocale);
2522
console.log("matched", matched);
2623
return matched;
2724
} catch (err:unknown) {

0 commit comments

Comments
 (0)