Plural formatter
Choose a word form based on a count, following the pluralization rules of the active culture.
Read time 3 minutesLast updated 6 days ago
Choose a word form based on a count, following the pluralization rules of the active culture.
Languages handle plurals differently: English has two forms ("hour" and "hours"), some languages have one, and others have several. The Plural formatter picks the right form by following the Unicode Common Locale Data Repository (CLDR) plural rules. Apply it explicitly with the name , or omit the name to apply it implicitly to a number or a collection.
pluralAnatomy of the plural formatter syntax.
CLDR defines these plural categories: , , , , , and . Provide one format per form the culture uses, in CLDR order. The formatter supports cardinal rules.
zeroonetwofewmanyotherThe formatter determines which rules to apply from the active culture: the CultureInfo passed to , or when none is given. Override the culture with the options in parentheses, for example to force English plural rules.
Smart.FormatCultureInfo.CurrentUICulture(en)// "I have 10 apples" Smart.Format(new CultureInfo("en"), "I have {0:plural:an apple|{} apples}", 10); // "1 банан" Smart.Format(new CultureInfo("ru"), "{0} {0:plural:банан|{} банана|{} бананов}", 1);
An empty placeholder reuses the current value, so you can repeat the count inside a form.
{}The formatter requires at least two forms when applied explicitly. The number of forms you provide must match what the selected rule expects; a mismatch raises a formatting error.
Smart String | Argument | Result |
|---|---|---|
| | I have an apple |
| | I have 10 apples |
| | 2 apples |
You can also pass an : the formatter uses its item count as the plural value. This lets one argument drive both a plural and a list:
IEnumerable// "The following people are impressed: bob, and alice." Smart.Format("The following {0:plural:person is|people are} impressed: {0:list:{}|, |, and}.", new[] { "bob", "alice" });
Custom plural rules
Provide a as the format provider to replace the rules for a single call without changing the defaults elsewhere. This is useful when a scenario needs fewer or different forms than the culture's standard rules.
CustomPluralRuleProvider