Locales & Routing Mechanics
Relevant source files
The following files were used as context for generating this wiki page:
Locales and Routing Mechanics manage the transformation of standard Nuxt page definitions into localized route trees. The system localized URLs based on pre-defined strategies, ensuring deterministic route generation during the Nuxt build and extend-pages phases. Sources: packages/route-strategy/README.md:1-12, src/module.ts:550-555
The core routing logic resides in the @i18n-micro/route-strategy package. It operates independently of the Nuxt runtime and Vue Router, processing plain NuxtPage objects to maintain performance and testability. Sources: packages/route-strategy/README.md:40-50, AGENTS.md:38-40
Localization Strategies
The RouteGenerator supports four primary strategies that dictate how locale codes appear in application URLs. Sources: packages/route-strategy/README.md:54-59, src/module.ts:50-55
Strategy Definitions
| Strategy | URL Behavior | Locale Handling |
|---|---|---|
no_prefix | No locale prefix in URLs (e.g., /about). | Uses cookies or runtime logic for detection. |
prefix | All routes require a prefix (e.g., /en/about, /fr/about). | No unprefixed default route exists. |
prefix_except_default | Default locale uses no prefix; others use prefixes. | The most common strategy for SEO. |
prefix_and_default | Default locale works both with and without a prefix. | /about and /en/about coexist. |
Sources: packages/route-strategy/README.md:62-85, packages/route-strategy/src/route-generator.ts:182-192
Path Resolution Flow
The diagram below illustrates how a raw path resolves into a localized URL based on the active strategy.
The system checks for custom overrides in globalLocaleRoutes or filesLocaleRoutes before applying the default strategy logic. Sources: packages/route-strategy/src/route-generator.ts:168-202, packages/route-strategy/README.md:88-100
Route Generator Architecture
The RouteGenerator class serves as the primary engine for route localization. It orchestrates the transformation of NuxtPage arrays during the pages:resolved hook. Sources: packages/route-strategy/src/route-generator.ts:25-72, src/module.ts:550-565
Generator Components
- GeneratorContext: Maintains the state of locales, strategies, and path mappings during a single generation pass. Sources: packages/route-strategy/README.md:126-133
- LocalizedPathsMap: A normalized map of canonical paths to their per-locale variants. Sources: packages/route-strategy/src/route-generator.ts:88-90
- Strategy Handlers: Concrete implementations for each routing strategy (e.g.,
PrefixStrategy) that decide how to process individual pages. Sources: packages/route-strategy/README.md:144-155
Execution Sequence
The Nuxt module triggers the extendPages method to mutate the route tree. Sources: src/module.ts:563, packages/route-strategy/src/route-generator.ts:74-88
Path Normalization and Utilities
Localized routing relies on strict normalization to ensure path keys match correctly across different configurations. Sources: packages/route-strategy/tests/utils-and-edge.test.ts:13-25
Key Utility Functions
normalizeRouteKey: Converts Nuxt-style dynamic segments (e.g.,[id]) to Vue Router syntax (e.g.,:id). Sources: packages/route-strategy/tests/utils-and-edge.test.ts:15-22isInternalPath: Identifies paths that should not be localized, such as_nuxt,sitemap.xml, or paths matchingexcludePatterns. Sources: packages/route-strategy/tests/utils-and-edge.test.ts:98-115, src/module.ts:400-410buildFullPath: Constructs the final URL string by combining the locale code and the normalized path. Sources: packages/route-strategy/tests/utils-and-edge.test.ts:81-90
Handling Internal Paths
The generator automatically excludes specific system paths from localization. Sources: packages/route-strategy/tests/utils-and-edge.test.ts:125-135
| Path Pattern | Example | Localization |
|---|---|---|
| Nuxt Internals | /_nuxt/entry.js | Excluded |
| Well-known APIs | /api/health | Excluded |
| Static Files | /robots.txt | Excluded |
| Custom Excludes | /admin/** | Excluded (Configurable) |
Sources: packages/route-strategy/tests/utils-and-edge.test.ts:100-145, src/module.ts:192-195
Custom Route Mapping
Users define custom localized paths through two primary mechanisms: globalLocaleRoutes and filesLocaleRoutes. Sources: packages/route-strategy/README.md:102-113, src/module.ts:340-360
Mapping Sources
- Global Configuration: Defined in
nuxt.config.ts. It maps canonical paths to locale-specific strings. Sources: packages/route-strategy/README.md:104-108 - In-File Metadata: Extracted from components using
$defineI18nRoute. The module scans these files during build time. Sources: src/module.ts:345-365, packages/route-strategy/README.md:110-113
Resolution Priority
When resolving a path, the RouteGenerator prioritizes global configuration over in-file metadata. Sources: packages/route-strategy/tests/advanced.test.ts:13-20
Prerendering and Data Routes
For static site generation (SSG) and performance optimization, the system generates localized "data routes" for translation payloads. Sources: src/module.ts:540-548, packages/route-strategy/src/route-generator.ts:213-225
Data Route Structure
Translation files are served via a predictable API structure: /{apiBaseUrl}/{pageName}/{locale}/data.json. Sources: packages/route-strategy/src/route-generator.ts:218-223, src/module.ts:515-520
Prerender Hook Logic
During the prerender:routes hook, the module adds localized versions of all crawlable routes to the Nitro prerender queue. If a strategy uses prefixes, the module removes the unprefixed root routes from the queue to prevent duplicate content or incorrect redirects. Sources: src/module.ts:750-785, src/module.ts:805-815
Routing mechanics in nuxt-i18n-micro ensure that localization remains a build-time transformation where possible, reducing runtime overhead. By combining strict strategy implementation with flexible custom path mapping, the system provides SEO-friendly URLs while maintaining high performance. Sources: README.md:95-105, packages/route-strategy/README.md:165-175