Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(docs): update i18n-routing.md #33123

Merged
merged 3 commits into from Feb 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/advanced-features/i18n-routing.md
Expand Up @@ -170,14 +170,21 @@ import { NextRequest, NextResponse } from 'next/server'

const PUBLIC_FILE = /\.(.*)$/

const stripDefaultLocale = (str: string): string => {
const stripped = str.replace('/default', '')
return stripped
}

export function middleware(request: NextRequest) {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes('/api/') &&
request.nextUrl.locale === 'default'

return shouldHandleLocale
? NextResponse.redirect(`/en${request.nextUrl.href}`)
? NextResponse.redirect(
`/en${stripDefaultLocale(request.nextUrl.pathname)}`
)
: undefined
}
```
Expand Down