ICU messages
How LangSync reads, translates and checks ICU MessageFormat strings in a namespace whose message syntax is icu, with plural forms per target language, quoting, tags and the codes a refused translation gets.
Software strings hold variables, plurals and choices:
{count, plural, one {# file} other {# files}}. Translated word by
word, a string like that breaks: a model translates plural or
other, drops {count}, or keeps English's two forms where Czech needs
four. In a namespace whose message syntax
is icu, LangSync parses every message itself, sends the model whole
sentences without any ICU syntax in them, writes the translated message
back with the target language's plural forms, and checks the result
before it stores anything. What people write in such a namespace is
checked the same way.
Supported syntax
LangSync reads ICU MessageFormat 1, the subset that Paraglide's ICU plugin formats:
| Construct | Example |
|---|---|
| Text | Save changes |
| Argument | Hello {name} |
| Number | {n, number}, {n, number, integer}, percent, currency |
| Date and time | {d, date, short}, {t, time, medium}; styles short, medium, long, full |
| Skeleton | {n, number, ::currency/EUR} (any style starting with ::) |
| Plural | {n, plural, =0 {No files} one {# file} other {# files}} |
| Plural with offset | {n, plural, offset:1 =0 {nobody} one {{name} and # other} other {{name} and # others}} |
| Ordinal | {p, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} |
| Select | {role, select, admin {Administrator} other {Member}} |
| Nesting | plurals and selects inside each other's branches, up to 8 levels |
| Apostrophe quoting | '{' is a literal {, '' is an apostrophe (see Quoting) |
Argument names are letters, digits and _. Plural and ordinal branches
are the CLDR form names (zero, one, two, few, many, other)
or an exact number (=0, =1, …); # in their branches is the number.
Refused with icu_invalid: the choice, spellout, ordinal and
duration argument types, any other style, a plural, ordinal or select
without an other branch, the same branch twice, an exact number in a
select, a quote that is never closed, a { or } that does not pair
up, nesting deeper than 8 levels, and a message over 16 KB.
How a message is translated
Automatic translation of terms (translate missing, a sync's autotranslate, a new term with Translate automatically, a bulk import with Translate automatically) handles an ICU message in four steps.
- Whole sentences. Every combination of branches becomes one
sentence. Text outside a plural or select is part of every sentence,
so the model never translates a fragment such as
# file. - The target language's forms. Each plural is expanded to the
target language's plural forms, plus the source's exact
=nbranches. A form's text comes from the source branch with the same name, else fromother. The model gets a note per sentence naming the form and example numbers for it. A select keeps its branches as they are. - Variables as tokens. Arguments,
#and tags are replaced by tokens (⟦1⟧,⟦2⟧) that the answer must keep. The model never sees ICU syntax. - Reassembled and checked. The translated sentences are put back into one message with the plurals and selects outside and a whole sentence in each branch, the same argument names and offsets, and quoting where ICU would read a character as syntax. The message is parsed again and checked against the source before it is stored.
Example: English to Czech
Source (English):
{count, plural, =0 {No files} one {# file} other {# files}} in {folder}Czech has four plural forms, so LangSync sends five sentences:
| Branch in Czech | Example numbers | Taken from | Sentence the model translates |
|---|---|---|---|
=0 | 0 exactly | =0 | No files in ⟦1⟧ |
one | 1 | one | ⟦1⟧ file in ⟦2⟧ |
few | 2, 3, 4 | other | ⟦1⟧ files in ⟦2⟧ |
many | 0.0, 1.5 | other | ⟦1⟧ files in ⟦2⟧ |
other | 0, 5, 6, 7 | other | ⟦1⟧ files in ⟦2⟧ |
Stored (Czech):
{count, plural, =0 {Žádné soubory v {folder}} one {# soubor v {folder}} few {# soubory v {folder}} many {# souboru v {folder}} other {# souborů v {folder}}}The same source translated into German gets =0, one and other;
into French or Spanish =0, one, many and other; into Polish the
same four forms as Czech. An ordinal (selectordinal) gets the target
language's ordinal forms the same way.
Plural forms per language
The forms come from Unicode CLDR 48. A language code with a region uses
its base language's forms (cs-CZ uses cs). For a language CLDR does
not describe, typically a custom language
with an invented code, LangSync does not know the forms: automatic
translation of a message with a plural or ordinal into it is refused
with plural_forms, and a translation a person writes only needs the
other branch and the source's exact branches.
The 24-sentence bound
One message expands to at most 24 sentences per target language. A
message over it is refused with too_many_variants and is not
translated; split it into smaller messages. The count depends on the
target: two plurals and a select with two branches make 2 × 2 × 2 = 8
sentences for German, but 4 × 4 × 2 = 32 for Czech, which is refused.
Quoting
ICU uses the apostrophe to quote syntax characters. LangSync reads it the way ICU's default mode does:
''is one apostrophe, anywhere.- A single
'directly before{or}(or#inside a plural or ordinal branch) starts quoted text, which runs to the next single'.'{'is a literal{. - Any other single
'is a literal apostrophe, soIt's {n}needs no escaping. - Quoted text that is never closed is refused (
icu_invalid).
When LangSync writes a translated message, it quotes each {, } and
(in a plural branch) # of the text on its own, and doubles an
apostrophe wherever it could start quoting: before one of those
characters, before another apostrophe, and at the end of a piece of
text, where an argument or a # may follow. A French translation
"Ouvrir l'" followed by the argument {folder} is stored as
Ouvrir l''{folder}. Written as l'{folder}, ICU would read
'{folder} as quoted text, and LangSync refuses that message when a
person saves it.
Tags
ICU MessageFormat 1 has no tag syntax: <b>, <link>…</link> and
<br/> are text, as in Paraglide's plugin. LangSync still protects
them. Each tag becomes a token the model must keep, and each sentence
of the translation must have the source sentence's tags, in the same
order and balanced. HTML's void elements (<br>, <img>, <hr>,
<input> and the others) need no closing tag; <link> does, because
messages use <link>…</link> for a link. Character entities such as
& are kept as they are too.
When a translation is not stored
An automatic translation that still fails the checks after the engine's retries is not stored: the translation stays empty, the term's other languages and the other terms are stored as usual, and the refusal is reported with a code. A sync job lists it in its failures with the key and the language; a translate missing run counts it as failed in its email. The next run tries the term again.
| Code | Meaning | Facts |
|---|---|---|
placeholders_changed | The translation's variables differ from the source's: one dropped, added, renamed or translated. In an ICU message, its arguments, plurals or selects. | missing, unexpected |
icu_invalid | The message is not valid ICU MessageFormat 1, or uses what LangSync does not support. | offset |
plural_forms | A plural or ordinal does not have exactly the target language's forms, the source's exact branches and other; a select does not have exactly the source's branches; or LangSync does not know the target language's forms. | argument, missing, unexpected |
tags_changed | A sentence's tags are not the source's, in the same order and balanced. | missing, unexpected |
glossary_term_missing | A glossary term that is not translatable occurs in the source but not, written as it is, in the translation. | |
too_many_variants | The message makes more than 24 sentences for the target language; split it. | |
untranslated | The model answered with the source itself, also when asked again, and gave no reason to keep it (a name, a code, text already in the target language). | |
model_failed | The model gave no usable answer after every retry (empty, far too long, cut off, the source of another text, or the provider failing), or the source is too long or not valid text. |
offset is the character (a Unicode code point, counted from 0) where
the message stops being valid. missing and unexpected list the
forms, arguments or tags that are missing or should not be there, and
argument names the plural or select whose branches are wrong.
placeholders_changed, tags_changed, glossary_term_missing,
untranslated and model_failed apply in a plain namespace too; the
other three only to ICU messages.
What people write is checked too
In an icu namespace, every value a person writes is checked before it
is stored, whichever way it arrives: an edit in the dashboard or over
the API, a new term's source value, a CLI push
and a bulk import.
- The source language's value must be a valid message.
- Any other language's value must be a valid message and hold against the source's current text: the language's plural forms, and per sentence the same arguments and tags. If the source is empty or is not a valid message itself, only the value's syntax is checked.
- An empty value (clearing a translation) is always accepted.
- Glossary terms are not checked. They steer the model; a person may decide otherwise.
A refused write gets:
| Where | Answer |
|---|---|
| Dashboard or API edit | 400 TERM_TRANSLATION_INVALID, with data {reason, offset?, argument?, missing?, unexpected?}. reason is one of the codes above. The dashboard says why in its own words, with the missing or unexpected forms, variables or tags. |
| New term | The same, when its source value is not a valid message. No term is created; the dialog says why. |
| Bulk import | 400 TERM_IMPORT_TRANSLATIONS_INVALID when you start it. The whole file is checked first and nothing is imported; data lists the first 100 problems (row, mark, languageId, reason and its facts) and total. The import dialog shows them by row, key and language. |
CLI push (nrc langsync sync) | The operation fails with the code and the sync goes on with the rest. A refused source value creates no term. The CLI lists it with the key, the language and the code. |
A plain namespace checks none of this.
Related
- Namespaces: set a namespace's message syntax.
- Auto-translate: what the model gets with every text.
- Sync jobs: where refusals are reported.