Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Enum Display Extension

, 30 Oct 2011
Rate this:
Please Sign up or sign in to vote.
In response to Adrian Cole's comment to the OP - due to living and working in a country where there are four official languages, plus English as a de facto fifth, I've encountered this before.Provided you have the corresponding .resx files embedded in the same assembly as the enum and your...

In response to Adrian Cole's comment to the OP - due to living and working in a country where there are four official languages, plus English as a de facto fifth, I've encountered this before.

Provided you have the corresponding .resx files embedded in the same assembly as the enum and your enum values correspond to the resource names in those files, you can do the following:

Create a bog-standard enum without attributes:

//standard enum
public enum EmployeeType
{
    Boss,
    AdministrativeAssistant,
    Janitor,
    StandardEmployee
}

And create an extension method that uses the enum value as the key for the .NET Framework to look up the appropriate language string:

//Enum Extension class
public static class EnumExtensions
{
    //Gets the localized string representation of the enum value
    public static string ToLocalizedString(this Enum value)
    {
        var resourceManager = new ResourceManager(Type.GetType(
            string.Format("{0}.{1}Strings", 
            value.GetType().Namespace, value.GetType().Name)));
 
        var localizedString = resourceManager.GetString(value.ToString());
 
        return string.IsNullOrEmpty(localizedString) ? value.ToString() : localizedString;
    }
}

Then all you need to do to get a localized string in place of the enum value is:

var localized = EmployeeType.Boss.ToLocalizedString();

And for converting a localized string back to the enum, you call an extension method on the string:

//string extension method
public static T FromLocalizedString<T>(this string localizedString)
{
   var t = typeof (T);
 
   var resourceType = Type.GetType(string.Format("{0}.{1}Strings", 
                                   t.Namespace, t.Name));
 
   var enumObject = resourceType.GetProperties()
       .Where(p => p.GetValue(null, null) != null && 
              p.GetValue(null, null).ToString() == localizedString)
       .DefaultIfEmpty(null)
       .FirstOrDefault();
 
   return enumObject == null ? default(T) : (T)Enum.Parse(t, enumObject.Name);
}

like this:

var boss = "Chef".FromLocalizedString<EmployeeType>();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jim lahey
Software Developer (Senior)
United Kingdom United Kingdom
First batch file in 1987
 
Messed around with the Bullfrog C++ Libraries for Syndicate in 1994
 
Web Developer since 2000
 
.net Developer since 2001
 
MCTS: Microsoft® .NET Framework 2.0 - Web-based Client Development
 
MCTS: Web Applications Development with Microsoft .NET Framework 4

Comments and Discussions

 
GeneralReason for my vote of 5 5. End of story. PinmemberOshtri Deka29-Feb-12 21:08 
GeneralReason for my vote of 5 Another great implementation! PinmemberFernandaUY21-Dec-11 14:01 
GeneralReason for my vote of 5 Wonderful! PinmemberPablo Aliskevicius16-Nov-11 19:51 
GeneralRe: A wonderful comment too! Thanks! Pinmemberjim lahey17-Nov-11 2:01 
GeneralIf you use the DescriptionAttribute for the enums as part of... Pinmembergerm1331-Oct-11 22:26 
GeneralRe: I have no idea. You'll have to ask the OP. Pinmemberjim lahey1-Nov-11 0:49 
GeneralReason for my vote of 5 Very nice implementation. PinmemberMarcus Kramer27-Oct-11 4:47 
GeneralRe: Thankyou! Pinmemberjim lahey27-Oct-11 4:50 
GeneralNice modification to it :-) PinmemberCollin Jasnoch26-Oct-11 3:20 
GeneralRe: Thanks! Pinmemberjim lahey26-Oct-11 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 30 Oct 2011
Article Copyright 2011 by jim lahey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid