API reference#

Exports of @sigx/i18n v0.3.1, grouped by entry point.

@sigx/i18n#

Setup#

ExportKindPurpose
createI18n(options)pluginapp.use(createI18n({ … })). The store is created lazily on first use during render.
provideI18nConfig(options)functionClient-only config for the no-app case (@sigx/resume boundaries). A no-op on the server.
I18nOptionstypeThe resolved runtime config.

Key I18nOptions fields:

FieldNotes
fallbackLocaleRequired. The master locale — the source of truth for which keys exist.
initialLocaleLocale to start on before detection. Defaults to fallbackLocale.
supportedNegotiation target set; empty accepts any locale.
localeFallbacksExplicit fallbacks layered on BCP-47 truncation.
namespacesLoaded up front. Leave section-specific ones out so they load on use.
defaultNamespaceUsed when useTranslation() is called without one. Default 'translation'.
loadThe catalog loader. Without it, catalogs come from addMessages.
layers / defaultLayer / loaders / layerMessagesPer-key override layering — see Layers.
initialMessagesCatalogs seeded synchronously at creation, marked loaded. The idiomatic SSR preload.
formatterDefaults to lightweightFormatter.
onMissingMissing-key handler.
onLoadErrorReplaces the default console logging for a failed catalog load.

Reading#

ExportKindPurpose
useTranslation(ns?, options?)hookThe typed translator. Namespace and keys checked.
useDynamicTranslation(ns?, options?)hookNamespace checked, keys open — for runtime catalogs.
useLocale()hook{ locale, locales, setLocale, error, retry, whenReady }.
useI18n() / useI18nConfig()hookThe store itself, and the resolved config.
TcomponentDeclarative translation — k, params, ns, locale, default, components.
renderRich(text, components)functionThe tag-splitting behind <T components>.

Both readers take { locale } to pin a call to a locale other than the active one — see Translating into a locale that isn't the active one.

Store actions#

useI18n() returns the store. Beyond the reactive locale / locales / messages signals:

ActionPurpose
setLocale(locale)Switch; resolves once catalogs have loaded.
loadNamespace(locale, ns)Load one namespace explicitly.
addMessages(locale, ns, catalog, { layer? })Seed a catalog directly.
setLayer(layer, tree)Replace a whole override layer.
invalidate(locale?, ns?, layer?)Drop and reload. Narrows from the left; omit an argument to cover every value of it.
forLocale(locale)A translator bound to one locale.
translateKey(key, params?, { locale? })The primitive under the readers.

Detection#

detectLocale, createDetectors, findSupported, parseAcceptLanguage, parseCookie, settingsDetector, browserDetector, cookieDetector, urlDetector, detectionContextFromRequest, resolveRequestLocale, localeCookie, localeSwitchUrl, LOCALE_COOKIE.

Detector.detect is synchronous — an async native locale goes through initialLocale or await setLocale().

Lower-level#

translate, translateWith, lookup, getMessage, localeChain, matchLocale, lightweightFormatter, isPluralForms, composeCatalogs, composeAt, layerFor, flatten, createTranslator, createDynamicTranslator, bindLocale, installPersistSSR.

@sigx/i18n/server#

ExportPurpose
createRequestT(options)Build once, bind per request. Returns (request) => LocaleTranslator.
createServerT(options)A translator over an in-memory catalog tree; forLocale(locale) binds.
LocaleTranslatorforNamespace(ns), withLayers(trees), and the bound translation calls.

@sigx/i18n/server/node is the Node flavour of the same surface.

@sigx/i18n/vite#

ExportPurpose
i18n(options)The build plugin — scans catalogs, generates types, runs the consistency check.
runI18nCheck / checkCatalogs / formatReportThe check, for wiring into CI without a build.
writeI18nTypes / generateDtsType generation on its own.
buildManifest / scanDirThe catalog scan.

runtimeNamespaces declares namespaces whose keys are not knowable at build time — see Runtime catalogs.

@sigx/i18n/virtual#

Types for the generated virtual module (virtual:sigx-i18n/server-catalogs and friends).