In my projects, I implement a Helper
(a static class) and call a function that does the corresponding translation, returning the corresponding string of the Resource file.
In the example below, I have implemented Helper
for payment frequency:
using MeuProjeto.Enums;
using MeuProjeto.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MeuProjeto.Helpers
{
public static class BillingPeriodHelper
{
public static string GetBillingPeriodName(BillingPeriod period) {
switch (period) {
case BillingPeriod.Weekly:
return Language.Weekly;
case BillingPeriod.Fortnightly:
return Language.Fortnightly;
case BillingPeriod.Monthly:
return Language.Monthly;
case BillingPeriod.Bimonthly:
return Language.Bimonthly;
case BillingPeriod.Trimonthly:
return Language.Trimonthly;
case BillingPeriod.Fourmonthly:
return Language.Fourmonthly;
case BillingPeriod.Sixmonthly:
return Language.Sixmonthly;
default:
return Language.NotDefined;
}
}
}
}
In this case, to do DropDown , it is:
IEnumerable<SelectListItem> items = from value in values
select new SelectListItem()
{
Text = YourHelper.GetDescription(value),
Value = value.ToString(),
Selected = (value.Equals(selectedValue))
};
Since in this case you are implementing a generic DropDown extension, you may have to implement a Helpers resolver. There Helpers can not be static.
First set an Interface for all of them:
namespace MeuProjeto.Interfaces
{
interface IHelper<TEnum>
{
String GetDescription(TEnum enum);
}
}
Your Helpers will need to implement this interface.
Resolver:
namespace MeuProjeto.Resolvers
{
public static class HelperResolver
{
public static IHelper Resolve(Type type)
{
switch (type)
{
case Helper1.GetType():
return new Helper2();
case Helper1.GetType():
return new Helper2();
...
}
}
}
Then DropDown would look like this:
IHelper helper = HelperResolver.Resolve(/* Pensar em como passar o tipo para resolver */);
IEnumerable<SelectListItem> items = from value in values
select new SelectListItem()
{
Text = helper.GetDescription(value),
Value = value.ToString(),
Selected = (value.Equals(selectedValue))
};