Skip to content

Commit 427d33e

Browse files
Copilothuangyiirene
andcommitted
refactor: address code review feedback
- Remove unused @formatjs/intl-localematcher dependency - Extract SUPPORTED_LANGUAGES constant to reduce type assertion duplication - Use Object.fromEntries for more concise header conversion - Improve code readability and efficiency Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 04d3893 commit 427d33e

3 files changed

Lines changed: 8 additions & 26 deletions

File tree

apps/docs/middleware.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { i18n } from '@/lib/i18n';
44

55
const LOCALE_COOKIE = 'FD_LOCALE';
66

7+
/**
8+
* Helper to check if a language is supported
9+
*/
10+
const SUPPORTED_LANGUAGES = i18n.languages as readonly string[];
11+
712
/**
813
* Language code mapping
914
* Maps browser language codes to our supported language codes
@@ -39,16 +44,12 @@ function normalizeLanguage(lang: string): string {
3944
function getPreferredLanguage(request: NextRequest): string {
4045
// Check cookie first
4146
const cookieLocale = request.cookies.get(LOCALE_COOKIE)?.value;
42-
if (cookieLocale && (i18n.languages as readonly string[]).includes(cookieLocale)) {
47+
if (cookieLocale && SUPPORTED_LANGUAGES.includes(cookieLocale)) {
4348
return cookieLocale;
4449
}
4550

4651
// Then check Accept-Language header
47-
const negotiatorHeaders: Record<string, string> = {};
48-
request.headers.forEach((value, key) => {
49-
negotiatorHeaders[key] = value;
50-
});
51-
52+
const negotiatorHeaders = Object.fromEntries(request.headers.entries());
5253
const negotiator = new Negotiator({ headers: negotiatorHeaders });
5354
const browserLanguages = negotiator.languages();
5455

@@ -57,7 +58,7 @@ function getPreferredLanguage(request: NextRequest): string {
5758

5859
// Find the first match
5960
for (const lang of normalizedLanguages) {
60-
if ((i18n.languages as readonly string[]).includes(lang)) {
61+
if (SUPPORTED_LANGUAGES.includes(lang)) {
6162
return lang;
6263
}
6364
}

apps/docs/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"server-only": "^0.0.1"
2222
},
2323
"devDependencies": {
24-
"@formatjs/intl-localematcher": "^0.8.0",
2524
"@tailwindcss/postcss": "^4.1.18",
2625
"@tailwindcss/typography": "^0.5.19",
2726
"@types/negotiator": "^0.6.4",

pnpm-lock.yaml

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)