Routing Integration
Enable Locale Path Prefixes
After setting localePathRedirect: true, the plugin takes over language recognition and navigation in the URL. It does four things:
- Recognizes the locale prefix in the URL: When visiting
/zh/detail, the plugin extractszhfrom the path as the current language. - Automatically redirects paths without prefixes: When visiting
/detail, it first detects the language with the detector. If detection fails, it usesfallbackLanguage, then redirects to/en/detailor/zh/detail. - Synchronizes the URL when switching language: If the current URL is
/en/detail, callingchangeLanguage('zh')automatically changes the URL to/zh/detail, instead of only changing i18next's internal language state. - Avoids duplicate path detection: The plugin automatically removes
pathfrom the i18next detectororder, because path language recognition is already handled by the plugin itself.
Result:
Some paths, such as API routes and static assets, do not need locale prefixes. Use ignoreRedirectRoutes to skip redirects. See Locale Detection -> ignoreRedirectRoutes.
Route Configuration
After enabling localePathRedirect, add a [lang] dynamic parameter in routes to receive the locale prefix.
File-system Routes
Create a [lang] directory under routes/:
Generated route structure:
If you need to read the current language parameter in a layout or page:
Custom Routes
Custom routes are only needed when you are not using the Modern.js file-system routing system. File-system routes are recommended for most projects.
If you use a custom route file (modern.routes.ts), add the :lang parameter manually:
I18nLink Component
I18nLink is a locale-aware link component. It automatically adds the current locale prefix to the target path, so you do not need to concatenate it manually.
When the current language is en:
Notes:
I18nLink extends the Link component from @modern-js/runtime/router and supports all standard Link props such as replace, state, and className. For the full Props type, see API Reference.