Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pluralize words/strings?

The goal

I want to pluralize words with Razor Engine of C#.Net. I'm using MVC 4.

The problem

I have the following:

<button class="button light-blue filled compare float-right" 
 title="This product is available in @Model["NumberOfMarketsThatHaveThisProduct"]
   market(s)">
   Compare
</button>

I do not want to use "market(s)", but yes "market" or "markets".

What I already have tried

<button class="button light-blue filled compare float-right" 
 title="This product is available in @Model["NumberOfMarketsThatHaveThisProduct"]
   @if((int)@Model["NumberOfMarketsThatHaveThisProduct"] == 1)
   {
      @: market
   } else {
      @: markets
   }">
   Compare
</button>

But I do not feel comfortable doing it.

What I have to do?

like image 351
Guilherme Oderdenge Avatar asked Jun 13 '13 18:06

Guilherme Oderdenge


3 Answers

You can use logic:

market@(someNumber == 1 ? "" : "s")
like image 125
SLaks Avatar answered Sep 30 '22 15:09

SLaks


A much better approach is to create a custom HTML Helper that will do the pluralization correctly using .NET 4's PluralizationService (in System.Data.Entity.Design.PluralizationServices namespace - reference the System.Data.Entity.Designassembly) which is also used by EF6 to pluralize table names.

The Razor helper looks like this:

namespace CustomHelpers
{
    public static class CustomHelpers
    {
        public static MvcHtmlString Pluralize(this HtmlHelper htmlHelper,
            string source)
        {
            var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
            var plural = serv.Pluralize(source);

            return MvcHtmlString.Create(plural);
        }
    }
}

You can easily use this helper in Razor with the following syntax:

@using CustomHelpers

<div class="jumbotron">
    <h1>Hi @Html.Pluralize("person")</h1>
</div>

As you can imagine it will correctly pluralize Person to People, Market to Markets and many other words, since it uses a pluralization dictionary inside. That's much better than using some custom pluralization code.

like image 44
Faris Zacina Avatar answered Sep 30 '22 16:09

Faris Zacina


The most "ASP.NET-MVC-esque" way is to use a display template:

@model int

@if (Model == 1)
{
    @String.Format("{0} market", Model)
}
else
{
    @String.Format("{0} markets", Model)
}

Put this in your DisplayTemplates folder and call it "Market.cshtml." Then, in your model, do:

[UIHint("Market")]
public int NumberOfMarketsThatHaveThisProduct { get; set; }

And in your view:

@Html.DisplayFor(m => m.NumberOfMarketsThatHaveThisProduct)

This approach can be very easily translated by modifying your display template to use local resources as and when you need it to.

This will be neater than doing it inline in the view if you have to repeat this a lot. If it's a one-off, you might find it overkill.

like image 20
Ant P Avatar answered Sep 30 '22 14:09

Ant P