Introducing a new way to review your PRs.

Read the announcement

generaltranslation / gt

Full-stack localization for every framework

View on GitHub

22 documents · Updated Nov 23, 2026

docs / localization-runtime

Localization runtime and shared domain

The localization runtime gives application code one shared way to select locales, load translated content, format messages, and access the translation service. The core package provides the runtime and project boundary. The i18n package connects that boundary to catalogs and lookups. The node package adds request-scoped locale state for server code.

Shared localization domain

Locale model

The system treats a locale as a language with an optional region or script. It uses a BCP 47 code as the service form. A configured locale may use an application alias instead. The runtime keeps that alias for application code and maps it to the canonical service code only at an external boundary.

Locale matching selects the best supported configured identity. It prefers an exact or close match and falls back to the configured default when no supported match exists. This shared rule keeps request selection, loaders, and service results aligned. Cache lookups use the locale identity they receive rather than negotiating or aliasing it again.

References: packages/…/LocaleConfig.ts• L126-149 packages/…/LocaleConfig.ts• L329-369 packages/…/determineLocale.ts• L79-100 packages/…/index.test.ts• L144-217 packages/…/getLocale.test.ts• L22-115 packages/…/I18nCache.test.ts• L250-269

Package responsibilities

The packages divide the localization domain by purpose. core owns shared runtime services and authenticated project operations. i18n owns configuration, catalogs, cache behavior, and translation-function foundations. node supplies request context without changing the shared lookup rules.

The main relationship is:

flowchart LR
    App[Application] --> Node[node request context]
    Node --> I18n[i18n config and cache]
    I18n --> Core[core runtime]
    Core --> Format[locale and format rules]
    I18n --> Catalog[translation catalogs]
    Core --> Service[translation service]

References: packages/…/runtime.ts• L70-85 packages/…/I18nConfig.ts• L51-69 packages/…/I18nCache.ts• L89-124 packages/…/initializeGT.ts• L16-25 packages/…/package.json• L53-140 packages/…/package.json• L54-57 packages/…/package.json• L58-60

Runtime services

Core runtime

GTRuntime is the small runtime used by application integrations. It provides locale state, locale checks, formatting, and direct translation access. It accepts locale defaults and project access settings in one shared configuration.

GT extends this runtime for tools and server workflows that also need project and file operations. The generaltranslation/runtime entry point lets browser-facing code use localization without carrying the broader project client. Service access can use an API key or a user-token provider when a token must be obtained at request time.

References: packages/…/runtime.ts• L47-69 packages/…/runtime.ts• L84-123 packages/…/runtime.ts• L254-297 packages/…/index.ts• L113-156 packages/…/index.ts• L355-417 packages/…/package.json• L59-79 packages/…/gtApiSdk.test.ts• L157-229

API adapter

The core API adapter is the shared boundary between runtime callers and the generated General Translation API client. It accepts application-facing locale values and project data, sends canonical service values, and maps returned locale data back to configured identities.

It also presents one access surface for translation, project, file, upload, and job operations. The adapter refreshes its client and configuration together when callers reconfigure it. It rejects unconfigured use before a request can reach the service.

References: packages/…/createGtApi.ts• L200-254 packages/…/createGtApi.ts• L280-321 packages/…/createGtApi.ts• L583-649 packages/…/createGtApi.test.ts• L70-122 packages/…/gtApiSdk.test.ts• L174-229

Package entry points

The public entry points separate the small application runtime from the full project client and the generated API contract. This lets each consumer take only the surface that matches its role.

Entry pointRole
generaltranslationFull project and file client with shared runtime features
generaltranslation/runtimeSmaller locale, formatting, and runtime translation surface
generaltranslation/apiGenerated service client and API types
generaltranslation/internalShared low-level values for related packages
gt-i18nApplication-facing catalog and translation foundation
gt-nodeServer-facing request and locale helpers

The published runtime entry point stays separate from project management. Its package artifact checks also verify that both module formats and their type surfaces work for fresh consumers.

References: packages/…/package.json• L59-140 packages/…/package.json• L58-118 packages/…/package.json• L62-92 packages/…/package-artifacts.test.ts• L105-175

Translation catalog cache

I18nCache is the shared catalog service used by translation functions. It keeps content by the locale identity supplied by the caller. The default locale supplies source content. Other locales load through a selected catalog source, and a missing entry can use runtime translation as a fallback.

