Home » Silverlight » Localized Silverlight with Resource File

Localized Silverlight with Resource File

Let say that you want to have your application in multiple language. In Asp.Net, the choice is obvious to go with resource file. Since I am coming from the web side, I did the same thing with Silverlight.

Visual Studio Resource Editor

In Silverlight, you can still use Resource File. They are available in the New Item dialog.

How to add a Resource file to Silverlight

In my project I added two resources file : MyRes1.resx and MyRes1.fr.resx. This way, I can have English and French words translated.

The next logical step is to handle the language selection. In my case, the language came from the Asp.Net page. It was passed as a parameter. In App.xaml.cs, in the Application_Startup() event, I added a call to SetCultureInformation() which I have defined like this:

private void SetCultureInformation(StartupEventArgs e)
{
	var languageInfo = new CultureInfo("en");
	if (e.InitParams.ContainsKey("language"))
	{
		if (e.InitParams["language"] == "fr")
		{
			languageInfo = new CultureInfo("fr");
		}
	}
	
	Thread.CurrentThread.CurrentCulture = languageInfo;
	Thread.CurrentThread.CurrentUICulture = languageInfo;            
 }

I were ready to execute to test but I had the problem that Silverlight didn’t want to switch to French.

After few hours, I realized that Silverlight’s project can have specific variable to be set to be able to tell Silverlight which language is supported.

To do, open your Silverlight project with Notepad and search for “SupportedCultures”

SupportedCultures

You need to set all language you want, but no need to add the default one. In my case, I added only the French one, so “fr”.

After that, everything worked.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.