This post explains how to add localization to a Silverlight application.

1. Adding a resource file (.resx) to your Silverlight projectfirst add a new folder to your Silverlight application and call it anything you like, i.e. LocResources, and then in your Visual Studio add a new Resource File in that folder: Add>New Item>(General category)Resources File. Then, add a new resource to your file, i.e. SiteTitle, and save it. While the resource file is opened, change the Access Modifier to Public (using the combo box).

Now copy and paste the .resx file in your LocResources folder and rename the new file to yourfilename.fr-FR.resx.

Very important: Open your resourcefile.Designer.cs file and change the Constructor to public, i.e. open SiteContent.Designer.cs and change internal SiteContent() to public SiteContent(). This will allow your MainPage to have access to this file.

A .resx file is really nothing but a XML file that contains your content in a format which can be used to localize your application, try opening the .resx file with Notepad to see it for yourself.

Please note, you can call your .resx file anything you like, but unless it is the default language of your site it has to end with the local indicator characters, i.e. if it is French your file has to be called name.fr-FR.resx

2. Add a Resources namespace and a static resource

xmlns:MyResources="clr-namespace:name"

Add the above namespace accordingly, where name is your Silverlight application name. Then add a static resource:

3. Replace the hardcoded text with Static Resourcesupdate Silverlight controls to have the following format where they display text:

Text="{Binding SiteTitle, Source={StaticResource SiteContent}}"

SiteTitle is the name of your resource data, you added this in step 1 in your resource file (.resx). SiteContent is the x:Name we gave to our reference of resource file which we created in step 2.

4. Update the .csproj file of your Silverlight projectopen the .csproj file of your Silverlight project in Notepad, locate the SupportedCultures element, at the moment it should not have any value, you will need to change this to a different value if you were going to localize it for a different language than your default language, i.e. you would needed to change this to fr-FR if you were going to localize this for French language, so change this to <SupportedCultures>fr-FR</SupportedCultures> then save and close the file.

5. add InitParameters to the Silverlight controlopen your TestPage.aspx (the default page that hosts your Silverlight Control in your web application project), and add InitParameters="UICulture=fr-FR" in order to localize for French. If you are using Object tag add <param name="uiculture" value="fr-FR"/>, and if you are using <asp:Silverlight ... tag add this: InitParameters="UICulture=fr-FR" to your tag.

6. Update App.xaml.cscopy the following code to the Application_Startup method:

Good article. The only issue with these steps is that I had to change all the methods and properties inside the resourcefile.Designer.cs file to public, not just the constructor.

Posted 10/21/2009 12:15:06 AM

Damon:

Hi Anil,

Welcome to Silverlight Tips and thanks for your comment. I tested this example by only changing the constructor to public and that worked fine, the only thing you need to keep in mind is that when you make a change in your resource.resx file and save it, it changes the constructor back to private.. so maybe you only needed to make sure the constructor is back to public?