2

I have dropdown, which is filled from the Enum. My Solution project consists of two parts: Domain and UI projects. And this enum is a part of domain model and so it is placed inside Domain project.

public enum ActivityStatus
    {
        InProgress = 0,
        Completed = 1,
        Freezed = 2,
        NotStarted = 3,
        None = 4
    }

I want to localise dropdown content on UI with RESX files. I looked at some solutions, and they proposed providing custom attributes on Enum fields. But I think that localisation is out of scope of my domain model, so i want to have these attributes here. Is there a way to have a localisation on UI for me?

2
  • 1
    Can you use attributes on the definition of your enum? Commented Jul 1, 2013 at 19:45
  • Felipe, I don't want to have attributes on my enum, since enum is a part of a domain model. I want to use somehow this attributes on the UI part. Commented Jul 3, 2013 at 12:16

2 Answers 2

4

I also have made some enumerations localized in my projects, so, I have a class like this, to create a annotation in enums:

public class LocalizedEnumAttribute : DescriptionAttribute
{
    private PropertyInfo _nameProperty;
    private Type _resourceType;

    public LocalizedEnumAttribute(string displayNameKey)
        : base(displayNameKey)
    {
    }

    public Type NameResourceType
    {
        get
        {
            return _resourceType;
        }
        set
        {
            _resourceType = value;

            _nameProperty = _resourceType.GetProperty(this.Description, BindingFlags.Static | BindingFlags.Public);
        }
    }

    public override string Description
    {
        get
        {
            //check if nameProperty is null and return original display name value
            if (_nameProperty == null)
            {
                return base.Description;
            }

            return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
        }
    }
}

I also have a EnumHelper class to use on my projects to create dictionaries of enums values localized:

public static class EnumHelper
{
    // get description data annotation using RESX files when it has
    public static string GetDescription(Enum @enum)
    {
        if (@enum == null)
            return null;

        string description = @enum.ToString();

        try
        {
            FieldInfo fi = @enum.GetType().GetField(@enum.ToString());

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

            if (attributes.Any())
                description = attributes[0].Description;
        }
        catch
        {
        }

        return description;
    }

    public static IDictionary<TKey, string> GetEnumDictionary<T, TKey>()
        where T : struct 
    {
        Type t = typeof (T);

        if (!t.IsEnum)
            throw new InvalidOperationException("The generic type T must be an Enum type.");

        var result = new Dictionary<TKey, string>();

        foreach (Enum r in Enum.GetValues(t))
        {
            var k = Convert.ChangeType(r, typeof(TKey));

            var value = (TKey)k;

            result.Add(value, r.GetDescription());
        }

        return result;
    }
}

public static class EnumExtensions
{
    public static string GetDescription(this Enum @enum)
    {
        return EnumHelper.GetDescription(@enum);
    }
}

With this class you can use on your enum:

public enum ActivityStatus 
{ 
    [LocalizedEnum("InProgress", NameResourceType = typeof(Resources.Messages))]
    InProgress = 0, 

    [LocalizedEnum("Completed", NameResourceType = typeof(Resources.Messages))]
    Completed = 1, 

    [LocalizedEnum("Freezed", NameResourceType = typeof(Resources.Messages))]
    Freezed = 2, 

    [LocalizedEnum("NotStarted", NameResourceType = typeof(Resources.Messages))]
    NotStarted = 3, 

    [LocalizedEnum("None", NameResourceType = typeof(Resources.Messages))]
    None = 4 
}

And to create a combo box of this, you can use on the controller of asp.net mvc, something like this:

var data = EnumHelper.GetEnumDictionary<ActivityStatus, int>();
Sign up to request clarification or add additional context in comments.

1 Comment

Using the generic method 'EnumHelper.GetEnumDictionary<T, TKey>()' requires 2 type arguments. Are you missing something?
2

I guees you can use HttpContext.GetGlobalResourceObject() for getting localized strings for enum names:

// here you get a list of localized strings from `SiteResources.resx` where the keys of strings present by enum names
var names = (Enum.GetNames(typeof(ActivityStatus)).Select(x => HttpContext.GetGlobalResourceObject("SiteResources", x).ToString()).ToList();

1 Comment

Thanks for you answer. I want to be free of using App_LocalResources or App_GlobalResourcers folders for Unit Testing purpose.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.