Pluralizing Words in C# (.NET) Using PluralizationService
Today I needed to generate a dynamic page title based on a category name. For example:
- My Playlists
- My Libraries
- My Categories
The page receives a categoryID parameter, retrieves the corresponding category object, and then dynamically pluralizes the category name.
Using PluralizationService in .NET
.NET provides a built-in service for pluralization via System.Data.Entity.Design.PluralizationServices.
using System.Data.Entity.Design.PluralizationServices;
....
Category category = Category.GetByID(categoryID);
PluralizationService ps =
PluralizationService.CreateService(new System.Globalization.CultureInfo("en-GB"));
Page.Title = $"My {ps.Pluralize(category.Name)}";
.....
Important Note
You must add a reference to System.Data.Entity.Design in your project.
When to Use This
This approach is useful when:
- Generating dynamic titles
- Building admin dashboards
- Creating REST endpoints with plural resource names
- Displaying category-based collections
Using the built-in PluralizationService avoids writing custom plural rules and handles irregular English nouns correctly.
Comments
Post a Comment