Pluralize and singularize nouns
Orientation
Use Pluralize or Singularize when the application has an English noun but not a count. Use ToQuantity when the count and noun belong together. Humanizer’s vocabulary handles common irregular and uncountable English nouns; an application can add domain-specific rules before it begins formatting text.
Example
This isolated executable covers a built-in English irregular noun, a quantity written as words, one application-specific irregular pair, and caller-authored Polish plural forms:
using System.Globalization;
using Humanizer;
var culture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
AssertEqual("people", "person".Pluralize());
AssertEqual("person", "people".Singularize());
AssertEqual("two people", "person".ToQuantity(2, ShowQuantityAs.Words));
Vocabularies.Default.AddIrregular("cactoid", "cactoidae", matchEnding: false);
AssertEqual("cactoidae", "cactoid".Pluralize());
AssertEqual("cactoid", "cactoidae".Singularize());
var polishForms = new PluralizationForms(
singular: "plik",
other: "pliku",
one: "plik",
few: "pliki",
many: "plików");
AssertTrue(polishForms.TryPluralize(5m, CultureInfo.GetCultureInfo("pl"), out var polish));
AssertEqual("plików", polish);
AssertTrue(polishForms.TrySingularize("pliki", out var singular));
AssertEqual("plik", singular);
Console.WriteLine("people; two people; cactoidae; plików; plik");
static void AssertEqual(string expected, string? actual)
{
if (actual != expected)
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}
static void AssertTrue(bool value)
{
if (!value)
throw new InvalidOperationException("Expected success.");
}
Tell Humanizer what the input means
Pluralize(inputIsKnownToBeSingular: false) avoids pluralizing an input that may already be plural. The matching Singularize flag avoids a second singularization. Singularize(skipSimpleWords: true) specifically skips the simple trailing-s rule for a one-word input; irregular and other configured rules still apply.
ToQuantity accepts:
ShowQuantityAs.Nonefor only the correctly inflected noun.ShowQuantityAs.Numericfor a formatted numeric count.ShowQuantityAs.Wordsfor a localized number word followed by the noun.
Numeric format strings and providers control the displayed number. They do not turn the English inflector into a localized noun engine.
Pluralize a localized noun for a count
On Humanizer 4, PluralizationForms.TryPluralize selects a
caller-supplied form using the explicit culture's Unicode CLDR decimal cardinal
rule. Every supported culture supplies this rule. If the selected form was not
supplied, the operation returns false; it does not guess morphology or fall
back to English. Use PluralizationForms.Invariant for a word that never
changes, or explicitly supply equal forms when categories share spelling. The
encoded decimal scale is significant, so a culture can select different forms
for 1m and 1.0m.
CLDR categories are count-selection tags, not complete grammatical categories.
PluralizationForms models one noun's singular and plural forms. Call
TrySingularize on that form set to resolve any exact authored form back to
its singular spelling. Matching is Unicode NFC-normalized, ordinal, and
case-sensitive.
The selector follows the Unicode CLDR plural-rule contract and its decimal operands.
Extend the vocabulary
Vocabularies.Default supports AddIrregular, AddUncountable, AddPlural, and AddSingular. Prefer an irregular or uncountable entry over a broad regular expression. Set matchEnding: false when a rule must match the entire noun instead of every word ending with the same letters.
In Humanizer 4, AddAcronym uses the same vocabulary to preserve the
canonical casing of a domain acronym during string humanization. Registration
is case-insensitive, but the registered spelling controls the output. Acronyms
must contain letters only.
Pitfall
Vocabulary changes, including acronym registrations in Humanizer 4, are
process-global. Register domain words once during startup and test them in an
isolated process; do not add rules per request. ToQuantity treats exactly 1
and -1 as singular. Fractional values, NaN, and infinity use the plural
form.
The existing Pluralize, Singularize, vocabulary, and ToQuantity APIs
continue to pluralize and singularize English nouns. Every supported culture
supplies the cardinal rule used by PluralizationForms.
Version notes
Core inflection is available across the documented release set. Individual
vocabulary rules and overloads have changed, and early Humanizer 3.0.x
temporarily lacked quantity overloads restored by 3.0.8. The verified source
runs against the selected package. AddAcronym is available on
Humanizer 4.
Culture-explicit PluralizationForms are new in v4.