Intersoft ClientUI 8 > ClientUI Fundamentals > Localization Overview |
This topic provides an overview of the ClientUI Localization Framework. The framework includes a localization manager that allows you to easily create dynamic localized applications that supports MVVM design pattern.
This topic contains the following sections:
ClientUI Localization Manager is a framework that enables you to build dynamic, reliable localized applications. It overcomes the limitations of the Silverlight's built-in localization feature and packed with advanced features such as dynamic localized resource updating, MVVM design pattern support, and more.
The localization manager was built with the key goals described in the following:
At the heart of the ClientUI Localization Framework are the LocalizationManager and ObservableResource<T> classes. The LocalizationManager contains the core implementation that processes the culture changes and consequently notifies the active observable resources. The ObservableResource<T> class implements IObservableResource interface to provide capability to track and monitor whether a localized resource has changed, which automatically updates the source elements on which the resource was bound to.
The framework also includes LocalizationConverter, a multi-binding converter class that enables you to easily bind two or more string values, merge the place holders and convert them into a single value to the dependency property. To learn more about multi-binding, see Introduction to Multi Binding.
The first step to get started with ClientUI Localization Framework is to create an observable resource that wraps the resource assets in your application. Before continuing, you will need to configure your Silverlight or WPF project to support multiple cultures. For more information on creating localizable Silverlight project, see Walkthrough: Create a Localizable Silverlight Project.
You define one observable resource for each resource asset in your application, which is generally classified as the model in an MVVM application. To define an observable resource, you create a new class that derives from ObservableResource<T> and specify the type of the resource that you would like to observe, and finally create a constructor that pass-in a new instance of the localized resource class.
The following code shows how to create a new observable resource class that encapsulates an existing resource asset.
CS |
Copy Code
|
---|---|
public class MyAppResource : Intersoft.Client.Framework.ObservableResource<Resources> { public MyAppResource() : base(new Resources()) { } } |
As shown in the above code, the Resources class, which is the automatically generated class in the Silverlight project, can be simply passed to the constructor of the observable class.
Once an observable resource class is defined, you can consume it through a number of ways, such as defining a static resource in your XAML page to consume the localized resource, or instantiating it in ViewModel for MVVM design pattern usage. More about these topics are explained in the following sections.
Consuming the observable resource class in your XAML page is easy and straighforward. You create the mapping to the namespace that contains the observable resource class and define it as a static resource in the Resources of your XAML page. See the following example.
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Models="clr-namespace:ClientUILocalizationManager.Models"> <Intersoft:UXPage.Resources> <Models:MyAppResource x:Key="AppResource"/> </Intersoft:UXPage.Resources> </Intersoft:UXPage> |
With the observable resource class defined as a static resource in the top most Resources of the page hierarchy, you can bind the UI elements to the desired localized string by using the {Binding} declaration. To learn more about data binding, see Data Binding Overview.
The following code shows how to bind a simple TextBlock to the localized TodayText resource.
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Models="clr-namespace:ClientUILocalizationManager.Models"> <Intersoft:UXPage.Resources> <Models:MyAppResource x:Key="AppResource"/> </Intersoft:UXPage.Resources> <StackPanel> <TextBlock Text="{Binding Resource.TodayText, Source={StaticResource AppResource}}"> </StackPanel> </Intersoft:UXPage> |
As described in the previous sections, an observable resource class is generally classified as the model or entity that represent each resources asset in the Silverlight project. This allows you to define and instantiate the observable resource in the ViewModel instead of defining it in the static resource such as explained in the previous section.
The following code shows how to define the observable resource in the ViewModel and then consume it in the XAML page.
ViewModel (C#) |
Copy Code
|
---|---|
public class MainPageViewModel : ViewModelBase { public MainPageViewModel() { this.Localization = new MyAppResource(); } public MyAppResource Localization { get; set; } } |
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:ViewModels="clr-namespace:ClientUILocalizationManager.ViewModels"> <Intersoft:UXPage.Resources> <ViewModels:MainPageViewModel x:Key="MainPageViewModel"/> </Intersoft:UXPage.Resources> <StackPanel> <TextBlock Text="{Binding Localization.Resource.TodayText}"> </StackPanel> </Intersoft:UXPage> |
For more information on building applications with MVVM design pattern, see MVVM Pattern Overview.
One of the key features in ClientUI Localization Framework is the ability to update the localized resources that bound to the UI elements automatically when the application's culture changes. Using the data binding approaches such as described in the previous sections, you can dynamically change the application's culture and have the UI elements updated without additional code or efforts.
To change the application's culture, you set the Culture static property of the LocalizationManager to the desired culture that supported in the project.
The following code example shows how to change the application's Culture to France through a DelegateCommand.
CS |
Copy Code
|
---|---|
private void ExecuteChangeLanguage(object parameter) { LocalizationManager.Culture = new CultureInfo("fr"); } |
To learn more about the culture format, resource concept and the localization best practice in Silverlight, see the Silverlight Localization topic in MSDN Library. |