Course contentsShow
← Web Development
Lesson 2539 of 2,98245. InternationalizationFree lesson

Path-Based Locale Routing

Implement locale prefixes in URL paths like /en/about and /fr/about with proper routing logic.

Path-Based Locale Routing

What you'll learn: How to implement URL paths with locale prefixes (like /en/about and /fr/about) and handle the routing logic behind them.

What Is Path-Based Locale Routing?

Path-based locale routing places the language or locale identifier at the beginning of your URL path. When a user visits /en/about, they see the English version, while /fr/about shows French. This approach makes the language immediately visible in the URL and creates distinct URLs for each language version—beneficial for SEO and bookmarking.

Core Implementation Patterns

The routing logic typically involves stripping the locale prefix from the path before matching it to your actual route handlers. Here's the flow:

  1. Parse the incoming URL path
  2. Extract the locale prefix (e.g., en, fr, de)
  3. Validate it against supported locales
  4. Route the remainder to your application logic
// URL: /fr/products/123
const pathParts = url.pathname.split('/').filter(Boolean);
const locale = pathParts[0]; // 'fr'
const actualPath = '/' + pathParts.slice(1).join('/'); // '/products/123'

if (supportedLocales.includes(locale)) {
  // Set locale context and route to /products/123
  setLocale(locale);
  routeToPath(actualPath);
}

Server vs Client Handling

On the server, middleware extracts the locale before routing. On the client, your router configuration needs locale-aware route matching that treats /en/dashboard and /fr/dashboard as the same logical route with different locale contexts.

Key Takeaway: Path-based locale routing embeds language identifiers in URLs as path prefixes, requiring your routing system to extract, validate, and strip locale segments before matching routes—creating clean, SEO-friendly, language-specific URLs.