Click here to Skip to main content
Click here to Skip to main content

Enum Display Extension

By , 30 Oct 2011
 

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)

About the Author

jim lahey
Software Developer (Senior)
Switzerland Switzerland
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 5. End of story.memberOshtri Deka29 Feb '12 - 21:08 
Reason for my vote of 5
5.
End of story.
GeneralReason for my vote of 5 Another great implementation!memberFernandaUY21 Dec '11 - 14:01 
Reason for my vote of 5
Another great implementation!
GeneralReason for my vote of 5 Wonderful!memberPablo Aliskevicius16 Nov '11 - 19:51 
Reason for my vote of 5
Wonderful!
GeneralRe: A wonderful comment too! Thanks!memberjim lahey17 Nov '11 - 2:01 
A wonderful comment too! Thanks!
GeneralIf you use the DescriptionAttribute for the enums as part of...membergerm1331 Oct '11 - 22:26 
If you use the DescriptionAttribute for the enums as part of a web-service, will the description attributes show up in the WSDL?
GeneralRe: I have no idea. You'll have to ask the OP.memberjim lahey1 Nov '11 - 0:49 
I have no idea. You'll have to ask the OP.
GeneralReason for my vote of 5 Very nice implementation.memberMarcus Kramer27 Oct '11 - 4:47 
Reason for my vote of 5
Very nice implementation.
GeneralRe: Thankyou!memberjim lahey27 Oct '11 - 4:50 
Thankyou!
GeneralNice modification to it :-)memberCollin Jasnoch26 Oct '11 - 3:20 
Nice modification to it Smile | :)
GeneralRe: Thanks!memberjim lahey26 Oct '11 - 3:50 
Thanks!

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 30 Oct 2011
Article Copyright 2011 by jim lahey
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid