Skip to main content

ICU MessageFormat

ICU MessageFormat is a flexible and powerful syntax designed to express the grammatical nuances of different languages. Its flexibility ensures that your application can handle grammatical variations, making it essential for effective internationalization.

Overview​

Simple text​

Example: Refresh inbox

Variables​

Example: Attachment {name} saved

Plurals​

Using language specific plural forms (one, other):

{count, plural, one {Message} other {Messages}}

Using exact matches for specific counts (=0):

{count, plural, =0 {No messages}
one {# message}
other {# messages}}

Offsetting plural forms:

{count, plural, offset:1
=0 {Nobody read this message}
=1 {Only you read this message}
one {You and # friend read this message}
other {You and # friends read this message}

Select​

{gender, select, male {He replied to your message}
female {She replied to your message}
other {They replied to your message}}

Ordinals​

{count, selectOrdinal, one {#st message}
two {#nd message}
few {#rd message}
other {#th message}}

Custom formatters​

Arguments can also use formatters registered on the i18n instance with i18n.setFormatters. For example, a list formatter based on Intl.ListFormat formats an array of values, with an optional parameter for the list type:

Genres: {genres, list}
Genres: {genres, list, disjunction}

With genres set to ["Rock", "Jazz", "Pop"] in English, this gives "Genres: Rock, Jazz, and Pop" and "Genres: Rock, Jazz, or Pop".

import { i18n } from "@lingui/core";

i18n.setFormatters({
list: (value, { locale, param }) =>
new Intl.ListFormat(locale, {
type: param === "disjunction" || param === "unit" ? param : "conjunction",
}).format(value as string[]),
});

See Also​