React integration
The React integration connects the localization runtime to React applications. It provides setup, a provider boundary, condition state, translation access, JSX components, macros, and separate browser, server, and React Server Component (RSC) entry points. The Application framework integrations page gives the wider framework view. The Next.js integration page covers Next.js-specific request and route behavior, while Alternative framework integrations covers other adapters.
Integration boundary
React runtime surface
gt-react is the application-facing package. It builds on @generaltranslation/react-core, which exposes shared pure helpers, hooks, standard React components, and RSC-safe components through focused public entry points. The two packages keep the application API aligned while separating React state setup from reusable translation behavior.
The React Core package publishes these surfaces as named pure, hook, standard-component, and RSC-component entry points. Its output is tree-shakeable, so an application can use only the public capabilities it needs. The runtime selects a surface for the environment that imports it. Browser code can use writable browser state. Server code uses request-scoped state. RSC code uses request-scoped readers and RSC-safe components instead of providers or mutable browser state. Configured locale identities remain application-facing values across these surfaces, including configured aliases such as en-GB or en-gb.
References: packages/…/package.json• L56-121 packages/…/package.json• L48-68 packages/…/react-core-package.test.ts• L49-102 packages/…/CHANGELOG.md• L445-447
Runtime setup
Server-rendered setup
Server-rendered React creates localization configuration and a translation cache before rendering. GTProvider receives the request conditions and the translation and dictionary data for that request. Descendants then render from one request snapshot.
The provider keeps conditions separate from translation data. This lets locale and region state guide lookups while the provider supplies the callbacks needed for development-time missing-translation refresh.
References: packages/…/initializeGTSRA.ts• L1-14 packages/…/initializeGTSRA.ts• L1-17 packages/…/ServerGTProvider.tsx• L1-45 packages/…/GTProviderProps.ts• L1-22
Browser setup
Browser applications use initializeGT for server-rendered hydration or initializeGTSPA for a standalone single-page application. SPA setup creates the configuration, browser cache, condition state, and React translation store. It loads the first locale snapshot before the application uses that state.
BrowserGTProvider connects these services to the React tree. In server-rendered browser flows, server-provided conditions remain the rendered state until a later server snapshot accepts a requested change. Condition setters persist locale, region, and translation enablement in cookies, then request a reload so the next render uses the new conditions. Provider-free SPA flows keep their cookie-backed behavior.
The setup flow moves from runtime configuration to state creation and then to the provider boundary:
flowchart LR
A[initializeGT or initializeGTSPA] --> B[Locale and region conditions]
A --> C[Translation cache and store]
B --> D[BrowserGTProvider]
C --> D
D --> E[React hooks and components]
References: packages/…/initializeGTSRAClient.ts• L1-19 packages/…/initializeGTSPA.ts• L1-44 packages/…/BrowserGTProvider.tsx• L1-102 packages/…/BrowserGTProvider.test.tsx• L100-166 packages/…/CHANGELOG.md• L151-160
Provider context
Provider boundary
GTProvider is the application boundary for React localization. It makes locale conditions, translation snapshots, dictionary snapshots, and the update store available to the tree. Hooks and components use this boundary to render current content and receive later translation updates.
Server and browser providers use different condition stores. The server store is read-only for one request. The browser store can write cookies and request a reload. Both providers use the same core context, so consumer code keeps one API across render modes.
References: packages/…/InternalGTProvider.tsx• L16-77 packages/…/ServerGTProvider.tsx• L13-45 packages/…/BrowserGTProvider.tsx• L16-102
Translation snapshots
Provider snapshots give React a synchronous view of translations and dictionaries that are known at render time. The shared I18nStore links these snapshots to the translation cache and notifies subscribed lookups when new content arrives.
This split supports request-time rendering and later browser updates. A lookup reads a stable snapshot first. In development, each resolver tracks the lookups used by its hook instance and filters store events to those keys. The store can then add runtime results without replacing the whole React tree or rerendering consumers for unrelated lookups.
References: packages/…/context.ts• L19-44 packages/…/I18nStore.ts• L44-75 packages/…/I18nStore.ts• L144-215 packages/…/useSubscribeToTrackedLookups.ts• L15-57 packages/…/singleton-operations.test.ts• L38-115
Condition store
Locale conditions
The condition store holds the current locale, region, and enableI18n setting. Translation hooks and formatting components read these values, so one set of conditions controls message lookup, locale formatting, and whether translation is required.
In the browser, the store persists conditions in cookies. It resolves locale candidates to configured supported identities before it writes or reloads. Initial values follow a defined priority between supplied values, cookies, custom getters, and browser language data. Setter hooks update the condition and use the configured reload path so the next render uses the new state.
Custom cookie names come from I18nConfig. This keeps condition reads and writes aligned when an application does not use the default names. Configured aliases also remain stable in the public locale, cookie, reload, and cache values.
References: packages/…/condition-store.ts• L6-53 packages/…/BrowserConditionStore.ts• L25-157 packages/…/createBrowserConditionStore.ts• L15-97 packages/…/BrowserConditionStore.test.ts• L54-231 packages/…/i18nConfig.test.ts• L30-113 packages/…/CHANGELOG.md• L489-495
Locale and region selectors
The selector hooks turn condition state and configured locale data into application controls. useLocaleSelector exposes the current locale and available locale choices. useRegionSelector adds region choices and can link a selected region to a locale.
LocaleSelector and RegionSelector provide ready-to-use HTML select controls. Applications can use the hooks instead when they need custom controls or display rules. Selector values preserve configured locale identities, including aliases, so the selected option matches the condition store.
References: packages/…/useLocaleSelector.ts• L1-21 packages/…/useRegionSelector.ts• L1-30 packages/…/LocaleSelector.tsx• L13-37 packages/…/RegionSelector.tsx• L25-74
Translation access
Translation hooks
The hooks provide translation access at the level that matches the calling code. useGT translates a message string with current conditions. useMessages handles encoded messages from the localization toolchain. useTranslations resolves entries in a configured dictionary and supports access to dictionary objects.
The same hook surface exposes runtime and locale facts. It gives application code access to values such as supported locales, locale direction, locale properties, custom mappings, and condition setters.
In development, the returned translation functions stay stable across unrelated renders. A function changes when one of the lookups it used receives a hot-reload update, so direct output and values derived in memoized code or effects can refresh together.
References: packages/…/hooks.ts• L4-32 packages/…/useGT.ts• L14-57 packages/…/useMessages.ts• L8-33 packages/…/useTranslations.ts• L21-141 packages/…/clientHooks.test.tsx• L91-191 packages/…/CHANGELOG.md• L54-58
Translation functions
The pure function surface supports translation outside a React component. t translates a message string or tagged template at run time. msg and related helpers register or decode message data for later translation. Other helpers support derived values, declared variables, and fallback forms.
The module-level t call needs initialized conditions. In server-rendered applications, calling it without request-time conditions is rejected in development and uses the default locale outside development. This prevents one request locale from becoming shared module state.
References: packages/…/pure.ts• L1-71 packages/…/t.ts• L18-84 packages/…/t.ts• L181-210
Translation macros
The gt-react/macros entry point installs a global tagged-template t function. It supports the short form t\message`` when the application build path supports the related transform or runtime lookup.
This entry point has side effects by design. Import it once during application setup before using the global function. Normal React entries export t explicitly and do not need this global setup. The package marks the macro output as a side effect so a bundler keeps its initialization.
References: packages/…/macros.ts• L1-18 packages/…/package.json• L12-14 packages/…/package.json• L112-121 packages/…/react-package.test.ts• L144-165
React components
JSX translation
T treats JSX children as translatable content. It prepares the source children, looks up target content for the active locale, and renders the translated or source form with the same variable and rich-content structure. Client and server versions use the React store and context. The RSC version receives request conditions directly and can wait for its lookup.
Tx is the runtime translation component for RSC. It has the same broad JSX translation goal as T, but it is available only from the RSC entry point. Client and server entries expose a diagnostic in its place rather than a usable Tx implementation.
References: packages/…/T.tsx• L8-87 packages/…/T.rsc.tsx• L11-73 packages/…/Tx.rsc.tsx• L10-68 packages/…/index.server.ts• L47-69 packages/…/index.rsc.ts• L38-50
Branch and value components
The component set keeps localized content expressive. Branch components select content by a value or plural rule. Value components format numbers, dates, currencies, and relative times for the active locale. Var passes a value into translated content. Derive marks content that can relate to a source message.
| Component group | Purpose |
|---|---|
Branch, Plural | Select a matching content branch. |
Var, Derive | Carry or mark values inside translated content. |
Num, DateTime, Currency, RelativeTime | Format values for the active locale and translation conditions. |
Standard React and RSC-safe exports cover the behaviors that each runtime can support. RelativeTime uses the relativetime transformation name expected by extraction and translation hashing, so formatted relative times keep their variable identity inside T.
References: packages/…/components.ts• L4-20 packages/…/components-rsc.ts• L6-31 packages/…/Branch.tsx• L3-45 packages/…/RelativeTime.tsx• L1-31 packages/…/RelativeTime.rsc.tsx• L1-26 packages/…/CHANGELOG.md• L74-82
Runtime entry points
Client and server entries
Package export conditions select the runtime surface that matches the consuming environment. The browser entry uses BrowserGTProvider and exposes browser condition setup. The server entry uses ServerGTProvider and rejects browser-only setup calls. Both entries expose context-aware hooks, functions, selectors, and standard React components.
The initializeGT name is shared across these entries, but its behavior is runtime-specific. Worker runtimes resolve to the server entry, which keeps request state separate from browser globals. The export map checks worker conditions before browser so server work on an isolate does not load browser state.
| Entry point | Main role | Provider surface |
|---|---|---|
| Browser | Client-side rendering and hydration | BrowserGTProvider as GTProvider |
| Server | Server-rendered React runtime | ServerGTProvider as GTProvider |
| RSC | React Server Component rendering | No usable provider or selector; RSC-safe components and request-scoped readers |
References: packages/…/package.json• L56-110 packages/…/index.client.ts• L6-35 packages/…/index.server.ts• L11-49 packages/…/react-package.test.ts• L64-142 packages/…/CHANGELOG.md• L461-464
RSC entry point
The RSC entry point avoids context-dependent client APIs. Its hooks read request-scoped runtime values. Its translation components can wait for translation data before returning React content. Client-only providers and selectors fail with a clear diagnostic when imported from this surface.
The entry point also exposes parseLocale for incoming server requests. It checks the configured locale cookie before the Accept-Language header, then uses the configured default when no supported candidate exists. This boundary lets server components translate content without moving mutable browser state into the server component graph.
References: packages/…/index.rsc.ts• L1-104 packages/…/parseLocale.ts• L4-32 packages/…/parseLocale.test.ts• L17-75 packages/…/components-rsc.ts• L1-65
Translation cache behavior
Runtime cache
The React cache stores translation and dictionary data used by pure functions, hooks, and components. Server setup creates the shared React cache from the configured loader and cache settings. SPA setup adds a browser cache and loads the first locale snapshot before rendering.
In development, the browser cache can retain runtime translations in local storage. This supports translation hot reload across refreshes without making local storage part of the normal production path. Cache entries use the resolved configured locale identity, so aliases can restore their runtime translations after cache recreation.
References: packages/…/ReactI18nCache.ts• L1-6 packages/…/initializeGTSPA.ts• L18-44 packages/…/BrowserI18nCache.ts• L25-78 packages/…/LocalStorageTranslationCache.ts• L24-33 packages/…/BrowserI18nCache.localeAliases.test.ts• L12-59
Missing translation handling
When a lookup has no target translation, rendering uses the source content or a formatted source value. In development with hot reload enabled, translation and dictionary resolvers record the missing lookup, then queue a request for the post-commit phase. This keeps store writes and network work out of render while allowing the provider to supply custom handlers for runtimes that cannot use effects.
After the store receives a result, it emits an event for the translation, dictionary entry, or dictionary object. The matching resolver invalidates its hook instance, so memoized dictionary objects and translation values update without broad tree refreshes. Compiler-injected message lookups are also tracked before render and requested after commit when their data is absent.
If the first locale snapshot cannot load translations, the runtime warns and omits that locale from the snapshot. Rendering still uses untranslated content. A later lookup can retry when the data becomes available. Runtime request failures are reported through React diagnostics and do not replace rendered content with an error.
References: packages/…/useTrackedTranslationResolver.ts• L21-43 packages/…/useTrackedTranslationResolver.ts• L114-181 packages/…/useTrackedDictionaryResolver.ts• L19-69 packages/…/useTrackedDictionaryObjResolver.ts• L19-69 packages/…/missing-translation.ts• L30-43 packages/…/missing-translation.ts• L142-189 packages/…/I18nStore.ts• L78-142 packages/…/CHANGELOG.md• L285-292
Change checklist
When these files or sections are changed, remember to consider these:
- Keep browser, server, worker, and RSC export conditions aligned with the provider and hook capabilities of each entry point.
- Keep the
@generaltranslation/react-corepublic subpaths aligned with the pure helper, hook, standard component, and RSC component surfaces consumed bygt-react. - Keep
SharedGTProviderPropscompatible with both condition stores and with the translation and dictionary snapshots consumed by the core provider. - Keep configured cookie names aligned between
I18nConfig, browser condition-store reads, browser condition-store writes, and request locale parsing. - Keep locale identity resolution consistent between provider snapshots, browser cookies, selectors, request parsing, and translation cache keys.
- When changing browser provider conditions or setters, preserve cookie writes and the reload payload for locale, region, and
enableI18n. - When changing translation lookup or store events, preserve per-hook lookup tracking, event-key matching, post-commit requests, and updates to memoized or effect-derived values.
- When changing
TorTx, preserve the distinction between context-based rendering and explicit request-condition rendering. - Keep the macro export side effect separate from the explicit
tfunction so bundlers do not remove required global initialization. - Keep variable transformation names aligned between React components,
_gtttags, extraction, and rendered translation hashes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A context-aware hook or component renders without GTProvider. | The runtime cannot read localization context and raises a diagnostic error. | Initialize the runtime before rendering and place GTProvider at the root of the relevant tree. |
initializeGTSPA or browser condition-store setup runs from the server entry. | Browser-only state setup fails immediately. | Use the browser entry for SPA state and the server entry for server rendering. |
| A worker runtime selects the browser entry. | SSR can touch browser globals or share browser condition state across requests. | Keep workerd and worker ahead of browser in the export conditions. |
Tx is imported from a client or server entry. | The call fails because Tx is RSC-only. | Import Tx from the RSC entry and use T in context-aware entries. |
Server code calls module-level t before request conditions exist. | Development throws. Other environments use the default locale. | Move the call into request-time code or use the request-aware translation API. |
| A locale or region change updates cookies but does not reload the application. | The browser keeps rendering with stale request conditions. | Use condition-store setters or selector controls so the configured reload path runs. |
| Initial translation data does not match provider conditions. | Lookups miss or hydration can show source content before the correct target data is ready. | Pass request conditions and matching snapshots to the provider. |
| A missing lookup starts a store request during render. | React can see side effects from an abandoned render or repeat the request. | Queue development hot-reload work and flush it after commit. |
| A store event key does not match the key tracked by a resolver. | New translations arrive but the affected hook keeps its old function or memoized value. | Use the same listener-key rules for tracking, subscription filtering, and store events. |
| A relative-time transformation name differs from the extractor’s name. | _gtt parsing can lose the variable type, so formatted time or hash matching fails. | Keep the relativetime tag and variable name consistent across React, RSC, and extraction paths. |
Key files
| File | Why to read it |
|---|---|
| packages/…/package.json | Defines browser, server, worker, RSC, and macro export selection. |
| packages/…/package.json | Defines focused public entry points for shared React helpers, hooks, and components. |
| packages/…/index.client.ts | Shows the browser-facing React API. |
| packages/…/index.server.ts | Shows the server-rendered React API and browser-only guards. |
| packages/…/index.rsc.ts | Shows the RSC API, request locale parsing, and request-scoped restrictions. |
| packages/…/BrowserGTProvider.tsx | Explains the browser provider boundary, cookie persistence, and condition changes. |
| packages/…/ServerGTProvider.tsx | Explains the server provider boundary and missing-translation hooks. |
| packages/…/initializeGTSPA.ts | Shows SPA startup responsibilities. |
| packages/…/BrowserConditionStore.ts | Shows how browser locale, region, and translation enablement change. |
| packages/…/createBrowserConditionStore.ts | Shows how browser state is selected from server props, cookies, and browser settings. |
| packages/…/LocaleSelector.tsx | Shows how locale conditions become a user-facing selector. |
| packages/…/parseLocale.ts | Shows how server requests select a locale. |
| packages/…/macros.ts | Shows the side-effecting global macro setup. |
| packages/…/BrowserI18nCache.ts | Shows browser translation caching and development hot reload. |
| packages/…/InternalGTProvider.tsx | Shows the shared context contract for snapshots and runtime stores. |
| packages/…/useGT.ts | Shows the hook path for string translation. |
| packages/…/useTranslations.ts | Shows dictionary entry and object access with tracked update behavior. |
| packages/…/useSubscribeToTrackedLookups.ts | Shows how only lookups used by a hook instance invalidate it in development. |
| packages/…/useTrackedTranslationResolver.ts | Shows translation lookup tracking, compiler lookup preloading, and update-driven function identity. |
| packages/…/useTrackedDictionaryResolver.ts | Shows tracked dictionary entry resolution and missing-entry requests. |
| packages/…/useTrackedDictionaryObjResolver.ts | Shows tracked dictionary object resolution and missing-object requests. |
| packages/…/missing-translation.ts | Shows post-commit handling for missing translation and dictionary data. |
| packages/…/t.ts | Shows pure string and tagged-template translation behavior. |
| packages/…/T.tsx | Shows context-based JSX translation. |
| packages/…/T.rsc.tsx | Shows RSC JSX translation. |
| packages/…/RelativeTime.tsx | Shows locale-aware relative-time values inside standard React translation content. |
| packages/…/RelativeTime.rsc.tsx | Shows the request-scoped relative-time surface for RSC content. |
| packages/…/components-rsc.ts | Defines the RSC-safe component and helper surface. |
| packages/…/clientHooks.test.tsx | Shows stable hook identities and development updates for direct, memoized, effect-derived, and dictionary values. |
| packages/…/react-package.test.ts | Shows the runtime export conditions, macro isolation, and built package contract. |
| packages/…/BrowserConditionStore.test.ts | Shows cookie precedence, custom storage names, and configured locale aliases. |
| packages/…/BrowserI18nCache.localeAliases.test.ts | Shows runtime translation persistence across browser cache recreation. |
| packages/…/BrowserGTProvider.test.tsx | Shows server-condition precedence and reload behavior for browser updates. |
| packages/…/react-core-package.test.ts | Shows the public React Core entry surfaces and tree-shakeable package shape. |