Localization in Silverlight

8 Feb

It’s pretty easy to use localization in Silverlight using resoucefiles (http://msdn.microsoft.com/en-us/library/cc838238(VS.95).aspx and http://msdn.microsoft.com/en-us/library/dd882554(VS.95).aspx). Instead of using resource files it’s possible to use a database and WCF RIA Services to retrieve the localized strings. The following is an example of how to do it.

The server part

First of all I have created an entity to represent my datamodel – the entity is called LocalizatoinItem and have three properties: Key (string), Value(string) and Language (string).

LocalizationItem

To retrieve the data from the database I have created a repository called LocalizationRepository. The repository retrieves and cache data on the server.

LocalizationRepository-1

To get data in this example I have created a method to generate the it.

LocalizationRepository-3

WCF RIA Services make it easy to get data on the client from the server. To enable this, I have created a DomainService called LocalizationService. It contain a method to retrieve an IEnumerable of LocalizatoinItems.

LocalizationService

The client part

On the client side I have a class to maintain the state of the application – I have called it ApplicationState. It has a static property called Current that holds an instance of the class and a property called LocalizationStrings that will hold a list of the localized strings.

ApplicationState

When the application starts up I call the GetLocalizationItems method on the server to retrieve the localized strings. When the list of strings is retrieved I set the property in the current instance of the ApplicationState and set the RootVisual.

App

To use the localized strings in XAML I need to use DataBinding and a ValueCOnverter called LocalizationValueConverter. The ValueConverter will retrieve a specific string from the list of localized strings based on the parameter sent to the converter.

LocalizationConverter

In XAML I have made a reference to the LocalizationValueConverter.

xaml-2

To bind the strings to a TextBlock I use DataBinding and sent the key I wish to use as parameter to the LocalizationValueConverter.

xaml-1

Finally I have set the DataContext on the page to trigger the DataBinding.

MainPage

It’s properly possible to make some shortcuts compared to the steps above – but it works.

Code is available here.

by xamlgeek

One Response to “Localization in Silverlight”

  1. Shemesh April 7, 2010 at 11:49 #

    nice one!
    it seems there is no “clean” solution for localizing from server.

    say… why aren’t you pushing the strings to the app’ resources?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: