It looks like I’m using too much resources and localizing too much applications lately :)
This time I’ll show how to use localized resources from the RESX files. Here I already explained how to create the RESX files for Silverlight (by the way the article is relevant also for Visual Studio 2010 Beta 2 & Silverlight 3), and how to use those resources from XAML. In some cases, though, it is needed to use those resources from code behind. In my scenario I had to create value converter, which converts some Boolean value to string representation and for localizes application I had to provide localized value.
In order to use resources from code behind one need to get instance of ResourceManager class, and the use its methods to get the resource by name for any supported culture.
Here the sample code snippet to do it:
ResourceManager resourceManager = new ResourceManager("Silverlight.Labs.TextEditor.Strings", GetType().Assembly);
return (string)resourceManager.GetString("txt_Page_Caption", Thread.CurrentThread.CurrentUICulture);
The ResourceManager constructor expects 2 parametes – resource name and assembly. In my case “Silverlight.Labs.TextEditor” is a namespace of my application, “Strings” is a RESX file name (and the class), and I need to pick up resources from current executing assembly.
The ResourceManager provides functions to get various types of resources via:
Stay tuned for new set of articles I’m writing those days – will be released pretty soon.
Enjoy,
Alex