DocsKey concepts

Hashing

How Generic Translation identifies a message by its content, why ids are not part of the key, and how to compute a hash yourself.

On this page

Generic Translation does not ask you to name your strings. A message is identified by a hash of its content, and that hash is the key in every translation map, cache and API response.

The algorithm

hash = first 32 hex characters of
       SHA-256( JSON.stringify([dataFormat, source, context ?? ""]) )
  • dataFormat is "ICU" or "STRING".
  • source is the message in the source language, exactly as serialized.
  • context is the optional disambiguating note. A missing context is hashed as the empty string.
  • The JSON text is encoded as UTF-8 before hashing.
  • The result is the first 32 lowercase hex characters (128 bits) of the digest.

For the message Save with no context, the hashed text is the 17 characters ["ICU","Save",""], and the hash is 4c39ccda3b013a53caffae360acf81a2. Add the context Button label and it becomes ab10c3afc8449d17ea09ffbb6d6ef20c.

Computing a hash

With @generic-translation/core:

import { hashSource, hashSourceSync } from "@generic-translation/core";

await hashSource({ source: "Save" });
// "4c39ccda3b013a53caffae360acf81a2"  (WebCrypto, async)

hashSourceSync({ source: "Save", context: "Button label" });
// "ab10c3afc8449d17ea09ffbb6d6ef20c"  (pure JS, sync)

Both functions return the same value for the same input. The async one uses WebCrypto; the sync one is a pure-JavaScript SHA-256 for places where you cannot await, such as during a React render.

Without the library, in any environment with WebCrypto:

async function genericTranslationHash(source: string, context = "", dataFormat = "ICU") {
  const bytes = new TextEncoder().encode(JSON.stringify([dataFormat, source, context]));
  const digest = await crypto.subtle.digest("SHA-256", bytes);
  return [...new Uint8Array(digest)]
    .map((b) => b.toString(16).padStart(2, "0"))
    .join("")
    .slice(0, 32);
}

In a shell:

printf '%s' '["ICU","Save",""]' | sha256sum | cut -c1-32
# 4c39ccda3b013a53caffae360acf81a2

If you implement this in another language, make sure your JSON encoder produces the same bytes as JavaScript’s JSON.stringify: no spaces between elements, and non-ASCII characters written literally rather than as \u escapes.

What changes a hash

Change New hash?
Any character of the source text, including whitespace and punctuation Yes
Adding, removing or editing context Yes
Switching dataFormat Yes
Wrapping a word in a new element (a new tag appears in the message) Yes
Changing an element’s attributes, class or href No. Attributes are not in the message.
Swapping <b> for <strong> No. Both are just tag <0>.
Renaming or adding an id No
Moving the component to another file No

Why id is not part of the hash

You can attach an id to a message (<T id="checkout.title">, the $id option of t(), or a dictionary path like nav.home). Ids are useful for finding a string in the editor and for dictionaries, but they are labels, not identity.

Keeping ids out of the hash has two consequences that are worth the trade:

  • Identical text is translated once. Forty “Cancel” buttons across your app are one unit, one translation and one review.
  • Renaming an id costs nothing. Reorganize your dictionary and no translation is lost.

When the same text needs different translations in different places, that is what context is for. “Save” on a button and “Save” as a noun in a football app are different units as soon as one of them has a context, because context is hashed.

Consequences of content addressing

  • Editing source text creates a new unit. Fix a typo and the old hash is orphaned while a new one is translated. This is intentional: the translation of the old sentence is not necessarily right for the new one. The service caches by hash and locale, so reverting the text brings the old translation straight back.
  • Stale entries are harmless. A translation map may contain hashes your code no longer uses. They cost bytes, not correctness. Delete unused strings in the editor or with DELETE /strings/:hash when you want to tidy up.
  • Runtime and build must agree. The CLI computes the hash from your source at build time and the library computes it from the rendered JSX at run time. They match because both use the same serialization rules.
  • Collisions are not a practical concern. 128 bits of SHA-256 is far beyond what any project’s string count could collide in.

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

Type to search the documentation.