The cache supports startup data, a remote store, or an application loader. It shares concurrent loads and misses so the same message does not create repeated work. Cache misses can also be grouped into bounded batches. A canonical spelling, an alias, an unconfigured dialect, or an invalid locale that does not match the supplied cache identity reads as a cache miss and can fall back to source content. An application loader receives the configured identity supplied by the caller. When a target cannot be resolved, translation helpers can use source content as the safe result.

References: packages/…/I18nCache.ts• L105-187 packages/…/I18nCache.ts• L256-277 packages/…/I18nCache.ts• L583-597 packages/…/TranslationsCache.ts• L105-224 packages/…/I18nCache.test.ts• L211-269 packages/…/TranslationsCache.test.ts• L131-191 packages/…/fallbacks.test.ts• L20-115

Node request context

The Node package gives each request an active locale through an asynchronous scope. initializeGT creates the shared configuration and cache. getRequestLocale maps request language preferences to the supported locale set. withGT runs application work inside the selected locale scope.

This scope keeps concurrent requests from sharing mutable locale state. Locale aliases and supported dialects resolve to the configured identity before work runs. Outside a scope, development reports an error. Production warns and uses the configured supported locale so the application still has a defined result.

sequenceDiagram
    participant Request as HTTP request
    participant Node as node runtime
    participant Scope as request locale scope
    participant Cache as i18n cache
    participant App as application work

    Request->>Node: Accept-Language
    Node->>Scope: select supported locale
    Node->>Scope: withGT(locale, callback)
    Scope->>App: run callback in locale scope
    App->>Cache: resolve message
    Cache-->>App: catalog value or runtime fallback

References: packages/…/initializeGT.ts• L11-26 packages/…/getRequestLocale.ts• L13-39 packages/…/withGT.ts• L3-8 packages/…/AsyncConditionStore.ts• L24-51 packages/…/AsyncConditionStore.ts• L87-89 packages/…/getLocale.test.ts• L68-115 packages/…/withGT.test.ts• L25-65

Configuration and locale handling

Shared configuration

GTConfig is the common configuration for locale defaults, supported locales, custom mappings, project access, catalog sources, runtime translation, and optional development behavior. I18nConfig separates locale settings from service settings and provides the resolved view used by the runtime.

The effective configuration always has a default locale. If none is set, it uses en. If no supported list is set, the default is the only supported locale. A configured list also includes the default. Service-backed configurations validate locale values, while local-only configurations can keep application-defined values.

References: packages/…/config.ts• L41-83 packages/…/I18nConfig.ts• L25-69 packages/…/I18nConfig.ts• L199-223 packages/…/I18nConfig.test.ts• L33-70

Service modes

Catalog loading and runtime translation are separate service choices. A deployment can use published catalogs without runtime network calls. It can also enable runtime translation only when project access and credentials are available.

ConcernModesMain signals
Catalog loadingGeneral Translation remote, custom remote, custom loader, disabledprojectId, cacheUrl, loadTranslations
Runtime translationGeneral Translation API, custom API, disabledprojectId, apiKey, devApiKey, userTokenProvider, runtimeUrl

The default catalog store is https://cdn.gtx.dev. The default API and runtime translation service is https://api.gtx.dev. Callers can replace both service URLs. Remote catalog paths use canonical locale codes. Custom loaders receive configured identities so application-owned keys remain stable.

References: packages/…/getLoadTranslationsType.ts• L5-16 packages/…/getLoadTranslationsType.ts• L23-39 packages/…/getTranslationApiType.ts• L3-13 packages/…/getTranslationApiType.ts• L20-40 packages/…/createRemoteTranslationLoader.ts• L32-50 packages/…/routeCreateTranslationLoader.ts• L16-70 packages/…/configTypes.test.ts• L15-86

Locale normalization and matching

Locale input is standardized before it enters the runtime. The shared locale rules validate BCP 47 values, resolve custom aliases, and select the best supported locale from request preferences. The selected result remains the approved application identity.

Service and remote catalog requests use canonical codes. Application results use configured identities when the runtime can match the returned service code. A source and target that represent the same locale do not need translation and can return source content through the cache path.

References: packages/…/LocaleConfig.ts• L297-369 packages/…/requiresTranslation.ts• L16-43 packages/…/resolveCanonicalLocale.ts• L10-17 packages/…/resolveAliasLocale.ts• L9-19 packages/…/runtime.ts• L228-251 packages/…/createGtApi.ts• L208-224 packages/…/configuredLocaleEgress.test.ts• L45-119 packages/…/serviceLocaleEgress.wire.test.ts• L35-97

Public locale access

