How to internationalize Enum values?

Asked

Viewed 446 times

4

I have the following code in 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:

Arquivo RESX

I copied this Helper from someone on the internet:

public static MvcHtmlString EnumDropDownListFor<TModel, TProperty, TEnum>(
                this HtmlHelper<TModel> htmlHelper,
                Expression<Func<TModel, TProperty>> expression,
                TEnum selectedValue)
    {
        IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
                                    .Cast<TEnum>();
        IEnumerable<SelectListItem> items = from value in values
                                            select new SelectListItem()
                                            {
                                                Text = value.ToString(),//Here
                                                Value = value.ToString(),
                                                Selected = (value.Equals(selectedValue))
                                            };

        return SelectExtensions.DropDownListFor(htmlHelper, expression, items);
    }

Where the comment "Here" is where I need to put the value that is in the internationalization file. I have tried several other options and so far have not succeeded. How I do this?

1 answer

3

In my projects, I implement a Helper (a static class) and call a function that does the corresponding translation, returning the string correspondent of the file of Resource.

In the example below, I implemented a Helper for frequency of payment:

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 the Dropdown, stays:

IEnumerable<SelectListItem> items = from value in values
    select new SelectListItem()
    {
        Text = YourHelper.GetDescription(value),
        Value = value.ToString(),
        Selected = (value.Equals(selectedValue))
    };

As in this case you are implementing an extension of Dropdown generic, possibly you will have to implement a solver of Helpers. There the Helpers cannot be static.

First set an Interface for all of them:

namespace MeuProjeto.Interfaces
{
    interface IHelper<TEnum>
    {
        String GetDescription(TEnum enum);
    }
}

Your Helpers will have to implement this interface.

Solver:

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();
                ...
        }
    }
}

There the 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))
    };
  • Thanks Gypsy, went to try to implement and then I give you a return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.