Lesson 5: Globalization and Localization

Lesson 5: Globalization and Localization

The .NET Framework provides unprecedented developer support for globally accessible applications. You can create applications that adapt to different languages, currency formats, date/time formats, and other culture-sensitive information. In this lesson, you will learn how to create globalization and localization in your application.

After this lesson, you will be able to

  • Explain what is meant by globalization and what is meant by localization

  • Explain how to implement globalization and localization in the user interface

  • Describe how to prepare culture-specific formatting

  • Describe how to implement right-to-left formatting

  • Explain how to convert existing text encoding schemes

  • Describe how to validate non-Latin input

Estimated lesson time: 30 minutes

If your company does business in the global marketplace, you must design your applications to accommodate users from a variety of cultures. Users in other parts of the world might be unfamiliar with U.S. formatting standards for currency and dates, or they might be unable to understand the English language. By designing international support into your applications, you increase your application s user base and enable its global use.

Globalization and Localization

Globalization and localization are two different but related processes. Globalization involves applying culture-based formatting to existing data, while localization involves retrieving the appropriate piece of data based on the culture. The following examples illustrate these differences:

  • Globalization.

    Currency is formatted in some countries using a period (.) as a thousand separator and a comma as a decimal separator. A globalized application takes the existing data for currency and formats it appropriately based on location.

  • Localization.

    The title of a form is displayed in a given language depending on the country in which it is deployed. A localized application retrieves the appropriate string and displays it according to the location.

Culture

In your application, culture refers to cultural information about the region in which the application is being used. In the .NET Framework, cultures are identified using a culture code that represents the current language to the framework. A culture code can also specify information about a region. Generally, the culture code is either a two-letter code that specifies the language or a two-letter language code followed by a dash and another two-letter code that specifies the region. Culture codes that only specify the language are known as neutral cultures, whereas codes that specify the language and the region are referenced as specific cultures. The following list shows examples culture codes:

  • en.

    Specifies English language, no region

  • en-CA.

    Specifies English language, Canada

  • af-ZA.

    Specifies Afrikaans language, South Africa

  • eu.

    Specifies Basque language, no region

  • kn-IN.

    Specifies Kannada language, India

  • tr.

    Specifies Turkish language, no region

A complete list of culture codes can be found in the CultureInfo Class reference topic in the .NET Framework reference documentation.

Although most culture codes follow the format just described, there are some exceptions. For example:

  • uz-UZ-Cyrl.

    Specifies Uzbek language, Uzbekistan, Cyrillic alphabet

  • uz-UZ-Latn.

    Specifies Uzbek language, Uzbekistan, Latin alphabet

  • zh-CHT.

    Specifies traditional Chinese language, no region

  • zh-CHS.

    Specifies simplified Chinese language, no region

Getting and Setting the Current Culture

The application automatically reads the system culture settings and implements them. You can change the current culture of your application programmatically by setting the current culture to a new instance of the CultureInfo class. This class holds information about a culture and how it should interact with your application. For example, the CultureInfo class contains information on the type of calendar, currency formatting, date formatting, and so on for a specific culture. You can set the culture of an application programmatically by setting the CurrentThread.CurrentCulture property to a new instance of the CultureInfo class. The CultureInfo class constructor takes a string that represents the appropriate culture code as a parameter. The following code example demonstrates how to set the current culture to French-Canadian:

Visual Basic .NET

System.Threading.Thread.CurrentThread.CurrentCulture = New _ System.Globalization.CultureInfo("fr-CA")

Visual C#

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-CA");

You can retrieve the CultureInfo class that represents the CurrentCulture by accessing the CultureInfo.CurrentCulture class, as follows:

Visual Basic .NET

' This example assumes Imports System.Globalization Dim myCurrentCulture As CultureInfo myCurrentCulture = CultureInfo.CurrentCulture

Visual C#

// This example assumes using System.Globalization CultureInfo myCurrentCulture; myCurrentCulture = CultureInfo.CurrentCulture;

Implementing Globalization

When the Thread.CurrentThread.CurrentCulture property is set to a new CultureInfo, any data formatted by the application is updated with the new format. Data that is not formatted by the application, however, is unaffected. Consider a form with a single Label control. Suppose that you set the text of this Label control in code like this:

Visual Basic .NET

Label1.Text = "$500.00"

Visual C#

label1.Text = "$500.00";