The i18n package exposes the active locale, default locale, supported locales, region, locale properties, and canonical locale resolution. The Node package re-exports the request-safe subset for server code. These helpers depend on initialized shared state and, where required, the active request scope.

Request matching handles exact values, quality values, language fallbacks, dialect fallbacks, arrays, wildcards, and aliases. It returns a configured identity from the approved set rather than creating a new spelling from the request.

References: packages/…/locale.ts• L4-77 packages/…/index.ts• L11-19 packages/…/index.ts• L7-14 packages/…/getRequestLocale.ts• L29-39 packages/…/getLocale.test.ts• L22-115

Project access and translation foundations

Project access

A project connects application localization to the translation service. Its ID selects the project. Its credentials authorize requests. Its project data describes the default locale and the locales available to the project.

The shared adapter exposes project and organization access, pagination, project API key creation, organization API key creation, file work, and translation jobs. Project access also scopes catalog URLs and runtime translation. Organization key creation requires a signed-in user session because a project API key cannot create an organization key.

References: packages/…/createGtApi.ts• L295-305 packages/…/createGtApi.ts• L583-649 packages/…/createGtApi.ts• L599-633 packages/…/createGtApi.test.ts• L254-300 packages/…/CHANGELOG.md• L3-34

File transfer boundary

File translation keeps source format and output format as separate parts of the contract. Text content crosses the service boundary as encoded text. Binary content keeps its encoded representation and does not pass through text decoding.

Small source files can travel in the request. A large source file or request is staged in storage and then sent to the service by reference. The same reference path can stage related format metadata. The upload path uses a storage transport without API credentials.

References: packages/…/createGtApi.ts• L86-178 packages/…/createGtApi.ts• L756-815 packages/…/base64.ts• L1-56 packages/…/createGtApi.test.ts• L588-829 packages/…/CHANGELOG.md• L3-14

Translation identity

Every translatable message needs a stable identity so catalogs can find the stored translation. The shared identity uses the source message and metadata that changes its meaning or handling. A supplied hash can preserve identity across build and runtime boundaries.

Content or hash-affecting metadata changes create a new translation entry. A format or review requirement can therefore change identity, while an application-only lookup ID does not change a calculated message hash. This identity is shared by registration, catalog lookup, and runtime translation requests, so those paths must agree on the same message description.

References: packages/…/hashSource.ts• L19-70 packages/…/types.ts• L4-16 packages/…/hashSource.test.ts• L29-139 packages/…/translateMany.ts• L72-89 packages/…/translateMany.test.ts• L212-264 packages/…/hashMessage.ts• L7-34 packages/…/hashMessage.test.ts• L23-105 packages/…/TranslationsCache.ts• L145-180

Translation content and metadata

The shared translation domain carries string, ICU, i18next, and JSX content. A successful result includes its target locale and content format. A failed result carries an error and status instead of pretending that translated content exists.

Metadata gives the system enough information to select and render an entry. It can carry translator context, an external ID, message format, review state, limits, action settings, and service hints. Typed variables preserve display meaning for numbers, dates, currency, and relative time across locales.

References: packages/…/translate.ts• L8-48 packages/…/entry.ts• L20-46 packages/…/types.ts• L1-31 packages/…/transformations.ts• L4-31 packages/…/options.ts• L12-49 packages/…/translation-data.ts• L4-10

Dictionary foundations

A dictionary is a tree of named entries rather than a flat list of messages. The default locale supplies the source tree. A target dictionary can contain translated leaves or partial subtrees. The cache can fill missing target entries through runtime translation.

The source tree remains the reference for shape and metadata. This lets application code request a complete object while the system resolves only the missing parts. A dictionary loader therefore needs source content when it loads a target dictionary.

References: packages/…/dictionary.ts• L3-23 packages/…/DictionaryCache.ts• L42-63 packages/…/DictionaryCache.ts• L94-133 packages/…/I18nCache.ts• L348-404 packages/…/I18nCache.ts• L715-749 packages/…/DictionaryCache.test.ts• L78-153

Localization lifecycle

Localization starts with configuration. The request or active scope selects a locale. The cache then resolves a catalog entry or asks runtime translation for a missing entry. Formatting produces the application value.

The same locale boundary applies to project and file work. Application inputs use configured identities. Service payloads use canonical codes. Returned data maps back to a configured identity when one exists.

flowchart TD
    Config[Initialize locale and service config] --> Request[Resolve request or active locale]
    Request --> Key[Build message lookup identity]
    Key --> Catalog{Catalog entry available?}
    Catalog -->|Yes| Format[Format with locale rules]
    Catalog -->|No| Runtime[Request runtime translation]
    Runtime --> Cache[Store resolved translation]
    Cache --> Format
    Format --> Output[Localized application output]

