Locale Detection
The plugin supports multiple language detection methods, which can be combined to meet different business requirements.
Detection Methods
1. URL Path Detection (localePathRedirect)
When localePathRedirect is set to true, the plugin will detect the language from the URL path.
Examples:
/zh/about→ Detected language:zh/en/about→ Detected language:en/about→ If there's no language prefix, will redirect to the default language path
Configuration:
Route Configuration (Convention-based Routing):
When using convention-based routing, you need to create a [lang] directory under the routes/ directory to represent the language parameter:
routes/[lang]/layout.tsx:
routes/[lang]/page.tsx:
routes/[lang]/about/page.tsx:
If using custom routing (modern.routes.ts), you need to add :lang dynamic parameter in the route configuration. Convention-based routing will automatically generate corresponding routes based on the file structure.
2. i18next Language Detector
When i18nextDetector is set to true, the i18next language detector will be enabled, supporting language detection from the following locations:
- Cookie: Read language settings from cookies
- LocalStorage: Read from browser LocalStorage
- Query Parameters: Read from URL query parameters (e.g.,
?lng=en) - Request Headers: Read from HTTP request headers (e.g.,
Accept-Language) - HTML Tag: Read from the
langattribute of HTML tags - Subdomain: Read from subdomain (e.g.,
en.example.com)
Configuration:
3. Custom Detection Configuration
You can customize detection behavior through the detection option:
Detection Priority
The plugin's language detection follows the following priority order (from highest to lowest):
- SSR Data (highest priority): Read language from
window._SSR_DATAset during server-side rendering, applicable to both SSR and CSR projects - Path Detection: If
localePathRedirectistrue, detect language prefix from URL path - i18next Detector: Execute detection according to the order configured in
detection.order(Cookie, LocalStorage, query parameters, request headers, etc.) - User Configured Language: Use the language configured in
initOptions.lng - Fallback Language: Use
fallbackLanguageas the final fallback
SSR data detection has the highest priority to ensure the client uses the language detected during server-side rendering, avoiding language flickering issues caused by client-side re-detection.
Example:
Detection Options
order (Detection Order)
Specifies the order of language detection, optional values:
path: Detect from URL pathquerystring: Detect from query parameterscookie: Detect from cookieslocalStorage: Detect from LocalStoragesessionStorage: Detect from SessionStoragenavigator: Detect from browser language settingshtmlTag: Detect from HTML tagsheader: Detect from HTTP request headerssubdomain: Detect from subdomain
path detection requires localePathRedirect to be true. localStorage, sessionStorage, navigator, and htmlTag are only available in browser environments.
caches (Cache Method)
Specifies where the detected language should be cached, optional values:
false: No caching['cookie']: Cache to Cookie['localStorage']: Cache to LocalStorage (browser only)['cookie', 'localStorage']: Cache to both Cookie and LocalStorage
lookupQuerystring, lookupCookie, lookupLocalStorage, lookupSession, lookupHeader
Specifies the key name used when reading language from query parameters, cookies, LocalStorage, SessionStorage, or request headers:
lookupQuerystring: Default'lng', e.g.,?lng=enlookupCookie: Default'i18next'lookupLocalStorage: Default'i18nextLng'(browser only)lookupSession: SessionStorage key name (browser only)lookupHeader: Default'accept-language'
lookupFromPathIndex
Specifies which position in the URL path to start detecting language (when 'path' is included in order):
lookupFromPathIndex: Path segment index, defaults to0(first path segment)
Example:
cookieMinutes, cookieExpirationDate
Controls Cookie expiration time:
cookieMinutes: Cookie expiration time (minutes), default525600(1 year)cookieExpirationDate: Cookie expiration date (Date object), takes precedence overcookieMinutes
Example:
ignoreRedirectRoutes
Specifies which routes should ignore automatic language redirection. This is very useful for API routes, static resources, and other paths that don't need language prefixes.
Configuration:
Matching Rules:
- String array: Supports exact match (
'/api') and prefix match ('/api'will match/apiand/api/users) - Function: Receives pathname (with language prefix removed), returns
trueto indicate ignoring redirection
Example: