Skip to content

Docs: fix 04-linking-and-navigating.mdx locale switcher example #81753

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

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

minnnn7716
Copy link

What?

Fix locale switcher example to prevent infinite URL path accumulation when switching languages multiple times.

Why?

The current example in the documentation has a bug where repeatedly clicking language switcher buttons causes infinite accumulation of locale prefixes in the URL path. For example:

  • Initial: /about
  • Click EN: /en/about
  • Click FR: /fr/en/about
  • Click EN: /en/fr/en/about

This creates malformed URLs and poor user experience.

How?

Added a regex pattern to remove existing locale prefixes before adding new ones:

// Remove existing locale prefix if present
const pathWithoutLocale = pathname.replace(/^\/(en|fr)/, '') || '/'
const newPath = `/${locale}${pathWithoutLocale}`

The original example code has a bug where repeatedly clicking language switcher buttons causes infinite accumulation of locale prefixes in the URL path.

Example of the issue:

Initial path: /about
Click English: /en/about
Click French: /fr/en/about
Click English again: /en/fr/en/about
And so on...

Solution: Added a regex pattern to remove any existing locale prefix before adding the new one:
@ijjk ijjk added the Documentation Related to Next.js' official documentation. label Jul 17, 2025
@ijjk
Copy link
Member

ijjk commented Jul 17, 2025

Allow CI Workflow Run

  • approve CI run for commit: 624a645

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@@ -427,6 +427,8 @@ export function LocaleSwitcher() {
const pathname = usePathname()

function switchLocale(locale: string) {
// Remove existing locale prefix if present
const pathWithoutLocale = pathname.replace(/^\/(en|fr)/, '') || '/'
// e.g. '/en/about' or '/fr/contact'
const newPath = `/${locale}${pathname}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is using pathname in the newPath construction, but this will cause locale prefixes to accumulate in the URL when switching languages multiple times. To fix this issue, use the newly created pathWithoutLocale variable instead:

const newPath = `/${locale}${pathWithoutLocale}`

This ensures that any existing locale prefix is removed before adding the new one, preventing paths like /fr/en/about from occurring.

Suggested change
const newPath = `/${locale}${pathname}`
const newPath = `/${locale}${pathWithoutLocale}`

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants