Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.
Recent comments

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

1. Adding a resource file (.resx) to your Silverlight project
first 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:


<UserControl.Resources>
   <MyResources:SiteContent x:Name="SiteContent"/>
</UserControl.Resources>

3. Replace the hardcoded text with Static Resources
update 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 project
open 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 control
open 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.cs
copy the following code to the Application_Startup method:

if (e.InitParams.Count > 0)
{
    string cultureName = e.InitParams["UICulture"].ToString();
    if (!String.IsNullOrEmpty(cultureName))
        Thread.CurrentThread.CurrentUICulture =
        new CultureInfo(cultureName);
}

You may need to addthe following namespaces to the top of the page:

using System.Threading;
using System.Globalization;

Run and enjoy!

Posted by Damon Serji on 15. September 2009 22:11 under: Intermediate
 with 2 Comments