Skip to content

Plurals and selects

A namespace’s format picks ICU, MF2 or your own; all compile to the same functions. Live, ICU beside MF2:

Loading…

{ "items": "{count, plural, =0 {Your cart is empty} one {# item} other {# items}}" }

Exact values (=0, 0) beat categories (one, from Intl.PluralRules), which beat the catch-all.

{ "rank": "You finished {place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}" }

A locale with one ordinal form needs no select: German writes Du bist {$place :integer}. geworden.

{
"delivery": "{method, select, pickup {Collect it from {store}} courier {{courier} delivers it on {day, date, long}} other {Ships soon}}"
}

When one :string selector decides the params, each branch types its own:

t.shop.delivery({ method: 'pickup', store: 'Milano Centrale' }); // store only
t.shop.delivery({ method: 'courier', courier: 'Rita', day: new Date() }); // no store
t.shop.delivery({ method: 'pickup' }); // error: store is missing
declare const method: string; // may hold any key
t.shop.delivery({ method, store: '…', courier: '…', day: new Date() }); // needs every branch's

The generated type:

delivery(
params:
| { method: 'pickup'; store: string | number }
| { method: 'courier'; courier: string | number; day: Date | number }
| { method: string; store: string | number; courier: string | number; day: Date | number },
): string;

When every branch needs the same params, a call takes the keys, other and any string, but no other literal:

{ "reply": "{who, select, female {She} male {He} other {They}} replied" }
t.shop.reply({ who: 'female' });
t.shop.reply({ who: user.gender }); // a string
t.shop.reply({ who: 'femal' }); // error: not "female", "male" or "other"

An enum or a union with members that mean other, or a type parameter S extends string, is passed as any string:

t.shop.reply<string>({ who: user.role });