If you then set the current culture to en-GB, which specifies Great Britain, what would you expect to see displayed in the label? It will read $500.00. The culture setting has no effect on the label content because no formatting was used. On the other hand, suppose the value of the Label control was set in the following manner:

Visual Basic .NET

Label1.Text = Format(500, "Currency")

Visual C#

label1.Text = (500).ToString("C");

When the culture is changed to en-GB, the label reads 500.00. The value is formatted to the currency format appropriate for the locale. Note that no currency conversion is performed only the formatting changes.

Implementing Localization

The .NET Framework makes localization easy by creating resource files that hold data for alternative forms associated with the cultures your application supports. At run time, the appropriate form is loaded based on the CultureInfo.CurrentUICulture property.

Getting and Setting the Current UI Culture

The UI culture is represented by an instance of CultureInfo, and is distinct from the CultureInfo.CurrentCulture property. The CurrentCulture setting determines the formatting that will be applied to formatted data, whereas the CurrentUICulture determines the resources for localized forms that will be loaded at run time. You can retrieve the CurrentUICulture by accessing the CultureInfo.CurrentUICulture property, as follows:

Visual Basic .NET

' This example assumes Imports System.Globalization Dim myCurrentCulture As CultureInfo myCurrentCulture = CultureInfo.CurrentUICulture

Visual C#

// This example assumes using System.Globalization CultureInfo myCurrentCulture; myCurrentCulture = CultureInfo.CurrentUICulture;

The current UI culture is set in the same way as the current culture: by accessing the current Thread. For example, the following code sample demonstrates how to set the current UI culture to Thailand:

Visual Basic .NET

System.Threading.Thread.CurrentThread.CurrentUICulture = New _ System.Globalization.CultureInfo("th-TH")

Visual C#

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("th-TH");

When the current culture is set, the operating system loads resources specific to that culture if they are available. If culture-specific resources are unavailable, the user interface displays resources for the default culture.

NOTE
The UI culture must be set before a form is loaded displaying any localized resources. If you want to set the UI culture programmatically, you should set it in the constructor of the main form or in the main method of the application. At the very least, it should be set before any important UI elements are displayed.

Creating Localized Forms

Creating localized forms with the .NET Framework is a nearly effortless process. Every form exposes a Localizable property that determines if the form is localized or not. Setting this property to true enables localization.

When the Localizable property of a form is set to true, Visual Studio .NET automatically handles the creation of appropriate resource files. This is handled through the Language property of the form. When this property is set to (Default), you can edit any of the form s UI properties or controls to provide a representation for the default UI culture. To create a localized version of the form, you can set the Language property to any value other than (Default). Visual Studio .NET will create a resource file for the new language and store any values you set for the UI in that file.

NOTE
Although localized UI elements are usually strings, any property can be localized. Thus, you can have buttons that adjust in size to accommodate different text lengths for different languages or a picture box that displays different pictures based on locale.

You can view the resource files for a form by clicking the Show All Files button in Solution Explorer, and then expanding the node next to your form. Under your form, you will see a resource file for every language for which the form has a version. An example of a form with resource files for the default language and English, French, and German is shown in Figure 8.7.

figure 8-7 localized resource files.

Figure 8-7. Localized resource files.

To create localized forms

  1. Set the Localizable property of your form to true.

  2. Design the user interface of your form and translate any UI elements into the localized languages.

  3. Add UI elements for the default culture. This is the culture that will be used if no other culture is specified.

  4. Set the Language property of your form to the appropriate culture.

  5. Add the localized UI content to your form.

  6. Repeat Steps 4 and 5 for each localized language.

  7. Build your application.

When the CurrentUICulture is set to a localized culture, your application will load the appropriate version of the form by reading the corresponding resource files. If no resource files exist for a specified culture, the default culture UI will be displayed.

Validating International Input

You might need to incorporate techniques for validating international input into your application. Validating keystroke input can be particularly problematic for international applications. The developer might be unfamiliar with the character system used in a particular locale and, therefore, unable to determine what category keystrokes belong to.

You can use the validation methods provided in the Char structure to make these determinations in much the same way as you would for Latin alphabet input. The Char.IsDigit, Char.IsLetter, and other methods will return appropriate values no matter what input character set is used. Thus, validation code that uses these methods will function correctly without any special modification.

Culture-Specific Formatting

