Skip to content

Commit 5e954bd

Browse files
committed
i18n
1 parent d445767 commit 5e954bd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

document/middleware.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,35 @@ export default function middleware(request: NextRequest) {
5757
}
5858
}
5959

60-
// Continue with i18n middleware
60+
// Check if URL has a language prefix
61+
const hasLangPrefix = i18n.languages.some(
62+
(l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`
63+
);
64+
65+
if (hasLangPrefix) {
66+
// Save user's language preference to cookie when visiting a localized URL
67+
const currentCookie = request.cookies.get('FD_LOCALE')?.value;
68+
if (currentCookie !== lang) {
69+
// @ts-expect-error - Fumadocs middleware signature mismatch
70+
const response = i18nMiddleware(request) ?? NextResponse.next();
71+
// @ts-ignore
72+
response.cookies.set('FD_LOCALE', lang, {
73+
path: '/',
74+
maxAge: 60 * 60 * 24 * 365
75+
});
76+
return response;
77+
}
78+
} else {
79+
// No language prefix — check cookie for user's language preference
80+
const cookieLocale = request.cookies.get('FD_LOCALE')?.value;
81+
if (cookieLocale && i18n.languages.includes(cookieLocale)) {
82+
const newUrl = new URL(`/${cookieLocale}${pathname}`, request.url);
83+
newUrl.search = request.nextUrl.search;
84+
return NextResponse.redirect(newUrl);
85+
}
86+
}
87+
88+
// Continue with i18n middleware (falls back to Accept-Language detection)
6189
// @ts-expect-error - Fumadocs middleware signature mismatch
6290
return i18nMiddleware(request);
6391
}

0 commit comments

Comments
 (0)