CodePark/ questions / Plurals & messages
START PRACTICINGPRACTICE
Frontend question · Internationalization › Plurals & messages

What is ICU MessageFormat? Explain this message and why it is better for translators than building the sentence in code.

{gender, select,
  female {{name} shared {count, plural,
    =0 {no photos} one {# photo} other {# photos}} with her team}
  male {{name} shared {count, plural,
    =0 {no photos} one {# photo} other {# photos}} with his team}
  other {{name} shared {count, plural,
    =0 {no photos} one {# photo} other {# photos}} with their team}
}

ICU MessageFormat is a standard syntax for translatable messages. One string holds the whole sentence, with placeholders and rules for every variant. Libraries like react-intl (FormatJS) and many translation tools read it.

The parts in the example:

  • {name} is a simple placeholder for a value.
  • {gender, select, female {…} male {…} other {…}} picks a branch by a value. other is the required fallback.
  • {count, plural, …} picks a branch by the language's plural category.
  • =0 matches the exact number 0, before the plural categories are checked.
  • # is replaced by the number, formatted for the locale.

Why it is better than building the sentence in code:

  • The translator sees the full sentence. Word order changes by language, and the translator can move {name} anywhere.
  • Each language adds the plural branches it needs, such as few and many for Polish, without code changes.
  • Gender and grammar agreement stay inside the translation, where they belong.
  • The code passes only data: { name, gender, count }.

One tip from the example: put whole sentences inside each branch, not small pieces. That gives translators full sentences to work with.

  • ICU MessageFormat keeps a whole sentence and all its variants in one string.
  • {name} is a placeholder, select picks a branch by value and plural picks by plural category.
  • =0 matches an exact number, and # shows the formatted number.
  • Translators can reorder placeholders and add the plural forms their language needs.
  • The code only passes data, so no sentence is built by concatenation.