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


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.

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)
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

    '---display all the cutlures in the combobox control
    Dim CI As CultureInfo
    For Each CI In System.Globalization. _
      CultureInfo.GetCultures( _
        Dim item As New ComboBoxItem
        item.Content = CI.DisplayName
        item.Tag = CI.Name

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
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"
   <DockPanel x:Uid="DockPanel_1" 
      <StackPanel x:Uid="StackPanel1" 
         Name="StackPanel1" DockPanel.Dock="Left" 
            x:Uid="TextBlock_1" Margin="10,10,10,0" 
         Date and Time
To check that all elements have the Uid attribute, you can use the following command:

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

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