How to internationalize Enum values?

4

I have the following code in the Model:

public enum Lista 
{
    [Display(Name = "Lista_regular", ResourceType = typeof(Mensagem))]
    Regular = 0,
    [Display(Name = "Lista_irregular", ResourceType = typeof(Mensagem))]
    Irregular = 1 
}

In the internationalization file (.resx) I have:

Icopiedthishelperfromsomeoneontheinternet:

publicstaticMvcHtmlStringEnumDropDownListFor<TModel,TProperty,TEnum>(thisHtmlHelper<TModel>htmlHelper,Expression<Func<TModel,TProperty>>expression,TEnumselectedValue){IEnumerable<TEnum>values=Enum.GetValues(typeof(TEnum)).Cast<TEnum>();IEnumerable<SelectListItem>items=fromvalueinvaluesselectnewSelectListItem(){Text=value.ToString(),//HereValue=value.ToString(),Selected=(value.Equals(selectedValue))};returnSelectExtensions.DropDownListFor(htmlHelper,expression,items);}

Whereyouhavethecomment"Here" is where I need to put the value that is in the internationalization file. I've tried several other options and so far I have not been successful. How do I do this?

    
asked by anonymous 17.04.2014 / 04:06

1 answer

3

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))
    };
    
17.04.2014 / 04:20