DocsKey concepts
Message format
Every translatable unit in Generic Translation is an ICU MessageFormat string with numbered tags. This page is the full grammar.
On this page
Generic Translation has exactly one wire format. Whether a string comes from a <T> component, a t() call, a dictionary file or an API request, it is stored, hashed, translated and delivered as an ICU MessageFormat string with tags:
Hello <0>{name}</0>, you have {count, plural, one {# message} other {# messages}}.
You can experiment with the format in the playground on the home page.
Arguments
An argument is a name in braces. It is replaced by a value at render time.
| Syntax | Meaning |
|---|---|
{name} |
Insert the value as text. |
{n, number} |
Format a number for the locale (1,234.5 / 1.234,5). |
{n, number, ::currency/USD} |
Format as currency. The three-letter code is an ISO 4217 currency. |
{d, date, short} |
Format a date. Styles: short, medium, long, full. |
{d, time, short} |
Format a time of day, with the same four styles. |
{n, plural, …} |
Choose a branch by the plural category of a number. |
{n, selectordinal, …} |
Choose a branch by ordinal category (1st, 2nd, 3rd…). |
{x, select, …} |
Choose a branch by exact string match. |
Plurals
Languages disagree about how many plural forms exist. English has two (one, other); Russian and Polish have four; Arabic has six; Japanese has one. ICU names the categories zero, one, two, few, many and other, and each locale uses a subset.
{count, plural, =0 {No files} one {# file} other {# files}}
otheris required. It is the fallback for every number.=Nmatches an exact number and wins over a category.=0is the usual way to write an “empty” message.#inside a plural branch prints the number, formatted for the locale.offset:Nsubtracts N before choosing a category and before printing#. Exact matches (=N) compare against the original number.
{guests, plural, offset:1
=0 {Nobody is coming}
=1 {{host} is coming alone}
one {{host} and # other are coming}
other {{host} and # others are coming}}
A translator is free to use different categories from the source. A source with one and other becomes one, few, many, other in Polish. That is expected and valid: compatibility checks compare variables and tags, not plural branches.
selectordinal works the same way with ordinal rules:
You finished {place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}.
Select
select picks a branch by string value. other is required and used when nothing matches.
{role, select, owner {You own this project.} admin {You can manage this project.} other {You can view this project.}}
Tags
Tags mark a span of the message that will be wrapped in something at render time: a link, bold text, a component.
Read the <0>terms</0> and the <1>privacy policy</1>.<2/>
<0>…</0>wraps content.<1/>is self-closing, for elements with no translatable children such as an icon or a line break.- Numbered tags are what the React integration produces. Numbers are assigned depth-first in document order starting at 0, so the translator can reorder them freely and the library still knows which element is which.
- Named tags such as
<b>…</b>or<link>…</link>are allowed for messages you write by hand, for example int()calls or dictionaries. A tag name is a non-negative integer or an identifier. - Tags nest:
<0>Read <1>this</1></0>.
The message never contains attributes, class names or URLs. Those stay in your code, which means a translator cannot break your markup and your markup changes do not invalidate translations.
Escaping
Generic Translation follows ICU apostrophe rules.
| You write | You get |
|---|---|
'' |
A literal apostrophe '. |
'{' |
A literal {. A quote that precedes syntax starts a quoted run that ends at the next single '. |
'{name}' |
The literal text {name}. |
don't |
don't. A lone apostrophe that is not followed by syntax is literal. |
In practice you rarely write escapes yourself. Text inside <T> is escaped for you, and escapeMessage() in @generic-translation/core does it for arbitrary strings.
Data formats
Every unit carries a dataFormat:
"ICU"(the default): parsed as described above."STRING": opaque plain text. Nothing is parsed, braces and angle brackets are literal. Use it for content that is not a message, such as a paragraph that happens to contain code.
The data format is part of the hash, so the same text as ICU and as STRING are two different units.
Compatibility between source and translation
A translation is compatible with its source when it uses the same set of variables and the same set of tags. Generic Translation checks this in three places:
- After the model returns a translation, before it is stored.
- When someone edits a translation in the editor or through
PUT /strings/:hash/:locale, which answers400 incompatible_messageif the check fails. - On demand, with
messagesCompatible(source, translation)from@generic-translation/core.
Dropping {name}, inventing {username}, or losing <1> are all caught. Reordering, changing plural categories and rewriting the surrounding text are all fine.
How JSX becomes a message
@generic-translation/react at runtime and the CLI at build time must agree on the message for a given piece of JSX, byte for byte, or the hash would not match. Both call the same function, serializeJsx in @generic-translation/core. The rules:
JSX inside <T> |
In the message |
|---|---|
| Text | The text, whitespace-normalized as JSX does, then ICU-escaped. Adjacent text merges. |
Any element or component, e.g. <a href="…">x</a> |
<N>x</N>, or <N/> if it has no children. |
A plain fragment <>…</> |
Transparent. Its children are inlined. |
<Var name="user"> |
{user} |
<Num name="total"> |
{total, number} |
<Currency name="price" currency="EUR"> |
{price, number, ::currency/EUR} |
<DateTime name="due"> |
{due, date, medium} |
<Plural n={…} one="…" other="…"> |
{n, plural, one {…} other {…}} |
<Branch branch={…} a="…" b="…">fallback</Branch> |
{branch, select, a {…} b {…} other {fallback}} |
Details worth knowing:
- Variables without a
nameare namedv0,v1, … from a single counter in document order. Naming them gives translators something meaningful to read. - A
<Plural>defaults its argument name ton; a<Branch>defaults tobranch. - Plural branches are always emitted in the order
=0,one,two,few,many,other. Thezeroprop maps to=0. If there is nootherprop, the element’s children are theotherbranch. - Tag numbers and auto-assigned variable names keep counting through plural and select branches, in the order branches are emitted.
A non-literal expression directly inside <T>, such as <T>Hello {name}</T>, is an error: the extractor cannot know what it is. Wrap it in <Var>. String literals like {" "} count as text.
Something wrong or unclear on this page? Open an issue.