Browse DevX
Sign up for e-mail newsletters from DevX


Globalize and Localize Your "Avalon" Applications Using LocBaml

You already know that internationalizing your applications can propel you into new markets. But did you know that the Windows Presentation Framework can make it easier than ever to do? Learn how to write culture-aware WPF applications using this example.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

nternationalization of applications is something developers have been encouraged to do for years, though all-too-frequently it doesn't get done. But the risks of not doing so are growing ever higher. The rapid maturation of the global marketplace means companies must increasingly look beyond their shores and venture into fast moving markets like China and India. Surviving in these huge markets requires localization of the applications into the native language(s). English alone won't cut it. For example, in China, a large portion of the computing population uses applications with UIs localized in Chinese, not English.

Microsoft recognizes the importance of providing globalization and localization support for applications running on Windows—at least, it has provided a number of ways to support developers in creating localized versions, and the WPF (Windows Presentation Foundation) is no exception. In WPF, Microsoft has taken a different methodology for localization that helps address the serious concern that localization is more than just pure string translation—rather, it is about localizing the entire UI.

In this article I will take you through the steps of localizing a WPF application. I will show how you can handle formatting issues (such as date and currency) as well as how you can localize string contents in your application. For this article I will use Chinese as the target language. You can modify the application to include as many other languages as you like.

Figure 1 shows the sample application you will build in this article. The user can choose the culture he wants and upon restarting the application, the selected language will be displayed. Restarting the application is necessary to ensure that the culture is changed since changing during runtime can be unreliable.

What You Need

Creating the UI
To get started, I'll first create the application using Visual Studio 2005 Beta 2. In File->New Project…, select the Avalon Application template (using Visual Basic) and name the application "WPF". Save the project as "C:\WPF".

I will first create the left panel of the window (see Figure 2).

The following XAML code creates the controls shown in Figure 2:

Figure 1. Choosing the Tongue: The sample application for this article allows the end user to choose the language that they would like to use. For the sample, Chinese is the only option.

<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" Text="WPF"> <DockPanel Background="LightGreen"> <StackPanel Name="StackPanel1" DockPanel.Dock="Left" Background="LightYellow"> <TextBlock Margin="10,10,10,0" FontStyle="Oblique" Foreground="Blue" FontSize="12" > Date and Time </TextBlock> <TextBlock Margin="10,10,10,0" Name="txtDateTime" FontSize="12" /> <TextBlock Margin="10,10,10,0" FontStyle="Oblique" Foreground="Blue" FontSize="12" > Currency </TextBlock> <TextBlock Margin="10,10,10,0" Name="txtCurrency" FontSize="12" /> <TextBlock Margin="10,10,10,0" FontStyle="Oblique" Foreground="Blue" FontSize="12" > Cultures </TextBlock> <ComboBox Name="cmbCultures" Margin="10,10,10,0" SelectionChanged="SelectedIndexChanged" > </ComboBox> </StackPanel>

Figure 3 shows the components on the right panel of the window.

Figure 2. Left Panel: These are the components you'll need in the left panel of the window.
Figure 3. Right Panel. These are the components you'll need in the right panel of the window.

The following XAML code creates the controls shown in Figure 3:

Figure 4. Add the Culture: Create a new application setting and set it to en-US.

<DockPanel DockPanel.Dock="Right"> <TextBlock Name="txtTitle" DockPanel.Dock="Top" HorizontalAlignment="Center" FontSize="12" > Title </TextBlock> <StackPanel Name="StackPanel2" Background="VerticalGradient Lavender Yellow"> <Label Name="lblName" Margin="10,5,10,5" FontSize="12" > Name </Label> <TextBox Name="txtName" Width="300" Height="25" HorizontalAlignment="Left" Margin="10,5,10,5" FontSize="12" ></TextBox> <Button Name="btnOK" Margin="10,5,10,5" HorizontalAlignment="Left" FontSize="12" > OK </Button> </StackPanel> </DockPanel> </DockPanel> </Window>

Adding a New Application Setting
Next you will want to create a new application setting for your project so that it can be used to persist the language preference of the user. To create an application setting in your WPF application, double-click on My Project in Solution Explorer and click the Settings tab. Add the new Culture application setting as shown in Figure 4. Give it a default value of "en-US" (the default English-US culture) and set its scope to User so that each language preference is specific to individual users rather than by machine.

Thanks for your registration, follow us on our social networks to keep up-to-date