Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PluralizationServices changing Status to Statu

Tags:

c#

pluralize

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;
}
like image 503
M Kenyon II Avatar asked Jan 03 '17 16:01

M Kenyon II


1 Answers

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.

like image 173
RB. Avatar answered Nov 12 '22 15:11

RB.