You can supply values for specific members of CultureInfo to tailor your application to specific cultural needs. For example, suppose you are writing an application for a client that collaborates with a group in Japan, but the currency used in the application is U.S. dollars. You would need to create an application that uses Japan-specific formatting for most elements, but uses the dollar sign ($) as the currency symbol.

The CultureInfo class contains three members that define how globalization formatting is carried out. You can customize these members to provide specific formatting combinations that might be unavailable for standard cultures. These members are summarized in Table 8.3.

Table 8-3. Formatting Members of CultureInfo

Member

Description

DateTimeFormat

Contains information about how dates and times are formatted, including properties that describe how abbreviated day and month names are rendered, how A.M. and P.M. time is displayed, which calendar and day names to use, and which date and time formats to use when formatting is performed

NumberFormat

Contains information about how numbers and currency are formatted, including properties that describe the decimal and thousand separators to use for numbers and currency, which currency symbol to use, and how percentages are formatted

TextInfo

Contains information about how text is formatted, including which code page to use and the appropriate list separator symbol

You can change the properties of these members to suit specific culture formatting needs. For example, consider the aforementioned example. To create a culture setting that is mostly Japanese, but specifies the dollar symbol ($) for a currency, you would create a new CultureInfo object and modify it to the specific case, as demonstrated in the following code example.:

Visual Basic .NET

' This example assumes Imports System.Globalization and Imports ' System.Threading Dim modJPCulture As New CultureInfo("jp-JN") modJPCulture.NumberFormat.CurrencySymbol = "$" Thread.CurrentThread.CurrentCulture = modJPCulture

Visual C#

// This example assumes using System.Globalization and using // System.Threading CultureInfo modJPCulture = new CultureInfo("jp-JN"); modJPCulture.NumberFormat.CurrencySymbol = "$"; Thread.CurrentThread.CurrentCulture = modJPCulture;

Implementing Right-to-Left Display

Some languages are read from right-to-left instead of left-to-right as in most Latin alphabet languages. Standard Windows Forms provide a RightToLeft property that enables implementation of a right-to-left user interface.

The RightToLeft property has three settings: Yes, No, and Inherit, with Inherit being the default value. When this property is set to Inherit, the RightToLeft value is determined by the value of the parent control.

Setting a control s RightToLeft property to Yes does several things, depending on the type of control. Text alignment is reversed. Thus, any text that is normally left-aligned in the control becomes right-aligned. If a form s RightToLeft property is set to Yes, its caption is right-aligned. Vertical scroll bars are displayed on the left side, and horizontal scroll bars are initialized with the slider right-aligned. Check boxes have their CheckAlign property reversed. Tab buttons are reversed, as is the alignment of items in list boxes and combo boxes. In short, formatting for each control becomes a mirror image of itself.

The content contained in a RightToLeft control, however, is unchanged. For example, consider a TextBox control, shown with standard left-to-right formatting in Figure 8.8.

figure 8-8 a text box with left-to-right formatting.

Figure 8-8. A text box with left-to-right formatting.

In Figure 8.9, the same text box is displayed with right-to-left formatting.

figure 8-9 a text box with right-to-left formatting.

Figure 8-9. A text box with right-to-left formatting.

Although control s alignment has changed, the text is still displayed as being read from left to right. Thus, if you create localized resources for cultures that read from right to left, you must format the strings manually.

Setting a form s RightToLeft property to Yes will cause it and any controls that have a RightToLeft value of Inherit to become right-aligned. You might want to extend this principle further and create a mirror image of your form for cultures that read from right to left.

You can create a mirror image of your form by creating a localized version and positioning the controls manually to form the mirror image. The position properties of each control will be saved to the resource file for the localized version.

Converting Character Encodings

The .NET Framework uses Unicode UTF-16 character encoding to represent characters. The Unicode character set is a universal standard for representing characters. More than 65,000 characters have a unique representation in this character set, and there is room for the Unicode character set to expand by more than a million more characters. Thus, it is ideal for representing international text content.

Before Unicode, the language requirements for different cultures forced developers to use diverse encodings to represent data internally. Thus, data from different cultures came to be represented in different character sets, such as single-byte editions for European languages, double-byte editions for Asian languages, and bidirectional editions for Middle Eastern languages. This fragmentation made it difficult to share data between cultures and even more difficult to develop world-ready applications that support a multilingual user interface.

The .NET Framework allows you to convert legacy data in other formats to Unicode data. Also, if interaction with legacy components is required, you can convert Unicode data into the legacy-encoding format.

