PluralizationServices changing Status to Statu

1.2k views Asked by At

We have a T4 template using System.Data.Entity.Design.PluralizationServices to handle table names for some model generation.

When we run a table with Status in the name, such as CompanyStatus, the Singularize method returns CompanyStatu. I have some sample code below.

How can I get it to see CompanyStatus correctly?

void Main()
{
    // Sanity checks
    Console.WriteLine(Singularize("Cats"));
    Console.WriteLine(Singularize("Cat"));
    Console.WriteLine(Singularize("Status"));
    // The real issue
    Console.WriteLine(Singularize("CompanyStatus"));
}

static string Singularize(string word)
{
    var singularword = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(System.Globalization.CultureInfo.GetCultureInfo("en-us")).Singularize(word);
    return singularword;
}

This returns:

Cat
Cat
Status
CompanyStatu

I also tried:

static string Singularize(string word)
{
    var pluralizationService = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(System.Globalization.CultureInfo.GetCultureInfo("en-us"));
    ((ICustomPluralizationMapping)pluralizationService).AddWord("companystatus", "companystatuses");
    var singularword = pluralizationService.Singularize(word);      
    return singularword;
}
1

There are 1 answers

0
RB. On

Humanizer will do this out-of-the box if that's an option?

// Note that you have to set the "inputIsKnownToBePlural" flag to false,
// or it will behave the same as PluralizationServices.

Console.WriteLine("Statuses".Singularize(inputIsKnownToBePlural: false));
Console.WriteLine("Status".Singularize(inputIsKnownToBePlural: false));
Console.WriteLine("CompanyStatus".Singularize(inputIsKnownToBePlural: false));
Console.WriteLine("CompanyStatuses".Singularize(inputIsKnownToBePlural: false));

Documentation.