From 5a2c4b282542fc180dd26035e34df9b764691295 Mon Sep 17 00:00:00 2001 From: Rostyslav Melnychuk Date: Wed, 21 Sep 2022 00:45:19 +0300 Subject: [PATCH 1/2] Updated example for i18n middleware Added support for search params and NEXT_LOCALE cookie if set --- docs/advanced-features/i18n-routing.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/advanced-features/i18n-routing.md b/docs/advanced-features/i18n-routing.md index 76837abe110c5..9f4403134985e 100644 --- a/docs/advanced-features/i18n-routing.md +++ b/docs/advanced-features/i18n-routing.md @@ -183,7 +183,9 @@ export async function middleware(req: NextRequest) { } if (req.nextUrl.locale === 'default') { - return NextResponse.redirect(new URL(`/en${req.nextUrl.pathname}`, req.url)) + const locale = req.cookies.get('NEXT_LOCALE') || 'en' + + return NextResponse.redirect(new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url)) } } ``` From 72ac93d5ea600d1564867134778e30b4ca263cfa Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 20 Sep 2022 15:43:29 -0700 Subject: [PATCH 2/2] lint fix --- docs/advanced-features/i18n-routing.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/advanced-features/i18n-routing.md b/docs/advanced-features/i18n-routing.md index 9f4403134985e..c4ed2f39e0067 100644 --- a/docs/advanced-features/i18n-routing.md +++ b/docs/advanced-features/i18n-routing.md @@ -185,7 +185,9 @@ export async function middleware(req: NextRequest) { if (req.nextUrl.locale === 'default') { const locale = req.cookies.get('NEXT_LOCALE') || 'en' - return NextResponse.redirect(new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url)) + return NextResponse.redirect( + new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url) + ) } } ```