Type to search the documentation.

ICU messages, numbered tags and content hashing

How Generic Translation turns JSX into one translatable string, why tags are numbered, and how a content hash becomes the key for every translation.

Generic Translation’s whole pipeline rests on one decision: the unit of translation is a single ICU MessageFormat string, with numbered tags for markup, identified by a hash of its content. This post walks through why each of those three choices exists.

Why not translate fragments?

Take this component:

<T>
  Read the <a href="/docs">docs</a> or <button>contact us</button>.
</T>

You could translate “Read the “, “docs”, “ or “, “contact us” and “.” separately. Languages do not cooperate with that: word order changes, particles attach to links, and a model that sees four fragments cannot produce a natural sentence. So the sentence is translated whole, and the markup travels inside it.

Numbered tags

The serializer replaces each element with a numbered tag, counting depth-first in document order:

Read the <0>docs</0> or <1>contact us</1>.

The translator (human or model) may move, reorder or nest the tags, but must keep each one:

Lies die <0>Dokumentation</0> oder <1>kontaktiere uns</1>.

Why numbers instead of the element’s real name? Three reasons.

  • The message does not leak implementation. <a href> and <button> become <0> and <1>. Changing the className, swapping a for a router link, or renaming a component does not change the message, so it does not invalidate the translation.
  • The runtime keeps the props. At render time the library takes the translated message, finds <0> and attaches the original element and its attributes. Translators never touch href.
  • Validation is trivial. A translation is compatible with its source when it uses the same variable names and the same tag names. messagesCompatible in @generic-translation/core checks exactly that:
import { messagesCompatible } from "@generic-translation/core";

messagesCompatible("Hi <0>{name}</0>", "Salut {name}");
// { ok: false, problems: ["Missing tag <0>"] }

Hand-written messages, for example in @generic-translation/node, may use identifier tags such as <b>…</b> instead.

Variables, plurals and the rest of ICU

Dynamic values are ICU arguments, so formatting is locale-aware and the translator sees where the value goes:

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

The same grammar covers numbers and currencies ({total, number, ::currency/EUR}), dates, select and selectordinal. Plurals matter most: English has two forms, Russian has four, Arabic has six. The source message only needs one and other; the translation brings the categories its language requires. @generic-translation/core is a zero-dependency parser and formatter for this grammar, and it is the same code running in the playground on the home page.

In React you do not write the ICU by hand. <Var>, <Num>, <Plural> and friends inside <T> are serialized to it, by an algorithm that lives in @generic-translation/core so the CLI at build time and the library at runtime produce byte-identical messages. If they disagreed by a single space, the hashes would differ and the translation would not be found.

Content hashing

Once there is a canonical source string, the key is its hash:

hash = first 32 hex characters of
       SHA-256( JSON.stringify([dataFormat, source, context ?? ""]) )

For the message above, with no context, that is 26b383291da9a02c337ac3881ca995dd. Add a context note such as "inbox banner" and the hash becomes ff8088fb995380bfd65ed8d50e1b5ae8, so the same words can be translated differently in different places, as with “Save” the button and “Save” the noun.

Properties that fall out of this:

  • No key files. The sentence is the key.
  • Edits are explicit. Change a character and you have a new message. The old translation is not silently attached to different text.
  • Deduplication. Identical text with identical context shares one translation across files, and one cache entry on the server.
  • Cheap sync. A translation map is just { "<hash>": "<translated ICU string>" }. The CLI, the API cache and the CDN all speak it.
  • id is not hashed. Optional human-readable ids exist for dictionaries and tooling, but renaming one does not invalidate a translation.

The hash is computed in two ways, WebCrypto asynchronously and a small pure-JS SHA-256 synchronously, and both are tested to agree, because rendering code on a server often cannot wait.

Try it

Paste any message into the playground and it shows the formatted output, the plural category chosen for your locale, the variables and tags it found, and the hash. For the details, see message format, hashing and translation maps.

All posts