Skip to content

Instantly share code, notes, and snippets.

@davidwhitney
Created November 15, 2010 15:15
Show Gist options
  • Save davidwhitney/700453 to your computer and use it in GitHub Desktop.
Save davidwhitney/700453 to your computer and use it in GitHub Desktop.
Html Helper extension for pluralization
using System;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
namespace Extensions
{
public static class HtmlHelperExtensions
{
public static LanguageExtensions Language(this HtmlHelper helper)
{
return new LanguageExtensions();
}
}
}
using System.Data.Entity.Design.PluralizationServices;
using System.Threading;
namespace Extensions
{
public class LanguageExtensions
{
private readonly PluralizationService _pluralizationService;
public LanguageExtensions()
{
_pluralizationService = PluralizationService.CreateService(Thread.CurrentThread.CurrentCulture);
}
public string Pluaralize(string singular)
{
return _pluralizationService.Pluralize(singular);
}
public string Singularize(string plural)
{
return _pluralizationService.Singularize(plural);
}
public string CorrectTenseForQuantity(int value, string word)
{
switch (value)
{
case 0:
return _pluralizationService.Pluralize(word);
case 1:
return _pluralizationService.Singularize(word);
default:
return _pluralizationService.Pluralize(word);
}
}
}
}
<%@ Import Namespace="Extensions" %>
<%=Html.Language().CorrectTenseForQuantity(Model.Quantity, "word") %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment