Silverlight – RESX Files and Internationalization

FANT0062 One of the toughest things to get your head around in Silverlight is Internationalization and Localization.  But once you figure out the quirks and the parts that are missing in visual studio 2008, it’s not that bad.

In this post I hope to explain step by step what you need to do to have this work reliably and share some of the mistakes I made along the way.

To get started, break out the Silverlight project we’ve been working on the past few weeks.

Add The RESX File
The first obvious thing we want to do is to add a RESX file to the project.  You can do that the same way you add any file to any other project.  I would suggest as a matter or practice, that you create a “Resources” subdirectory.  But you don’t need to.

In the Resource file, create a string resource with the name “First” and the value, “This is a first name.”  Obviously, for your own projects, you’d create multiple resources.  For this demo we are only going to create one.

Register the Resources
The next thing you’ll need to do is to register the Resource in your Page.xaml file using the following XML:


            x:Name="Resource" />

The <UserControl.Resources> is just a container for named resources.  The key element here is <SilverlightApplication1:Resource x:Name=”Resource” />

What this is saying is “In the SilverlightApplication1 namespace, find the class named ‘Resource’ and give it the name, ‘Resource’.”  We could have given it a name of “XYZ” if we wanted (x:Name=”XYZ”), but I like calling it what it is.

Bind The String
To bind the resource to the text property of an element, you can use,

Text="{Binding Path=First, Source={StaticResource Resource}}"

This statement says, “Look for the ‘First’ property in the Static Resource we’ve named ‘Resource’ in the UserControl.Resources section.

Correcting Errors
You would think, based on previous experience using RESX files, that those two steps are all you need.  But you’d be wrong.

If you run it now, you will get an error at runtime, AG_E_PARSER_BAD_TYPE.  This is because the class that gets created off the RESX file is marked as “internal” and the constructor that is created is also marked as internal.

The first thing we want to do is make the class public.  This is easy.  Open the RESX file and change the access modifier to “Public”


That will make the class public.  But we still need to make the constructor public.

If all you want to do is get the demo running, all you have to do is open the Resource.Designer.cs file that was created when you created the RESX file and change the constructor from “internal” to “public”

public Resource() {

The problem with this solution is that you will need to do this every time you make a change to the RESX file.  In real application development, this could be annoying.

A more flexible alternative would be to expose it through another class that you have control over.

You might think that you might be able to create a new class that is a sub class of the class in the designer.  That doesn’t work.  You are going to need to create a pass through class instead.

namespace SilverlightApplication1
    public class PublicResource
        public static Resource m_Resource = new Resource();
        public PublicResource() : base()


        public static Resource Resource
            get { return m_Resource; }


And you’ll need to change your XAML to reference the Resource property of this new class.

To access the resource through this class you’ll need to change your binding statement to

Text="{Binding Path=Resource.First, Source={StaticResource Resource}}" 

Notice that our Path attribute is telling it to access the ‘First’ property of the ‘Resource’ property in the class we’ve labeled ‘Resource’ in the UserControl.Resources section.

So now that we have the resource file loading, we need to experiment with internationalization.

Adding Country/Culture Files
The obvious next step is to add a RESX file for the country/culture you want to support.  For testing purposes, I added Resource.en-US.resx because I live in the US.  You might want to create one for your country/culture for testing purposes.

You’ll see if you do any searching on the topic that there are various solutions to actually telling Silverlight what the country/culture information is.  I prefer to have Silverlight get that information from the operating system.  If you wanted to, you could pass it in as a parameter.  Whichever way you do it, you’ll want to set the information prior to any of the controls on the page getting created.

The place to set the information is in the Application_Startup method of the App class.  The codebehind of App.xaml.

private void Application_Startup(
    object sender, StartupEventArgs e)
    Resource.Culture =
    this.RootVisual = new Page();


Notice that I’m setting the Resource.Culture, which is a static method, to the CurrentUICulture.  For resources, this is what you want.  CurrentCulture is for setting how date/time and money will display.  It may start out looking right, but you’ll get bitten by this difference eventually.

Register The country/culture in the Project
There is one final step to make all this work.  You must open your project file (*.csproj or *.vbproj if you are using VB.NET) in a text editor and find the SupportedCultures to contain a comma-delimited list of country/culture codes that you’ve included in your application so that they get compiled in to the package that will be deployed to the web.

In my case, I added, <SupportedCultures>en-US</SupportedCultures>.

It is important to note here that you can’t have any white space between the opening and closing tags.  I did that the first time and it didn’t work.

“It’s Just That Simple”
Well, this has been a pretty long post, and I hesitate to say, “That’s all there is to it.”  But the fact of the matter is, it takes a whole heck of a lot more time to write about than it does to actually implement.  So don’t be intimidated by the number of words it took to explain it.

Hopefully, when visual studio 2010 comes out, a lot more of this will “just work.”

Other Places Talking About Silverlight, RESX, and Internationalization

Internationalization / localization in Silverlight – Localization and internationalization are the processes of making software capable to display content in different languages depending on the user preferences like described in Wikipedia. It’s a common scenario in business applications …

Silverlight Localizability and Pseudo-Localization – Pseudo Localization is a test process designed to make to easy to find hard coded strings, fixed width UI or improper encoding. It does this by converting the English UI elements to a extreme internationalized version of the UI …

Related Post

Comments are closed.