feat: implement path-based i18n routing in Next.js - #809
Merged
Merged
Conversation
- Add locale detection/rewrite in middleware: bare paths (e.g. /dashboard) are redirected to locale-prefixed equivalents (/en/dashboard) using cookie or Accept-Language header for detection. - Locale-prefixed paths are rewritten internally to strip the prefix while preserving the user-visible URL for SEO indexability. - Set NEXT_LOCALE cookie in middleware so server components and client i18n can both resolve the active locale. - Update LanguageSelector to navigate to locale-prefixed URLs on change. - Update root layout to set <html lang> from the NEXT_LOCALE cookie. - Update detectPreferredLocale to check the cookie before localStorage. - Expand middleware tests to cover locale routing scenarios. Refs: Betta-Pay#753
|
Someone is attempting to deploy a commit to the therealjhay's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@brodapeethar Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements path-based i18n routing for the Next.js App Router, enabling SEO-indexable localized URLs like and .
Closes #753
Changes
middleware.ts
Accept-Languageheader for detection.NEXT_LOCALEcookie on every response so server components and client i18n can both resolve the active locale.lib/i18n/locales.ts
LOCALE_COOKIEconstant (NEXT_LOCALE).lib/i18n/config.ts
detectPreferredLocaleto check theNEXT_LOCALEcookie first (set by middleware), thenlocalStorage, thenAccept-Language.components/i18n/LanguageSelector.tsx
/fr/dashboard) viarouter.push(), keeping the URL in sync with the selected language.app/layout.tsx
NEXT_LOCALEcookie server-side to set<html lang="...">correctly during SSR.Tests
next/navigationhooks.Architecture Notes
This project uses Next.js 14 App Router, where the
i18nkey innext.config.jsis not supported. Instead, locale routing is handled entirely via middleware rewrites — no filesystem restructuring required. The existing client-side i18next setup (bundled dictionaries, localStorage persistence) is preserved and augmented with the middleware-set cookie for SSR compatibility.How it works
GET /fr/dashboard→ middleware detectsfrprefix, strips it, rewrites to/dashboardinternally, setsNEXT_LOCALE=frcookie.GET /dashboard→ middleware detects no prefix, redirects to/en/dashboard(using cookie/Accept-Language for locale detection)./fr/dashboard(locale-prefixed URL).Testing Notes
/fr/dashboard— app boots in French with URL preserved./dashboard(no prefix) — redirects to/en/dashboard.