web 2.0

Localization of Silverlight Business Application (sample)

Illustration

Illustration

Although it looks that localization of Silverlight Business Applications is not worth of writing a post, there are some really inconspicuous problems which you have to resolve.

If you want to localize the application by creating resource files of strings for a particual language (AppStrings.sk-SK.resx, AppString.de-DE.resx) you should definitely read this post (with sample source-code) to not get into trouble.

At first I create a New project with Business Application Template.
Business Application Template
The strings resource files are in the template located in both Client and Server (Web) projects.
Resource files

The most important resource is the ApplicationStrings.resx file, in which almost all the strings from the client side are stored.

Strings which are used in the login and registration form are stored on the server side in RegistrationDataResources.resx.

To localize the strings you have to create copies of desired resource file with an appropriate sufix according to the culture, so slovak copy of AppStrings will be ApplicationStrings.sk-SK.resx. Do the same to all resource files in the project and translate them to your language.

Don’t add resource files to the Web\Resources directory in the client project, because in this folder should be placed just links to files in Resources folder in Web project. Link to a file si added by adding Existing item to client side Web\Resources and choosing “Add as link” option.Add as a link Till now, we did similar work we do in ASP.NET applications when localizing them.

To test whether it works add following line to App.xaml.cs

...
using System.Threading;
using System.Globalization;
...
private void Application_Startup(object sender, StartupEventArgs e)
{
    // This will enable you to bind controls in XAML files to WebContext.Current
    // properties
    this.Resources.Add("WebContext", WebContext.Current);

    // This will automatically authenticate a user when using windows authentication
    // or when the user chose "Keep me signed in" on a previous login attempt
    WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);

    //Change current culture to slovak, just for the test
    Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("sk-SK");

    // Show some UI to the user while LoadUser is in progress
    this.InitializeRootVisual();
}

Run the project and you’ll not see any localized string there.

Now, you’ll have to do a bit of manual work to make your localization working. At first, unload your client side project by right-click on a project and select “Unload Project”.
Navigate to the project folder in your filesystem and open the .csproj file in notepad or similar text editor.
Locate <SupportedCultures> element a place there all the culture that you want to have in your app separated by comma (<SupportedCultures>en-US,sk-SK</SupportedCultures>).

Reload the project back, build it and run. Localization should work.

Localized page

Localized page

Now, let’s look on how to localize new strings, which are not stored as resources. In this template, not localized are “Home page content” and “About page content” strings.

I created new resourcefiles HomeView.resx and AboutView.resx plus sk-SK localized equivalents and rebuild the project. In those I will store my strings for each view. To enable use of these resoruces in these views, add some piece of code to the ResourceWrapper.cs Helper class, which holds as a StaticResource in the XAMLs.
Note: Your resources are in Assets.Resources namespace in this case.

using Assets.Resources;
...
public sealed class ResourceWrapper
{
private static SecurityQuestions securityQuestions = new SecurityQuestions();
private static HomeView homeView = new HomeView();
private static AboutView aboutView = new AboutView();
...
public HomeView HomeViewStrings
{ get { return homeView; } }

public AboutView AboutViewStrings
{ get { return aboutView; } }

Then in our XAMLs we may use our ResourceWrapper properties HomeViewStrings and AboutViewStrings to bind to desired string.

<TextBlock ... Text="{Binding Path=HomeViewStrings.HomeContent, Source={StaticResource ResourceWrapper}}" />
Localized content

Localized content

You can localize the whole application this way, so I hope this article was helpful.

Remember what is the most important:

  1. Change the project file in text editor to allow the client to support different cultures (via SupportedCultures XML node).
  2. Login and registration resources are stored in the Web project.
  3. Add your own resourcefiles also to ResourceWrapper.cs Helper class to allow binding to it in XAML file.
  4. I recommend to set the Access Modifier in default resource files to “Public” (default is internal) and for culture-specific resource files to “No-code generation”, because we don’t need it in these.

Download source code of the sample. (Silverlight 4, .NET 4.0, VS 2010)

If you find some errors in my sample or you would like to share your knowledge about localization, some new ideas, etc, please leave comment below.

8 Responses to “Localization of Silverlight Business Application (sample)”

  1. Nice post. But the real troubls starts, when you don’t want use the proposed namespace of the projects.
    Change it, for example, to
    “LocalizedSilverlightApp.Client” and “LocalizedSilverlightApp.Server.Web” and the resources namespaces will not match anymore.

  2. [...] more here from  Lukas Zdechovan [...]

  3. >> Eric > Yes, thanks for the response, I’ll update the post as I’m playing with Composite Application Library and analysing options on how to build complex modular LoB applications with RIA Services framework & tools.
    So I believe I’ll face these problems soon.

  4. [...] This post was mentioned on Twitter by Katrien and brian_henderson, Kelps Leite de Sousa. Kelps Leite de Sousa said: RT @brian_henderson: Localization of Silverlight Business Application (sample): by Lukas Zdechovan: http://is.gd/7uhpq [...]

  5. Hi. I just noticed that your blog looks like it has a few code problems at the very top of your site’s page. I’m not sure if everybody is getting this same error when browsing your site? I am employing a totally different browser than most people, referred to as Opera, so that is what might be causing it? I just wanted to make sure you know. Thanks for posting some great postings and I’ll try to return back with a completely different browser to check things out!

  6. >> Chloe Rain > I’m using Opera too (version 10.1.) but I’m not getting any error. However, I checked whether the code of this page is valid and I found out, there are lot of errors in the content and head section of the page, so I’ll keep an eye on it.

  7. >> Emily stockman > No, Although I was really into web design last few years I didn’t have much time to build this blog, so I downloaded some ready-made wordpress designs and chose this one.

  8. Very helpful steps…saved me some time :-)