Localization configuration and locale resolution
Localization configuration defines the languages and regional variants an application can serve. Shared rules select one configured locale identity from request, browser, device, route, and cookie preferences and pass it to translation and formatting runtimes. The identity stays stable across routing and local loaders while service and formatting boundaries use the required canonical form. See Localization runtime and shared domain, Translation runtime, and Message formatting and ICU processing for related runtime behavior.
Configuration model
The configuration model gives each runtime one set of supported locales and one fallback rule. Framework setup supplies this model to request and client contexts so later work uses the same locale policy.
Locale settings
I18nConfig is the shared locale configuration. It defines the defaultLocale, the locales list, and optional customMapping data. The default locale is included in the supported set, duplicate entries are removed, and the library default is used when no default is supplied. Locale negotiation returns the configured spelling, so application-facing values keep their configured identity even when matching uses an equivalent standardized form. getDefaultLocale() and getLocales() expose these values to application code.
The model also carries project and runtime settings for translation services. Those settings control service use, but locale selection still ends with a configured locale or the default locale.
References: packages/…/I18nConfig.ts• L25-37 packages/…/I18nConfig.ts• L51-85 packages/…/I18nConfig.ts• L205-229
Package boundaries
The shared configuration contract is published by gt-i18n. The Node.js package, gt-node, builds on that package for server-side localization context, so both environments use the same locale settings rather than separate configuration models.
The package metadata keeps the shared i18n surface and the Node adapter as separate entry points. gt-i18n is currently version 1.0.40 and uses workspace dependencies for its formatting and shared locale rules. Its latest changelog entry updates the generaltranslation dependency to 9.5.9. gt-node is currently version 1.0.33 and uses gt-i18n as its shared server-side configuration boundary. These package metadata updates do not change the locale contract.
References: packages/…/package.json• L1-6 packages/…/package.json• L54-118 packages/…/package.json• L1-7 packages/…/package.json• L58-92 packages/…/CHANGELOG.md• L3-9 packages/…/CHANGELOG.md• L3-9
Supported-locale data
The @generaltranslation/supported-locales package provides the approved base languages and regional variants used by locale matching. A locale is first checked for validity and then matched against this data. Exact variants take priority, while a language or compatible minimized form can provide a fallback. The current list includes Greek for Greece as el-GR, English for Mexico as en-MX, Belarusian as be, Irish as ga, and Latin-script Serbian as sr-Latn.
The package exposes a sorted list for consumers that need to display or compare the supported set. It also maps valid inputs such as ar-Arab-OM to the approved ar-OM entry. Latin-script Serbian remains distinct from the broader sr entry, so sr-Latn resolves to itself. The package is published as a small runtime module and depends on the shared generaltranslation locale rules. Its current 2.1.41 release updates that dependency to generaltranslation@9.5.5 and adds the Belarusian, Irish, and Latin-script Serbian entries while retaining earlier corrections such as el-GR and en-MX.
The package contract includes both validity and resolution. Its checks require every listed locale to be valid and require each listed locale to resolve to another listed locale. They also cover language-only and script or region variants such as eo-US, ar-Arab-OM, en-Latn-MX, and sr-Latn-RS.
References: packages/…/supportedLocales.ts• L1-196 packages/…/index.ts• L8-80 packages/…/package.json• L1-22 packages/…/package.json• L40-58 packages/…/CHANGELOG.md• L1-10 packages/…/supportedLocales.test.ts• L5-53
Custom locale mapping
Custom mapping lets an application use an application-specific locale name while pointing to a valid language tag. The mapped tag is the value used for matching and formatting, while the configured alias remains the application-facing value.
Validation checks mapping targets when General Translation services are enabled. An invalid target produces a diagnostic error instead of adding an unusable locale to the supported set.
References: packages/…/resolveCanonicalLocale.ts• L1-17 packages/…/resolveAliasLocale.ts• L1-19 packages/…/validation.ts• L6-37
Framework configuration handoff
Framework setup connects application settings to runtime locale selection. Next.js combines plugin input, project configuration, environment values, and defaults, then validates and passes client-safe locale settings to the runtime.
This handoff must keep the supported list, default locale, custom mapping, and cookie names in sync. If routing and localization use different values, a URL or request can look valid to one system and unsupported to the other.
References: packages/…/config.ts• L313-347 packages/…/config.ts• L469-526 packages/…/config.ts• L656-685 packages/…/shared.ts• L11-36
Locale resolution
Locale resolution turns ordered locale candidates into one supported locale. A candidate is a locale string from a route, request, cookie, browser, device, or application callback. The resolver keeps candidate order and uses the configured default when no candidate matches.
Candidate sources
Each runtime gives its most deliberate input the first chance to win. The exact order depends on the boundary, but common sources are route locale, locale cookie, application callback, request header, and browser or device preferences.
The request helper parses Accept-Language into quality-ordered candidates. It ignores wildcards and invalid quality values. React request parsing and browser state check the locale cookie before language preferences, so an explicit user choice remains stable across visits.
The main selection path is shown below.
flowchart TD
A[Route or request input] --> B[Locale candidates]
C[Locale cookie] --> B
D[Language or device preferences] --> B
B --> E[Supported-locale resolver]
E -->|match| F[Configured locale identity]
E -->|no match| G[Configured default locale]
F --> H[Request or browser context]
G --> H
H --> I[Translation and formatting]
References: packages/…/request.ts• L3-27 packages/…/parseLocale.ts• L6-32 packages/…/readBrowserLocale.ts• L3-16 packages/…/getLocale.ts• L14-21
Supported-locale matching
The resolver checks candidates against the approved locale list. It first seeks an exact configured identity or equivalent standardized match. If that fails, it can match the same language through a region, script, or minimized form. The result is the configured identity rather than a newly normalized spelling. Invalid candidates and languages with no approved match are skipped.
determineSupportedLocale() reports whether a candidate matches. resolveSupportedLocale() adds the configured default when it does not. resolveLocale() is strict and throws for an invalid or unsupported supplied locale. Use fallback resolution for user preference data and strict resolution for values that must be valid. Equivalent spellings can resolve to distinct configured aliases, so exact configured identity remains meaningful when more than one alias represents the same language.
References: packages/…/determineLocale.ts• L57-71 packages/…/determineLocale.ts• L79-117 packages/…/I18nConfig.ts• L99-137 packages/…/index.ts• L21-68 packages/…/CHANGELOG.md• L21-27
Request and route precedence
Server boundaries can place route or framework input before normal preference detection. Next.js uses its selected route locale when that value is supported and falls back to the configured default for an invalid value. Its localized paths and route overrides preserve configured locale identities. Its older detector also supports a configured locale header, locale cookie, and Accept-Language.
TanStack Start checks the locale in the path when locale routing is active. It then checks the locale cookie and language header. The selected configured identity is written back to the cookie so later requests have a stable preference. Unsupported preferences are resolved before route decisions that depend on the default locale.
References: packages/…/parseLocale.ts• L22-50 packages/…/parseLocale.ts• L56-100 packages/…/requestConditions.ts• L23-57 packages/…/CHANGELOG.md• L27-29
Localization context
The selected locale must reach every operation in the current request or render. The repository carries this state in a condition store, with fixed stores for one render and writable stores for interactive client state.
Condition store state
A condition store carries the active locale, an optional region, and the enableI18n flag. A read-only store resolves its starting locale against the configured set and keeps it fixed. A writable store can change the locale or region, but each locale change uses the same configuration.
Helpers such as getLocale(), getRegion(), getLocales(), and getDefaultLocale() provide the common read path. Translation code can use the current condition store or configuration without receiving locale data through every call.
References: packages/…/ReadonlyConditionStore.ts• L5-35 packages/…/WritableConditionStore.ts• L9-25 packages/…/locale.ts• L1-50
Request-scoped context
Server rendering needs request isolation because requests can run at the same time. Next.js uses an asynchronous condition store for request reads. The Node runtime uses an asynchronous scope around withGT(). Both resolve the locale before translation work starts.
The server store can use a framework locale callback. Without one, it reads the configured header and cookie names and then considers preferred languages unless that option is disabled. Reads outside an active request scope produce a development error and a warned safe fallback in production.
References: packages/…/AsyncConditionStore.ts• L18-98 packages/…/AsyncConditionStore.ts• L100-159 packages/…/AsyncConditionStore.ts• L24-50 packages/…/AsyncConditionStore.ts• L87-89
Browser and client context
The browser condition store starts with server-provided values when they exist. It fills missing values from cookies and browser preferences, then stores the resolved configured identity so hydration and later requests use the same choice. A locale change updates the stored value and reloads the page when the runtime needs a new render. React also exposes the configured locale list through useLocales().
Vue follows the same contract through a cookie-backed locale accessor. An explicit server locale takes priority over an old browser cookie. A normal Vue runtime can load the target catalog and update reactive consumers. A reload-based runtime persists the choice and starts the next page with that locale.
References: packages/…/createBrowserConditionStore.ts• L23-71 packages/…/BrowserConditionStore.ts• L35-74 packages/…/localeCookie.ts• L18-60 packages/…/state.ts• L81-103 packages/…/CHANGELOG.md• L21-29
Global configuration registry
Shared configuration and condition stores use a registry on globalThis.__generaltranslation. Separate package entry points can therefore share one initialized instance in the same JavaScript environment.
The registry uses first-writer-wins behavior. A read before initialization throws a diagnostic error. A later replacement is ignored and can produce a debug warning. This prevents one request or application from silently using different locale configuration objects.
References: packages/…/createGlobalSingleton.ts• L7-27 packages/…/createGlobalSingleton.ts• L36-95 packages/…/singleton-operations.ts• L5-29
Persistence and settings
Locale state crosses requests through named storage settings. The same names must be used by every writer and reader that participates in locale selection.
Cookie contract
The shared cookie contract gives separate storage keys to locale context:
| Cookie role | Default name | Purpose |
|---|---|---|
| Locale | generaltranslation.locale | Stores the selected locale. |
| Region | generaltranslation.region | Stores the selected region. |
| Enable flag | generaltranslation.enable-i18n | Stores whether localization is enabled. |
| Reset marker | generaltranslation.locale-reset | Marks an explicit locale change for route handling. |
Browser reads are safe during server rendering because they do not access document outside a browser. Readers decode cookie values. Browser writers use a path-wide session cookie so web runtimes can share the value.
References: packages/…/cookieNames.ts• L1-11 packages/…/request.ts• L29-49 packages/…/browserCookies.ts• L7-36 packages/…/internal-cookies.ts• L1-11
Cookie name settings
React configuration can replace the locale, region, and enable-flag cookie names. It exposes the resolved names to browser stores and server readers. Next.js passes client-safe names from build configuration into runtime setup. Its routing integration can also align the locale cookie with the framework's standard preference cookie. These boundaries must preserve the configured locale identity rather than replacing it with a normalized spelling.
The locale cookie name is a cross-boundary contract. If an application changes it, request parsing, browser initialization, server synchronization, and external readers must use the same setting.
References: packages/…/i18nConfig.ts• L21-67 packages/…/config.ts• L315-329 packages/…/shared.ts• L22-36 packages/…/BrowserConditionStore.ts• L50-58
Enable and region settings
Region and enable state follow the locale through the same condition context, but each uses a separate storage key. A missing region stays unset. A missing enable flag defaults to enabled, so an absent cookie does not disable localization by accident.
Native runtimes use the configured cookie names as persistent-store keys. This keeps locale, region, and enable state aligned with web adapters without requiring browser cookies on the device.
References: packages/…/createBrowserConditionStore.ts• L74-97 packages/…/parseEnableI18n.ts• L5-16 packages/…/getInitialNativeConditions.ts• L5-31
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the supported-locale data and default locale aligned with framework routing lists. Next.js checks for mismatches between its
i18nsettings and GT settings. - Keep custom mapping targets as valid language tags. Request matching, alias resolution, and formatting use them.
- Keep configured locale identities stable across negotiation, route paths, cookies, hooks, translation loaders, and caches. Canonicalize only at service and formatting boundaries.
- Keep custom cookie names aligned across build-time config, server readers, browser stores, and native persistent-store keys.
- Preserve candidate precedence when adding a locale source. A route or explicit user choice must not be replaced by a browser preference.
- Preserve the server-to-client locale handoff so hydration does not replace an explicit server locale with an old cookie.
- Initialize shared configuration before condition stores or locale helpers are read. The global registry rejects late replacement.
- When changing the supported-locale package, keep its public resolver and list exports compatible with consumers that use them to validate or present locale choices.
- Treat every supported-locale entry as a valid locale and keep its resolver output inside the published list. When adding a region or script variant, verify that matching preserves the intended distinction from its broader language entry.
- Keep the
gt-nodedependency on the sharedgt-i18npackage aligned when the shared configuration or request-context contract changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A configured locale or mapping target is invalid while GT services are enabled | Configuration initialization fails with an invalid-locale diagnostic | Use valid BCP 47 locale codes or valid mapping targets |
| A request supplies an unsupported locale | The runtime uses the configured default, or a strict request validator rejects the value | Use isLocaleSupported() when rejection is required; use fallback resolution for preferences |
| Equivalent locale aliases are normalized before application code sees them | A configured identity can be lost in routes, loaders, hooks, or caches | Preserve the configured identity and canonicalize only at service and formatting boundaries |
| A supported-locale entry is invalid, removed, renamed, or a script-specific entry collapses into a broader entry | A listed choice can fail validity checks, or a user preference can resolve to the wrong locale | Keep every published entry valid, keep resolver output inside the list, and test distinct region and script variants |
| Server and client use different locale cookie names | The client cannot see the server choice and may resolve a different locale during hydration | Pass the same configured cookie name through framework setup and browser state |
| Locale helpers run before configuration or condition-store initialization | A diagnostic error is thrown instead of returning locale state | Initialize GT before reading locale state |
| A locale change updates a cookie without a new render in a reload-based runtime | The current page keeps content from the previous locale | Use the runtime locale setter so persistence and reload happen together |
| The Node adapter and shared i18n package use incompatible runtime contracts | Server request context can resolve or propagate locale state incorrectly | Keep gt-node and gt-i18n releases and dependency links aligned |
Key files
| File | Why to read it |
|---|---|
| packages/…/I18nConfig.ts | Defines shared locale settings and the main supported-locale resolution contract. |
| packages/…/package.json | Shows the published shared i18n boundary and its runtime dependencies. |
| packages/…/CHANGELOG.md | Records configured-identity behavior and the current shared dependency update. |
| packages/…/package.json | Shows the Node adapter boundary and its dependency on the shared i18n package. |
| packages/…/supportedLocales.ts | Defines the approved base languages and regional or script variants. |
| packages/…/index.ts | Shows how locale inputs map to the approved list and how consumers list it. |
| packages/…/supportedLocales.test.ts | States the validity and variant-resolution contract for published locale data. |
| packages/…/package.json | Shows the package boundary, version, exports, and shared locale-rule dependency. |
| packages/…/CHANGELOG.md | Records the current locale additions and dependency update. |
| packages/…/determineLocale.ts | Explains matching for approved locales and dialect variants. |
| packages/…/ReadonlyConditionStore.ts | Shows how a resolved locale enters a fixed render context. |
| packages/…/createGlobalSingleton.ts | Shows how configuration and condition state are shared across package entry points. |
| packages/…/parseLocale.ts | Shows request precedence between cookies and language headers. |
| packages/…/createBrowserConditionStore.ts | Shows how server values, cookies, callbacks, and browser preferences meet in the browser. |
| packages/…/AsyncConditionStore.ts | Shows the request-scoped locale boundary for Next.js server rendering. |
| packages/…/requestConditions.ts | Shows route, cookie, and header selection plus response cookie persistence. |
| packages/…/localeCookie.ts | Shows shared cookie behavior for Vue browser and server-rendered state. |