DocsReact

Components

Reference for T, Var, Num, Currency, DateTime, RelativeTime, Plural, Branch and LocaleSelector.

On this page

All components are exported from @generic-translation/react. In server components of a Next.js app, import T, Tx and the variable components from @generic-translation/react/next instead; see Server components.

How <T> works

<T> serializes its children into one ICU message, looks the message up in the current locale’s translations by hash, and renders the translated message back onto your original elements. If there is no usable translation, it renders the source.

<T>
  Hello <b><Var name="name">{user.name}</Var></b>, you have{" "}
  <Plural n={count} one="one message" other={<><Num>{count}</Num> messages</>} />.
</T>

This serializes to:

Hello <0>{name}</0>, you have {count, plural, one {one message} other {{v0, number} messages}}.

Every non-Generic Translation element becomes a numbered tag (<0>...</0>, depth-first in document order), so translators and the AI may reorder text around them but cannot change your markup. The same algorithm runs in the CLI at build time, so both produce identical messages and hashes.

Rules:

  • Children must be static text, elements, or Generic Translation components. A bare dynamic expression such as {user.name} directly inside <T> is an error; wrap it in <Var>. String literal expressions ({"text"}, {" "}) count as text.
  • Text whitespace follows the standard JSX rules.
  • If the children cannot be serialized, <T> logs an error in development and renders the children untouched.
  • Translations whose variables or tags differ from the source are ignored.

<T> props

Prop Type Description
children ReactNode The content to translate.
context string Disambiguates identical source text (for example "button" vs "verb"). Part of the hash.
id string Human-friendly identifier for the dashboard. Not part of the hash.
<T context="navigation menu">Home</T>

serializeChildren(children, context?)

Also exported: returns { message, hash } for what <T> would serialize, or null. Useful in tests and to check the CLI’s output.

import { serializeChildren } from "@generic-translation/react";
serializeChildren(<>Hello <b>world</b></>); // { message: "Hello <0>world</0>", hash: "..." }

<Var>

Marks dynamic content that is inserted unchanged.

Prop Type Description
name string Variable name in the message ({name}). Auto-named v0, v1, … when omitted.
children / value ReactNode The content.
<T>Signed in as <Var name="email">{email}</Var></T>

Names are part of the message and therefore of the hash. Renaming a variable creates a new message.

<Num>

A number formatted for the current locale. Inside <T> it becomes {name, number}.

Prop Type Description
name string Variable name.
children / value number | string | bigint The number.
options Intl.NumberFormatOptions Extra formatting options.
locales string | string[] Override the locale when rendered standalone.
<T>Downloaded <Num options={{ maximumFractionDigits: 1 }}>{12345.678}</Num> files</T>
<Num>{1234567}</Num> {/* standalone: "1,234,567" in en */}

<Currency>

A currency amount. Inside <T>: {name, number, ::currency/EUR}.

Prop Type Description
currency string ISO 4217 code. Use a string literal so the CLI can see it. Default "USD".
name, children/value, options, locales As for <Num>.
<T>Total: <Currency currency="EUR">{total}</Currency></T>

<DateTime>

A date or time. Inside <T>: {name, date, medium}.

Prop Type Description
name string Variable name.
children / value Date | number | string The moment to format.
options Intl.DateTimeFormatOptions Default { dateStyle: "medium" }.
locales string | string[] Override when standalone.
<T>Last login <DateTime options={{ dateStyle: "long" }}>{lastLogin}</DateTime></T>

When you pass custom options inside <T>, the value is formatted with those options for the current locale and inserted as text.

<RelativeTime>

A relative time (“3 days ago”). It is a standalone formatting component, not part of message serialization.

Prop Type Description
date Date | number | string Compare this date with now.
value + unit number, Intl.RelativeTimeFormatUnit An explicit amount, such as value={-3} unit="day".
now Date Reference point for date. Pass it explicitly so server and client output match.
options Intl.RelativeTimeFormatOptions
locales string | string[]
<RelativeTime date={post.createdAt} now={renderTime} />
<RelativeTime value={-3} unit="day" />

<Plural>

Chooses a branch by number using the locale’s plural rules. Inside <T> it becomes an ICU plural argument.

Prop Type Description
n number The number that selects the branch.
name string Variable name. Default n.
zero one two few many other ReactNode Branch content. zero maps to ICU =0.
children ReactNode Used as the other branch when other is not given.

other is required in the message (via the prop or children). Branches are emitted in the fixed order zero, one, two, few, many, other. Translators add the categories their language needs.

<T>
  <Plural n={items} zero="Your cart is empty" one="One item" other={<><Num>{items}</Num> items</>} />
</T>

Used outside <T>, <Plural> picks the branch with the source language’s rules.

<Branch>

A select: chooses content by the value of branch. Inside <T>: {name, select, ...}.

Prop Type Description
branch string | number | boolean | null | undefined The selecting value.
name string Variable name. Default branch.
any other prop ReactNode A branch keyed by the prop name.
children ReactNode The fallback (other).
<T>
  <Branch branch={role} admin="You can manage this project." member="You can edit." >
    You can view this project.
  </Branch>
</T>

Reserved keys that cannot be used as branch names: branch, name, children, other.

<LocaleSelector>

A plain <select> that switches locale. Each language is shown in its own name (Espanol, Francais).

Prop Type Description
locales string[] Restrict or reorder. Default: all provider locales.
names Record<string, string> Override labels.
showEmoji boolean Prefix each option with the locale’s flag emoji.
onChange (locale) => void Called after the switch is requested.
other SelectHTMLAttributes Passed to the <select>. Default aria-label="Language".
<LocaleSelector locales={["en", "es"]} names={{ en: "English" }} className="picker" />

It sets aria-busy while translations load. For custom UIs use useLocales and useSetLocale.

Server-only: <Tx> and tx()

Exported from @generic-translation/react/server and @generic-translation/react/next only. <Tx> is like <T> but strings missing from the published translations are translated on demand through the API. See Server components.

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.