Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Globalize and Localize Your "Avalon" Applications Using LocBaml : Page 2

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.




Application Security Testing: An Integral Part of DevOps

Writing Code
In the code behind of Window1.xaml (Windows1.xaml.vb), first import the following namespaces:

Imports System.Globalization Imports System.Threading

When the form is instantiated, you first retrieve the culture preference previously saved by the user and set the current thread to the retrieved culture. Note that you set both the CurrentUICulture (for localization of UI) and the CurrentCulture (for number and date formatting, etc.) properties.

Public Sub New() Dim cultureCode As String = My.Settings.Culture Thread.CurrentThread.CurrentUICulture = _ New CultureInfo(cultureCode) Thread.CurrentThread.CurrentCulture = _ New CultureInfo(cultureCode) InitializeComponent() End Sub

When the form is loaded, you first display the date and currency information through the DisplayText() subroutine (which I will define shortly). You then retrieve all the cultures supported in .NET using the GetCultures() method and programmatically add them to the combobox.

Private Sub OnLoaded( _ ByVal sender As Object, _ ByVal e As RoutedEventArgs) Handles Me.Loaded DisplayText() '---display all the cutlures in the combobox control Dim CI As CultureInfo For Each CI In System.Globalization. _ CultureInfo.GetCultures( _ CultureTypes.SpecificCultures) Dim item As New ComboBoxItem item.Content = CI.DisplayName item.Tag = CI.Name cmbCultures.Items.Add(item) Next End Sub

Note that the friendly name (DisplayName, e.g. "Chinese (People's Republic of China)") of the CultureInfo object is displayed in the combobox (see Figure 5) while the culture code (Name, e.g. "zh-CN") is saved in the Tag property of the ComboBoxItem object.
Figure 5. Cultures: All supported cultures are displayed in the combobox.

When the user changes the selection in the combobox, you save the selected culture into the Culture application setting. Notice that the My namespace automatically displays the Culture application setting through Intellisense:

Public Sub SelectedIndexChanged(ByVal sender As Object, _ ByVal e As SelectionChangedEventArgs) My.Settings.Culture = cmbCultures.SelectedItem.Tag My.Settings.Save() End Sub

The DisplayText() subroutine simply displays the current time and a sample currency by using formatting parameters in the ToString() method:

Private Sub DisplayText() txtDateTime.TextContent = Now.ToString("D") Dim amount As Single = 1234.56 txtCurrency.TextContent = amount.ToString("C") End Sub

Adding Uids to XAML files
With the UI built and the code written, you now need to add Uids to all the elements in your XAML files. Uids are used to keep track of changes to files and identify items that must be translated. You can use the MSbuild.exe tool to add Uids to all your XAML elements. To invoke the MSBuild tool, go to Start->Programs->Microsoft WinFX SDK->Release Build Environment and type in the following command (see also Figure 6):

C:\WPF>msbuild /t:updateuid WPF.vbproj

Figure 6. Adding Uids: Use MSbuild to add Uids to all XAML elements.

All the elements in your XAML files will now have the x:Uid attribute added:

<Window x:Uid="Window_1" x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" Text="LocalizedWPF"> <DockPanel x:Uid="DockPanel_1" Background="LightGreen"> <StackPanel x:Uid="StackPanel1" Name="StackPanel1" DockPanel.Dock="Left" Background="LightYellow"> <TextBlock x:Uid="TextBlock_1" Margin="10,10,10,0" FontStyle="Oblique" Foreground="Blue" FontSize="12"> Date and Time </TextBlock> ... ...

To check that all elements have the Uid attribute, you can use the following command:

C:\WPF>msbuild /t:checkuid WPF.vbproj

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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