Encoding conversions are carried out by the Encoding class, which is found in the System.Text namespace. The Encoding class cannot be directly instantiated, but you can obtain an instance of this class by using the Shared (static) method, Encoding.GetEncoding, to get an encoding that specifies a particular character code page, as shown in the example to follow:

Visual Basic .NET

' This example assumes Imports System.Text Dim myEncoding As Encoding ' Code page 932 represents Japanese characters myEncoding = Encoding.GetEncoding(932)

Visual C#

// This example assumes using System.Text Encoding myEncoding; // Code page 932 represents Japanese characters myEncoding = Encoding.GetEncoding(932);

Once you create an instance of a particular encoding, you can use it to convert characters in that encoding to Unicode format and vice versa. You can convert existing data to Unicode by calling the Encoding.Convert method. This method requires as parameters a source encoding, a target encoding, and an array of bytes that represents the data to be converted in the source-encoding format. This method returns an array of bytes in the target-encoding format. To convert to Unicode, you can specify an instance of the System.Text.UnicodeEncoding class as the target encoding. The following code example demonstrates how to convert data encoded by code page 932 to data encoded in Unicode:

Visual Basic .NET

' This example assumes Imports System.Text. The data to be converted ' is represented as a byte array by the variable name myData Dim tgtData() As Byte Dim srcEncoding As Encoding Dim tgtEncoding As New UnicodeEncoding() ' Code page 932 represents Japanese characters srcEncoding = Encoding.GetEncoding(932) ' tgtData now contains an array of bytes that represents the Unicode ' encoding of the byte array in myData. tgtData = Encoding.Convert(srcEncoding, tgtEncoding, myData)

Visual C#

// This example assumes using System.Text. The data to be converted // is represented as a byte array by the variable name myData byte[] tgtData; Encoding srcEncoding; UnicodeEncoding tgtEncoding = new UnicodeEncoding(); // Code page 932 represents Japanese characters srcEncoding = Encoding.GetEncoding(932); // tgtData now contains an array of bytes that represents the Unicode // encoding of the byte array in myData. tgtData = Encoding.Convert(srcEncoding, tgtEncoding, myData);

To convert legacy data to an array of bytes in preparation for this conversion, you can call the GetBytes method for your encoding to create a byte array from a char array or a string. For example:

Visual Basic .NET

' This example assumes Imports System.Text. The data in the legacy ' format is contained in a variable called myString Dim myEncoding As Encoding myEncoding = Encoding.GetEncoding(932) Dim myBytes() As Byte myBytes = myEncoding.GetBytes(myString)

Visual C#

// This example assumes Imports System.Text. The data in the legacy // format is contained in a variable called myString Encoding myEncoding; myEncoding = Encoding.GetEncoding(932); Byte[] myBytes; myBytes = myEncoding.GetBytes(myString);

Likewise, you can use the GetChars method to convert an array of bytes to an array of chars. For example:

Visual Basic .NET

' This example assumes Imports System.Text. The data is contained ' in an array of bytes called myBytes. Dim myEncoding As New UnicodeEncoding() Dim myChars() As Char myChars = myEncoding.GetChars(myBytes)

Visual C#

// This example assumes Imports System.Text. The data is contained // in an array of bytes called myBytes. UnicodeEncoding myEncoding = new UnicodeEncoding(); char[] myChars; myChars = myEncoding.GetChars(myBytes);

Lesson Summary

  • Globalization refers to formatting data based on locale. Localization refers to displaying a particular set of data based on locale.

  • You can implement globalization in your applications by using formatting for your data instead of hard coding the format. Formatting will update when the current culture changes.

  • Localization is implemented by providing multiple resource files for localized UI elements. These resource files are created automatically for forms when the Localizable property is set to true and the Language property is changed.

  • A culture is represented by an instance of CultureInfo. By setting System.Threading.Thread.CurrentThread.CurrentCulture to a new CultureInfo instance, you set the current culture. You can set the current UI thread by setting System.Threading.Thread.CurrentThread.CurrentUICulture.

  • You can create custom culture settings by setting individual members of a CultureInfo instance.

  • You can convert data from code page encodings to Unicode or any other encodings by using the Encoding.Convert method. You can obtain an array of bytes from a string or character array by calling the Encoding.GetBytes method. Likewise, you can encode an array of bytes to an array of chars by calling the Encoding.GetChars method.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net