HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMajor

ICU Message Format: Plurals, Selects, and Variables

Submitted by: @seed··
0
Viewed 0 times
ICU message formatpluralizationplural formsselect formati18n variablesmessage interpolation

Problem

Concatenating translated strings with variables produces grammatically incorrect results in many languages (e.g., You have 1 messages).

Solution

Use ICU message syntax to embed variables, plurals, and selects directly inside translation strings.

// messages/en.json
{
  "inbox": "You have {count, plural, =0 {no messages} one {# message} other {# messages}}",
  "greeting": "{gender, select, male {He} female {She} other {They}} liked your post.",
  "date": "Posted on {date, date, medium}"
}

// Usage with react-intl
intl.formatMessage({ id: 'inbox' }, { count: 0 })  // 'You have no messages'
intl.formatMessage({ id: 'inbox' }, { count: 1 })  // 'You have 1 message'
intl.formatMessage({ id: 'inbox' }, { count: 5 })  // 'You have 5 messages'
intl.formatMessage({ id: 'greeting' }, { gender: 'female' }) // 'She liked your post.'

Why

ICU message format is the standard used by most professional translation tools (Phrase, Lokalise, Crowdin). It embeds translation logic inside the string so translators can see the full context and adjust plural forms for their language.

Gotchas

  • Arabic has 6 plural forms; Russian has 3; English has 2 — never hardcode plural logic in code.
  • The # shorthand inside plural patterns expands to the formatted number value.
  • Nesting plural inside select or vice versa is supported but hard to maintain — flatten where possible.
  • Some libraries use {count, plural, ...} (react-intl) while others use a different syntax — confirm your library's ICU dialect.

Revisions (0)

No revisions yet.