Silverlight Quick Tip: How to get localized resources from RESX file in code behind

October 21, 2009

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:

image

 

Stay tuned for new set of articles Iโ€™m writing those days โ€“ will be released pretty soon.

 

Enjoy,

Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. Matthew March 27, 2012 ื‘ 16:33

    Wow! I could not even guess about it)) Not bad.

    Reply