Plurals and selects
A namespace’s format picks ICU, MF2 or
your own; all compile to the same functions. Live, ICU beside MF2:
Loading…
ICU
MF2
Plurals
Section titled “Plurals”{ "items": "{count, plural, =0 {Your cart is empty} one {# item} other {# items}}" }{ "items": ".input {$count :number}\n.match $count\n0 {{Your cart is empty}}\none {{{$count} item}}\n* {{{$count} items}}"}Exact values (=0, 0) beat categories (one, from Intl.PluralRules), which beat the catch-all.
Ordinals
Section titled “Ordinals”{ "rank": "You finished {place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}" }{ "rank": ".input {$place :number select=ordinal}\n.match $place\none {{You finished {$place}st}}\ntwo {{You finished {$place}nd}}\nfew {{You finished {$place}rd}}\n* {{You finished {$place}th}}"}A locale with one ordinal form needs no select: German writes Du bist {$place :integer}. geworden.
Selects, typed per branch
Section titled “Selects, typed per branch”{ "delivery": "{method, select, pickup {Collect it from {store}} courier {{courier} delivers it on {day, date, long}} other {Ships soon}}"}{ "delivery": ".input {$method :string}\n.match $method\npickup {{Collect it from {$store}}}\ncourier {{{$courier} delivers it on {$day :date style=long}}}\n* {{Ships soon}}"}When one :string selector decides the params, each branch types its own:
t.shop.delivery({ method: 'pickup', store: 'Milano Centrale' }); // store onlyt.shop.delivery({ method: 'courier', courier: 'Rita', day: new Date() }); // no storet.shop.delivery({ method: 'pickup' }); // error: store is missing
declare const method: string; // may hold any keyt.shop.delivery({ method, store: '…', courier: '…', day: new Date() }); // needs every branch'sThe 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;Selects with the same params
Section titled “Selects with the same params”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 stringt.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 });