In my “A Better Way to Data Bind Enums in WPF” post, I showed how we could use a combination of a custom MarkupExtention and TypeConverter to more easily data bind an ItemsControl to an Enum and display a custom description for each Enum value. The result looked something like this:
I have recently received a question related to that post on how to go about adding support for localizing those descriptions. Well, it just so happens to be extremely easy. We will be building on top of the previous Enum sample from the previous post. Let’s see what it take to localize enum descriptions in WPF.
Adding Localization Support
The first thing we need to do in order to localize enum descriptions is to create a custom Attribute. I called mine LocalizedDescriptionAttribute, and it looks like this:
This custom attribute derives from the already existing DescriptionAttribute. This allows us to override the Description property to provide our own implementation. We are expecting a resourceKey which will be the unique name defined in our Resources.resx file. We also need to know which Resource object to use, since you can create any number of uniquely named resource files in your application. We then rely on the built-in ResourceManager, given the correct resourceType, to get the correct value depending on the current culture information.
Go ahead and add a coupld of Resource.resx files to the project and target your desired cultures.
Now that we have our resource files, we need to modify our Enum to include the LocalizedDescriptionAttribute. I will only modify the last three values.
Go ahead and run the code. You should notice no change in the values themselves, but you will notice the values are being retrieved from our EnumResources.resx file.
Now, go ahead and go into the App.xaml.cs and override the OnStartup method, and change the current CultureInfo.
Now run the app again.
Now we can visually see our LocalizedDescriptionAttribute at work. That’s it! That’s all it takes to localize enum descriptions in WPF. Hopefully you will find this useful, and maybe even use this approach in your WPF applications. Be sure to check out the source code, and start playing with it. As always, feel free contact me on my blog, connect with me on Twitter (@brianlagunas), or leave a comment below for any questions or comments you may have.