Globalizing and Localizing Windows Applications, Part 1by Wei-Meng Lee
With the explosive growth of the Internet and rapid globalization of the world's economies, the earth is getting smaller and smaller. The applications that you develop for a local market may soon be used in another country. If the world used a common language, that would make the life of developers much easier. However, reality is far from perfect. With globalization, you need to consider the localization of your application for each specific market segment.
When people talk about localizing their applications, they often think of simply changing the text in a window to another language. While this is part of the localization process, localization involves much more, such as:
- Date formatting. People in the United States represent dates in a different format from someone in, say, the United Kingdom. Does "3/8/2002" represent 3rd August, 2002, or does it represent March 8th, 2002? The answer depends on where you are located.
- Text direction. Does text read from left to right or from right to left?
As a developer, you need to be concerned with the following:
- Globalization. When designing your application, you plan for all the necessary resources needed to enable your application to be modified with ease to suit different cultures.
- Localization. You perform the actual transformation to ensure that the user sees the application using the culture he/she has selected.
In this first article on globalization and localization, I will explain the basics of localization and how to display localized information in your Windows application. In the next installment, I will show you how to use .NET to localize a Windows application that changes its display to suit a particular culture.
A culture is a way to identify a particular setting pertinent to a location or country. You use a culture code to represent a culture. Let me give you some examples of culture codes:
- en-US: "en" represents the English language. "en-US" provides a specific culture, that is, the culture representing English used in the US.
- en-GB: This culture code represents the English language used in Great Britain.
- zh-CN: This culture represents the Chinese language used in the People's Republic of China.
A neutral culture represents a culture that is associated with a language but is not specific to a particular location. For example, "en" is a neutral culture, because it represents the English language but does not provide a specific instance of where it is used.
A specific culture is a culture that is specific to a region or country. For example, "en-GB" is a specific culture.
Finally, the invariant culture is neither a neutral nor specific culture. It is English, but is not associated with any location. The invariant culture is used for representing data that is not shown to the user. For example, you use the invariant culture to persist date information to a file. This ensures that the date information would not be misrepresented if is it going to be interpreted in another specific culture.
Displaying Culture information
In .NET, you can obtain detailed information about the culture used/supported
CultureInfo class. The following prints out all of the specific
cultures supported in .NET:
Imports System.Globalization ... Dim CI As CultureInfo For Each CI In _ CultureInfo.GetCultures(CultureTypes.SpecificCultures) Console.WriteLine(CI.Name) Console.WriteLine(ControlChars.Tab & CI.DisplayName) Console.WriteLine(ControlChars.Tab & _ CI.NumberFormat.CurrencySymbol) Console.WriteLine(ControlChars.Tab & _ CI.DateTimeFormat.ShortDatePattern) Next
Here are two specific ones:
... en-US English (United States) $ M/d/yyyy en-GB English (United Kingdom) £ dd/MM/yyyy ...
As you can see, the currency symbol for the "en-US" culture is "$", while that of the "en-GB" culture is "£". The date formats between the two cultures are also different, most notably in the order in which the day and the month are displayed.
Localizing Display Information
Be default, your .NET application will automatically load the current culture used by the operating system. However, it is often useful to let users explicitly choose the type of culture required during runtime.
The first step towards localization is to control the way information
is formatted. For example, you want to format numbers and dates according
to a particular culture. This is accomplished through the
property. As setting a culture in .NET is done at the thread level,
you can change a thread's culture by using the property:
To illustrate how to localize display information, let's build a Windows application. Populate the default Form1 with the following controls:
The populated Windows form is as shown in Figure 1.
|Figure 1. The populated Windows Form|
MainMenu control contains three menus representing these different
TextBox (for salary) control is changed,
I will display the date and salary using the
Private Sub DateTimePicker1_ValueChanged(ByVal sender _ As System.Object, _ ByVal e As System.EventArgs) _ Handles DateTimePicker1.ValueChanged drawText() End Sub Private Sub TextBox4_TextChanged(ByVal sender As _ System.Object, ByVal e As _ System.EventArgs) Handles _ TextBox4.TextChanged ' textbox for salary drawText() End Sub
Basically, I use the
drawText() subroutine to re-display the date of
birth and salary information. Note that for the salary, I first
assign it to a double variable and then format it using the
method with a
"c" currency format string.
Sub drawText() Try lblDOB.Text = DateTimePicker1.Value ' assign the salary to a double Dim salary As Double = TextBox4.Text lblSalary.Text = salary.ToString("c") Catch ex As Exception lblSalary.Text = "Error!" End Try End Sub
When the user clicks on the US menu, switch to English in the US culture:
Private Sub MenuItem2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MenuItem2.Click Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") MenuItem2.Checked = True MenuItem3.Checked = False MenuItem4.Checked = False drawText() End Sub
The same goes for English in the UK and Chinese in the China cultures:
Private Sub MenuItem3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MenuItem3.Click Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB") MenuItem3.Checked = True MenuItem2.Checked = False MenuItem4.Checked = False drawText() End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MenuItem4.Click Thread.CurrentThread.CurrentCulture = New CultureInfo("zh-CN") MenuItem4.Checked = True MenuItem2.Checked = False MenuItem3.Checked = False drawText() End Sub
When a culture is changed, I need to explicitly call the
subroutine to display the date and salary in the new format (see Figure
|Figure 2. Displaying the date and salary in English in the US, English in the UK, and Chinese in the China cultures.|
The beauty of .NET localization is that most of the formatting issues are taken care of by the framework. However, do note that although the currency symbol changes with different cultures, the value remains unchanged. .NET only performs display localization; you need to perform the actual currency conversion yourself.
In my next article, I will discuss how you can localize your Windows application to display different languages.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com
- Trackback from http://www.kellemeshusvetiunnepeket.com/0001607.php
böngészésének épített nagy personal lecsökkentik be
2005-07-11 03:31:01 [View]
2003-09-10 15:18:57 anonymous2 [View]