1

I have this enum of countries:

public enum Country
    {
        [Display(Description ="Netherlands", ResourceType = typeof(MyResource))]
        Netherlands = 0,

        [Display(Description = "Germany", ResourceType = typeof(MyResource))]
        Germany = 1,

        [Display(Description = "Belgium", ResourceType = typeof(MyResource))]
        Belgium = 2,

        [Display(Description = "Luxembourg", ResourceType = typeof(MyResource))]
        Luxembourg = 3,

        [Display(Description = "France", ResourceType = typeof(MyResource))]
        France = 4,

        [Display(Description = "Spain", ResourceType = typeof(MyResource))]
        Spain = 5
    }

And this is an extension method to display the enums in a MultiSelectList:

public static MvcHtmlString MultiSelectBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
        {
            return htmlHelper.ListBoxFor(expression, selectList, new { @class = "chzn-select", data_placeholder = Form.MultiSelect });
        }

This MultiSelectList has the 'chosen' style. See this site for more info

This all worked fine when I didn't need it to support more languages etc.

How can I make this work with localization?

Yustme
  • 6,125
  • 22
  • 75
  • 104

1 Answers1

1

You can implement a description attribute.

public class LocalizedDescriptionAttributre : DescriptionAttribute
{
     private readonly string _resourceKey;
    private readonly ResourceManager _resource;
    public LocalizedDescriptionAttributre(string resourceKey, Type resourceType)
    {
        _resource = new ResourceManager(resourceType);
        _resourceKey = resourceKey;
    }

    public override string Description
    {
        get
        {
            string displayName = _resource.GetString(_resourceKey);

            return string.IsNullOrEmpty(displayName)
                ? string.Format("[[{0}]]", _resourceKey)
                : displayName;
        }
    }
}

public static class EnumExtensions
{
    public static string GetDescription(this Enum enumValue) 
    {
        FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return enumValue.ToString();
    }
}

Define it like this:

public enum Roles
{
    [LocalizedDescription("Administrator", typeof(Resource))]
    Administrator,
...
}

And use it like this:

var roles = from RoleType role in Enum.GetValues(typeof(RoleType))
                    select new
                    {
                        Id = (int)role,
                        Name = role.GetDescription()
                    };
 searchModel.roles = new MultiSelectList(roles, "Id", "Name");
eluxen
  • 1,087
  • 8
  • 12