Pluralize or singularize names

When generating code for the Entity Framework with T4, you sometimes have to put a name in singular or in plural (eg GetCustomer, GetAllCustomers). This can be complicated to implement because of the numerous exceptions (ex: man, men, child, children, …). But once again Microsoft offers us a tool (at least for the English language): PluralizationService. You can use it very easily:

PluralizationService pluralizationService = PluralizationService.CreateService(new CultureInfo("en"));
Console.WriteLine(pluralizationService.Pluralize("man"));

Note that there are other free libraries that do the same thing. For example, CodeFluent Runtime allows you to manage French and English with the class GrammaticalNumberConverter.

Enjoy this blog? Buy Me A Coffee Donate with PayPal

Leave a reply