References: packages/…/initializeGT.ts• L16-25 packages/…/getRequestLocale.ts• L29-39 packages/…/getGT.ts• L43-116 packages/…/TranslationsCache.ts• L162-180 packages/…/createGtApi.ts• L208-224

The shared domain is the starting point for focused topics. Read Translation runtime for lookup and execution paths. Read Localization configuration for configuration and locale resolution. Read Message formatting for message forms and output rules.

References: packages/…/index.ts• L1-19 packages/…/index.ts• L73-107 packages/…/index.ts• L1-14

Change checklist

When these files or sections are changed, remember to consider these:

  • When locale mapping changes, check configured identities, request negotiation, cache keys, loaders, remote paths, service payloads, and returned metadata.
  • When the API adapter changes, check direct translation plus project, file, upload, and job operations for both input conversion and output restoration.
  • When upload routing changes, check inline limits, request limits, metadata staging, storage credentials, and large-file failure reporting.
  • When file encoding changes, check text decoding, binary pass-through, hashes, and download behavior together.
  • When package exports or workspace versions change, check the runtime entry point and the gt-i18n and gt-node consumers.
  • When project access changes, check project-key access separately from signed-in organization-key access.
  • When message identity or metadata changes, check registration, catalog lookup, runtime misses, and persisted entries together.
  • When request context changes, check locale negotiation, async scope isolation, and every helper that reads the active locale.

Important failure modes

TriggerConsequenceGuard
Runtime or adapter use occurs before configurationThe request fails because no shared client or locale state existsInitialize the runtime or configure the adapter first
Request work runs outside the Node locale scopeDevelopment cannot identify the active localeRun request work inside withGT
Input locale has no valid or supported matchConfiguration fails or resolution uses the default localeUse valid supported values or explicit mappings
A cache lookup uses a canonical, aliased, unconfigured, or invalid spelling instead of the supplied configured identityThe catalog is missed and source content is usedPass the configured locale identity to cache operations
Configured locale is sent directly to the serviceA catalog or API lookup uses the wrong locale keyConvert at the service boundary and restore the configured identity on output
Large content stays on the inline upload pathThe service or network rejects the request as too largeUse the staged reference path for content above the inline limits
Binary content is decoded as textFile bytes change or become unusableApply text encoding only to text formats
Catalog loading has no usable sourceA cache miss cannot load published contentConfigure a remote store, custom loader, startup data, or disable the path explicitly
Runtime translation has no project or credentialA missing catalog entry cannot be translatedProvide project access or disable runtime fallback
Organization key creation uses a project keyThe service rejects the operationUse the signed-in user session
Hash-affecting message metadata changes without a catalog updateExisting translations no longer matchKeep registration metadata and published entries aligned
Dictionary loading has no source dictionaryMissing target entries cannot be materializedProvide the default dictionary with the loader

Key files

FileWhy to read it
packages/…/runtime.tsDefines the shared locale, formatting, authentication, and runtime translation boundary.
packages/…/createGtApi.tsShows the service boundary for locale conversion, project access, file transfer, uploads, and jobs.
packages/…/createGtApi.test.tsShows the adapter contracts for reconfiguration, locale round trips, pagination, and large-file uploads.
packages/…/base64.tsDefines the text and binary content boundary.
packages/…/hashSource.tsDefines stable message identity across runtime and catalog paths.
packages/…/package.jsonDefines the full client, runtime, API, and shared utility entry points.
packages/…/package-artifacts.test.tsShows the published runtime boundary for new ESM, CommonJS, and type consumers.
packages/…/I18nConfig.tsConnects shared configuration to locale rules and service modes.
packages/…/I18nCache.tsCoordinates catalogs, cache misses, dictionaries, and runtime fallback.
packages/…/TranslationsCache.tsDefines per-locale message lookup, batching, and identity use.
packages/…/DictionaryCache.tsDefines source and target dictionary lookup and materialization.
packages/…/locale.tsProvides shared public locale access.
packages/…/package.jsonDefines the application-facing i18n and internal entry points.
packages/…/initializeGT.tsBuilds the Node runtime and installs shared services.
packages/…/getRequestLocale.tsMaps request language preferences to configured locales.
packages/…/withGT.tsEstablishes the request scope for server translation work.
packages/…/AsyncConditionStore.tsHolds active locale state for concurrent requests.
packages/…/package.jsonDefines Node entry points and links to the shared runtimes.