Nested ICU Messages

Combine plural, select, and formatting arguments in nested ICU messages. Learn nesting rules, depth limits, and strategies for keeping complex messages translatable.

Advanced

Detailed Explanation

Nesting ICU Message Arguments

ICU MessageFormat supports nesting arguments inside the cases of plural, select, and selectordinal blocks. This enables complex messages that adapt to multiple variables simultaneously.

Basic Nesting: Select + Plural

{gender, select,
    male {He has {count, plural,
        =0 {no photos}
        one {# photo}
        other {# photos}
    }}
    female {She has {count, plural,
        =0 {no photos}
        one {# photo}
        other {# photos}
    }}
    other {They have {count, plural,
        =0 {no photos}
        one {# photo}
        other {# photos}
    }}
}

With gender="male" and count=5: "He has 5 photos" With gender="other" and count=1: "They have 1 photo"

Nesting with Variables

Cases can contain both nested arguments and simple variable placeholders:

{gender, select,
    male {{name} shared his {count, plural, one {post} other {# posts}}}
    female {{name} shared her {count, plural, one {post} other {# posts}}}
    other {{name} shared their {count, plural, one {post} other {# posts}}}
}

Triple Nesting

While possible, triple nesting should be avoided for translatability:

{role, select,
    admin {{gender, select,
        male {He ({name}) deleted {count, plural, one {# file} other {# files}}}
        other {They ({name}) deleted {count, plural, one {# file} other {# files}}}
    }}
    other {{name} deleted {count, plural, one {# file} other {# files}}}
}

Best Practices for Nesting

  1. Limit to two levels: Deeper nesting makes translation nearly impossible
  2. Put the varying element on the outside: If gender affects the whole sentence, make select the outermost argument
  3. Duplicate is acceptable: It is better to repeat similar text in each case than to create fragmented messages that translators cannot understand
  4. Consider splitting: If a message needs three or more nested arguments, consider splitting it into separate messages

Anti-Pattern: Over-Fragmentation

Do NOT try to minimize duplication by splitting sentences:

// BAD: Fragments cannot be translated naturally
{gender, select, male {He} female {She} other {They}} has {count, plural, one {# item} other {# items}}.

This breaks because "has" should be "have" when gender is "other" (They have), and in many languages the entire sentence structure changes.

// GOOD: Full sentence in each case
{gender, select,
    male {He has {count, plural, one {# item} other {# items}}.}
    female {She has {count, plural, one {# item} other {# items}}.}
    other {They have {count, plural, one {# item} other {# items}}.}
}

Message Complexity Metrics

Keep these limits in mind:

  • Simple messages: 0 nesting levels, 1-2 variables
  • Moderate messages: 1 nesting level, 2-3 variables
  • Complex messages: 2 nesting levels, 3-4 variables (max recommended)
  • Too complex: 3+ nesting levels -- split into separate messages

Use Case

i18n architects designing message patterns for complex UI scenarios like social notifications, e-commerce order summaries, or activity feeds that must adapt to multiple dynamic factors simultaneously.

Try It — ICU Message Format Tester

Open full tool