PluralizationServices将状态更改为Statu

时间:2017-01-03 16:59:24

标签: c# pluralize

我们有一个使用System.Data.Entity.Design.PluralizationServices的T4模板来处理某些模型生成的表名。

当我们在名称中运行Status的表格时,例如CompanyStatusSingularize方法会返回CompanyStatu。我在下面有一些示例代码。

如何才能正确查看CompanyStatus?

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;
}

返回:

  


  猫
  状态
  CompanyStatu

我也尝试过:

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 个答案:

答案 0 :(得分:0)

如果这是一个选项,

Humanizer会开箱即用吗?

// 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