
Locale Routing: URL Architecture for Web i18n
Where the locale lives in your URLs, and how it reaches every component, link, and cached page. The URL scheme, the fan-out, and the decisions that are hard to reverse.
TL;DR: Locale routing decides where the locale lives in a site's URLs and how it travels from the request to every component, link, and cached page. This article covers the path-prefix, subdomain, and domain choice from the engineering seat; resolving the locale once and scoping it everywhere; locale-aware link generation; the build and cache fan-out of N locales times M pages; and the default-prefix and localized-slug decisions that are expensive to reverse.
The locale is part of the address
The principle this article builds on: every localized page has its own stable, shareable URL. The English pricing page and the German pricing page are two resources, and each gets an address.
The alternative, one URL whose body changes based on a cookie or the Accept-Language header, looks tidy and fails in three places at once. Sharing breaks, because a link copied by a German reader opens in English for the colleague who receives it. Caching breaks, because a CDN that keys on the URL serves whichever language it cached first to everyone. And crawling breaks, because a search engine requesting the URL sees one language and has no address for the others.
This article starts after a locale has been chosen. How new visitors and crawlers are suggested a locale, and what the language switcher looks like, is the insight locale-detection literature's territory. How alternate-language URLs are declared to search engines is the hreflang literature's. Here the question is code architecture: once the locale is in the URL, what does the routing layer do with it?
It has three jobs:
- Resolve it: parse the locale from the URL and validate it against the supported list.
- Scope it: make the resolved locale available to every component, data fetch, and message lookup in that request.
- Preserve it: make sure every link the page generates stays in that locale unless the user deliberately switches.
The decisions that follow are the URL scheme, default-locale handling, and slug localization, each with rendering and caching consequences.
Path prefix, subdomain, or domain — as an engineering decision
There are three common places to put the locale. Google's documentation on multi-regional sites lays out the landscape; the search-equity differences between them are the international-SEO literature's axis and are not re-argued here. From the engineering seat, they differ like this:
| Engineering property | Path prefix (example.com/de/) | Subdomain (de.example.com) | Separate domain (example.de) |
|---|---|---|---|
| Codebase and deploy | One app, one deploy | One app routed by host, or several | Often several apps or a multi-tenant host router |
| Local development | Works on localhost as-is | Needs host mapping or wildcard local DNS, unless tested on `*.localhost` names, which RFC 6761 says resolvers should map to loopback | Needs host mapping per domain |
| Cookies and certificates | One origin, shared cookies and one certificate | Cookies need a parent-domain scope; a wildcard or multi-name certificate | Separate cookies and sessions per domain; certificates per domain or one multi-domain certificate |
| Static generation fan-out | Locales as a route parameter in one build | Per-host builds or host-aware routing | Per-domain builds and deploy targets, or one build where the framework supports domain-aware routing |
| CDN cache keying | Distinct paths give distinct cache entries | Distinct only if the CDN keys on the host (Cloudflare's default key does; CloudFront's does not, as of Nov 2026) or each subdomain has its own CDN configuration | Separate cache per domain only if the CDN keys on the host or each domain has its own CDN configuration |
Path prefix is the default for a single application. One origin, one certificate, one session, and the locale is simply the first route segment. Framework support exists but takes different forms (as of Nov 2026): Astro and the Next.js Pages Router have built-in i18n routing, the Next.js App Router documents a [lang] route segment paired with a locale redirect in Proxy, SvelteKit documents an optional [[lang]] route parameter, and Nuxt adds locale prefixes through the routing strategies of its i18n module; several of these also document locales served from separate domains. Its honest cost is that every route becomes locale-parameterized and the locale-resolving middleware runs on every page request.
Subdomains and country domains are usually forced by something outside engineering. A separate market operation with its own team, an in-country hosting requirement, or a brand that is registered per country. The insight localization literature owns those drivers. The engineering consequence is multi-site plumbing: host-based routing, cross-domain sessions and sign-in, and per-host configuration.
Whichever scheme is chosen, it is expensive to reverse after launch. Every inbound link, bookmark, analytics report, and cached entry keys on it, and a scheme change is a migration project with redirects for every page. Decide it deliberately, once.
Threading the locale through the app
Parse once, at the boundary. The locale segment is validated against the supported list at the routing edge, in middleware or the route matcher, before any page code runs. Invalid tags need a decided policy rather than whatever happens by accident: return a 404 for a segment that is not a locale at all, or redirect a recognizable but unsupported tag such as a regional variant to its supported base. Normalize case and form at the same point, so the rest of the app only ever sees canonical BCP 47 tags.
// middleware: resolve once, then everything downstream reads the result
const SUPPORTED = ["en", "de", "fr", "ja", "pt-BR"] as const;
type Locale = (typeof SUPPORTED)[number];
export function resolveLocale(pathname: string): Locale | null {
const segment = pathname.split("/")[1] ?? "";
const canonical = SUPPORTED.find((l) => l.toLowerCase() === segment.toLowerCase());
return canonical ?? null; // null: the app returns 404, a decided policy
}
// route tree
// /[locale]/ home
// /[locale]/pricing pricing
// /[locale]/blog/[slug] articleTwo details keep the resolver from becoming its own source of bugs. Exclude static assets, API routes, and well-known files such as robots.txt and the favicon from locale matching, or the middleware will try to resolve a locale from /images/logo.svg and redirect requests that should never be localized. And keep the supported list in one exported constant that the resolver, the link helper, the sitemap generator, and the language switcher all import, so adding a locale is a one-line change rather than a search across the codebase.
Scope it without prop-drilling. The resolved locale becomes request-scoped context that pages, components, data fetches, and the message layer all read from one place. Passing it by hand through twelve component layers invites the one component that forgot it, and re-deriving it in a component from the pathname invites two components that disagree. This is this topic's one-computation-authority rule applied to routing: one resolution per request, read everywhere.
Generate links through a helper. Every internal link goes through a locale-aware helper that prefixes the current locale, or a different locale when the link is an explicit switch. One hardcoded href="/pricing" in a footer is enough to drop a Japanese reader into the English site, and they will not notice until the next page is in the wrong language. Lint rules that flag raw internal hrefs outside the helper catch this class of leak in review.
export function localizedHref(locale: Locale, path: string): string {
return `/${locale}${path.startsWith("/") ? path : `/${path}`}`;
}
// localizedHref(current, "/pricing") -> "/de/pricing"
// localizedHref("fr", "/pricing") -> "/fr/pricing" (explicit switch)Set the document language from the resolved locale. The root element's lang attribute comes from the same resolved value: <html lang="de">. It is one line, and every other element on the page inherits it. As the W3C article on why to use the language attribute explains, language information assists speech synthesizers and Braille translators, CSS automatic hyphenation is only required for text with a declared language and an appropriate hyphenation resource, and browsers can, and do, use it to select language-appropriate fonts. Routing owns it because routing is where the locale is known.
Static generation, caching, and the fan-out
Locale routing multiplies the page count. With N locales and M pages, a site has N times M routes, and full pre-rendering fans out every build. Ten locales and two thousand product pages is twenty thousand pages to render on every deploy.
The working alternatives trade build time against first-hit latency:
- Pre-render everything. Fastest possible first hit for every page in every locale, and build times that grow with both dimensions. Reasonable for small sites.
- Pre-render primary locales and high-traffic pages, render the rest on demand and cache. The build stays bounded; the first visitor to a long-tail page in a secondary locale waits for one render, and everyone after them gets the cached result.
- Render at request time behind a CDN. No fan-out at build, with cache rules doing the work that pre-rendering would. Correctness depends entirely on cache keying.
Cache keying follows the URL scheme. With the locale in the path, every locale is a distinct cache entry automatically. With the locale in the host, confirm that the CDN includes the host in its cache key: Cloudflare's default cache key does, while CloudFront's default key uses the distribution's domain name and the URL path rather than the viewer's Host header (as of Nov 2026). The danger is any response that still varies on something the cache key does not include. If a page reads a cookie or Accept-Language to decide anything language-related, it must declare that variation with a Vary header and the CDN must be configured to honor it (Cloudflare, for example, ignores Vary values other than Accept-Encoding by default, as of Nov 2026), or the CDN will serve one visitor's language to the next. The safest arrangement is that a localized URL's response depends only on the URL.
Keep redirects at the edge honest. Structural redirects, such as legacy unprefixed URLs moving under a locale prefix, are permanent. A root-path redirect that suggests a locale should never override a URL the user explicitly requested, and should never trap crawlers in a redirect based on their headers. The deeper redirect policy belongs to the locale-detection literature.
Decide what an untranslated page does. When /ja/whitepaper exists in English only, the options are a 404, the source-language page served with a notice, or a redirect to the source locale. Each is defensible for different content types: a missing legal page may need a notice, a missing blog post may simply not exist in that locale. The routing layer wires in whatever policy is decided; the fallback-chain semantics themselves are the locale-fallback article's.
Default locale and localized slugs
Should the default locale carry a prefix? Both answers are defensible. An unprefixed default, /pricing for English and /de/pricing for German, reads cleaner and preserves legacy URLs from a site that was single-language first. An always-prefixed scheme, /en/pricing alongside /de/pricing, is symmetric: every route has the same shape, the resolver has no special case, and there is no ambiguity about whether /pricing means English or means "no locale chosen yet." For a site with existing English URLs and inbound links, unprefixed default is usually the lower-cost choice. For a new build, always-prefixed is usually simpler to maintain. Pick once; mixing the two later means redirecting one half of the site.
Should slugs be translated? /de/preise instead of /de/pricing reads better to German users and aligns with how they search. The cost is a slug-mapping table: every link helper, sitemap generator, and language switcher must translate a page's slug when it changes locale, and every translated slug is a URL that must never change without a redirect. The mapping becomes localized content in its own right, and where it lives is the content-modeling article's decision. A reasonable default is to localize slugs for marketing and content pages where readability and search alignment matter, and keep stable identifiers for application routes such as account settings, where nobody reads or shares the URL.
The mapping also has consumers outside routing. The table that says /en/pricing and /de/preise are the same page is exactly what a language switcher needs to send a user to the equivalent page, and exactly what alternate-URL declarations need to tell search engines the pages are translations. It is produced by the routing layer and consumed by the switcher UX and the hreflang declarations, both of which live in the literatures this article defers to.
To restate the boundaries: detection signals and switcher design belong to insight locale-detection, alternate-URL declarations to insight hreflang, and what happens when a locale or a string is unavailable to the locale-fallback article. Routing's job is narrower and foundational: put the locale in the address, resolve it once, carry it everywhere, and never lose it in a link.