In a country like Canada where there are two official languages it makes good business sense to design your applications with localization as a strategy. Windows Presentation Foundation provides the means to localizing applications. There are a few localization strategies available to developers. There are 3 that I’d like to touch base on in this article:
Localizing WPF Using .resx Files
The .resx resource file format consists of XML entries, which specify objects and strings inside XML tags. One advantage of a .resx file is that when opened with a text editor (such as Notepad or Microsoft Word) it can be written to, parsed, and manipulated. Apart from any binary information stored in a .resx file it is completely readable and maintainable.
There are 7 key steps in creating a localized resource using this approach:
The sample project included with this blog entry is a demonstration of using .resx files to localize your applications. By changing the Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; line in the App.XAML.CS:App class you can simulate how your application will behave based in different cultures. For example changing the previous line to Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-CA"); will simulate a French Canadian culture.
Using this approach has a couple of things going for it. First there is good support for editing .resx files within the Visual Studio environment. It also provides focus at the resource entry level.
There are however a couple of issues that may present obstacles to integrating this approach in your project. Binding the control for the text that needs changing requires that the XAML be changed to point to the resource file and binding using this method can only be applied to dependency properties in the XAML.
Localizing WPF using LocBaml
Microsoft has published a utility that will parse a resource DLL created by a WPF application. That utility is called LocBAML. The source code for LocBAML can be found here.
In order to develop an application that can be localized using LocBAML follow these steps:
Using LocBAML to create culture resources has a couple of advantages over the .resx method mentioned at the beginning of this article. Localization can be performed without the original source and localization does not need to be part of the development process. In addition binding to properties requires no change to the XAML and any change can be bound to any property.
LocBAML focused resource granularity is at the BAML resource level and will overwrite any existing CSV files. The tool will also fail to generate correct CSV files if a resource contains a comma.
Using a database to store localized information
If your application is connected to a database it is possible to store string translations in the database and query for the appropriate string translation at runtime. Binding data in XAML is inherent in the mark-up language.
Here are an excellent series of articles describing how to connect to different data sources using XAML. In short an application developer can bind at runtime to the results of a database query and display the resulting text to the user.
Best Practices for WPF UI Design
When you design a WPF based UI, these are a few best practices to keep in mind:
References:
WPF Application Quality GuideHow to: Localize an ApplicationGlobalization for the Windows Presentation FoundationLocBaml Tool Sample
Thanks,John HinzISV Developer AdvisorMicrosoft Canada