Pluralizer

Get it… via NuGet (the easiest way to get your hands on .net assemblies). The package is called “Pluralizer” and it’s up at the NuGet official package source.

PM> get-package Pluralizer -remote |fl
PM> install-package Pluralizer

Alternatively, you can simply download it manually.

Pluralizer is a small library to make simple string formatting easier. It allows you to use one string to express two variations of a sentence that are dependent on a numeric value.

Or, if code makes you feel more comfortable, consider this simple definition:

var myPluralizer = new Pluralizer();
var str = "There {is} {_} {person}.";

var single = myPluralizer.Pluralize(str, 1);
Assert.AreEqual(“There is 1 person.”, single);

var several = myPluralizer.Pluralize(str, 47);
Assert.AreEqual(“There are 47 people.”, several);

It does more than that too. You can use several distinguishing numbers, define specific singular/plural word variations, format numbers, etc. Here’s another example, this time at the complex end of things (just to show off a bit).


var target = new Pluralizer();
string sentence = "There {0|is|isn't} one person and there {0|he} {goes|go}. The other {1|person|team} isn't, so {1|he|they} will have to pay {2|_:C}.";
double[] numbers = new double[] { 1, 3, 4.5284 };

string actual = target.Pluralize(format, numbers);
Assert.AreEqual(“There is one person and there he goes. The other team isn’t, so they will have to pay $4.53.”, actual);

Read More