Created
November 15, 2010 15:15
-
-
Save davidwhitney/700453 to your computer and use it in GitHub Desktop.
Html Helper extension for pluralization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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