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

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

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

Published Wednesday, October 21, 2009 1:06 PM by Alex Golesh

Comments

# Links (10/22/2009) « Steve Pietrek – Everything SharePoint

Pingback from  Links (10/22/2009) « Steve Pietrek – Everything SharePoint

# Social comments and analytics for this post

This post was mentioned on Twitter by bobpulgino: RT @SilverlightNews Silverlight Quick Tip: How to get localized resources from RESX file in code behind - http://snurl.com/sn4l0

Sunday, October 25, 2009 10:18 AM by uberVU - social comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: