Switching Language Mid-Product Is a State Reset Problem
The short answer: Changing locale is not just changing a label — it changes the resource identity. On a product page, the correct target is a different localized slug, resolved from the backend's translation mapping before navigation. A robust switcher resolves the slug, navigates to that canonical URL, and resets state tied to the old locale. Guessable URL transformations fail for irregular translations, and inconsistent cookies make the next request disagree with the URL.
A user viewing a product switched from English to German. The app swapped the locale prefix in the URL and navigated. The page 404'd — the German slug was not a translation of the English words. It was a different identifier entirely.
That bug taught the team to treat language switching as routing, not decoration.
1. Locale changes identity
On a localized catalog, a product is not one resource with several labels. It is several resources linked by a translation group. Each has its own slug, and the mapping lives in the CMS.
Therefore, "switch language" really means "find the equivalent resource in another locale and navigate to it." Anything less is a guess.
2. Resolve, don't transform
String substitution works only when translations are mechanical. They rarely are. The reliable path is a dedicated endpoint:
GET /api/translated-product-slug?slug=<current>&lang=<target>
→ { slug: "<exact translated slug>" }The endpoint queries the CMS translation mapping — by translation group or included ID — and returns the authoritative slug. The switcher navigates to the resolved URL.
3. Keep the cookie consistent
Localized apps often store the chosen locale in a cookie that middleware reads. If the URL says /de/... but the cookie still says en, middleware may redirect or render the wrong language. When switching:
- Navigate to the resolved URL.
- Update the locale cookie to match.
- Ensure middleware treats the URL as authoritative when a prefix is present.
Consistency here prevents the maddening "switch language, get bounced back" bug.
4. Reset locale-dependent state
Some state is tied to the old locale:
- Filter selections whose term IDs are locale-specific.
- Breadcrumb trails built from translated category names.
- Forms whose validation messages are cached in the old language.
Carry over only what maps cleanly. Otherwise reset it on switch. A fresh, correct page beats a preserved, wrong one.
5. Apply the pattern everywhere
Product pages are the most visible case, but the same rule applies to categories, blog posts and legal pages. Build one switcher abstraction backed by slug-resolution endpoints for each entity type, rather than reimplementing the logic per page.
6. Defense in depth
| Dimension | URL substitution | Resolved navigation |
|---|---|---|
| Irregular slugs | Breaks | Works |
| Translation source | Guess | Backend mapping |
| Cookie | Drifts | Kept in sync |
| Stale state | Carried over | Reset when needed |
How to switch language correctly
- Resolve the target slug via an authoritative endpoint.
- Navigate to the resolved canonical URL.
- Update the locale cookie to match.
- Reset state tied to the old locale.
- Reuse the pattern across all localized entity types.
Frequently asked questions
Can I precompute all slug translations client-side? You can, but it duplicates CMS data and goes stale. An endpoint is always current.
What if no translation exists? Fall back to a documented default — the original locale or a language selector page — rather than a 404.
Is the cookie or the URL authoritative? The URL with an explicit locale prefix should win; the cookie handles the no-prefix case.
Should I preserve the shopping cart? Yes — the cart is not locale-specific. Preserve it; reset only locale-bound UI state.
Key takeaways
- Locale changes resource identity.
- Resolve translated slugs from the backend.
- Navigate to the canonical resolved URL.
- Keep cookies consistent with the URL.
- Reset state that is locale-dependent.
Summary
A language switcher is a router with a translation lookup, not a string replacer. Resolve the real target, navigate cleanly, and reset what no longer applies.
References
- Next.js documentation — internationalization routing.
- WPML REST API documentation.
- MDN Web Docs —
navigator.language.