Chapter 6: Localization


Developers usually build their applications in the English language. Then, as the audience for the application expands, they realize the need to globalize the application. Of course, the ideal is to build the application to handle an international audience right from the start - but in many cases this may not be possible because of the extra work it requires.

With the release of .NET Framework 2.0, a considerable effort has been made to address the internationalization of Web applications. Changes to the API, the addition of capabilities to the server controls, and even Visual Studio itself equip you to do the extra work required to bring your application to an international audience. This chapter takes a look at some of the important items to consider when building your applications for the world.

Cultures and Regions

The ASP.NET page that is pulled up in an end user’s browser runs under a specific culture and region setting. When building an ASP.NET application or page, the defined culture in which it runs is dependent upon a culture and region setting specified either in the server in which the application is run or in a setting applied by the client (the end user). By default, ASP.NET runs under a culture setting defined by the server.

The world is made up of a multitude of cultures, each of which has a language and a set of defined ways in which it views and consumes numbers, uses currencies, sorts alphabetically, and so on. The .NET Framework defines cultures and regions using the Request for Comments 1766 standard definition (tags for identification of languages), which specifies a language and region using two-letter codes separated by a dash. The following table provides examples of some culture definitions:

Open table as spreadsheet

Culture Code

Description

en-US

English language; United States

en-GB

English language; United Kingdom (Great Britain)

en-AU

English language; Australia

en-CA

English language; Canada

Looking at the examples in this table, you can see that four distinct cultures are defined. These four cultures have some similarities and some differences. All four cultures speak the same language (English), so the language code of en is used in each culture setting. After the language setting is the region setting. Even though these cultures speak the same language, it is important to distinguish them further by setting their region (such as US for the United States, GB for the United Kingdom, AU for Australia, and CA for Canada). As you are probably well aware, the English language in the United States is slightly different from the English language used in the United Kingdom, and so forth. Beyond language, differences exist in how dates and numerical values are represented. This is why a culture’s language and region are presented together.

The differences don’t break down by the country only. Many times, countries contain more than a single language, and each area has its own preference for notation of dates and other items. For example, en- CA specifies English speakers in Canada. Because Canada is not only an English-speaking country, it also includes the culture setting of fr-CA for French-speaking Canadians.

Understanding Culture Types

The culture definition just given is called a specific culture definition. This definition is as detailed as you can possibly get, defining both the language and the region. The other type of culture definition is a neutral culture definition. Each specific culture has a specified neutral culture with which it is associated. For instance, the English language cultures shown in the previous table are separate, but they also belong to one neutral culture EN (English). The diagram presented in Figure 6-1 displays how these culture types relate to one another.

image from book
Figure 6-1

From this diagram, you can see that many specific cultures belong to a neutral culture. Higher in the hierarchy than the neutral culture is an invariant culture, which is an agnostic culture setting that should be utilized when passing items (such as dates and numbers) around a network. When performing these kinds of operations, make your back-end data flows devoid of user-specific culture settings. Instead, apply these settings in the business and presentation layers of your applications.

In addition, pay attention to neutral culture when working with your applications. Invariably, you are going to build applications with views that are more dependent on a neutral culture than on a specific culture. For instance, if you have a Spanish version of your application, you’ll probably make this version available to all Spanish speakers regardless of their regions. In many applications, it won’t matter whether the Spanish speaker is from Spain, Mexico, or Argentina. In a case where it does make a difference, use the specific culture settings.

Looking at Your Thread

When the end user requests an ASP.NET page or runs a Windows Forms dialog, the item is executed on a thread from the thread pool. That thread has a culture associated with it. You can get information about the culture of the thread programmatically and then check for particular details about that culture.

To see an example of working with a thread and reading off the culture information of that thread, create a Windows Forms application that is laid out as shown in Figure 6-2.

image from book
Figure 6-2

The idea here is that when the button on the form is pressed, the Button1_Click event is fired and the user’s culture information is read and displayed in the ListBox control. The code for the form is presented here:

  Imports System.Threading Public Class MyCulture     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Dim ci As New _           System.Globalization.CultureInfo( _              Thread.CurrentThread.CurrentCulture.ToString())         ListBox1.Items.Add("CURRENT CULTURE'S INFO")         ListBox1.Items.Add("Culture's Name: " & ci.Name)         ListBox1.Items.Add("Culture's Parent Name: " & ci.Parent.Name)         ListBox1.Items.Add("Culture's Display Name: " & ci.DisplayName)         ListBox1.Items.Add("Culture's English Name: " & ci.EnglishName)         ListBox1.Items.Add("Culture's Native Name: " & ci.NativeName)         ListBox1.Items.Add("Culture's Three Letter ISO Name: " & _            ci.ThreeLetterISOLanguageName)         ListBox1.Items.Add("Calendar Type: " & ci.Calendar.ToString())     End Sub End Class 

Because this form is working with the Thread object, in order for this to work you need to make a reference to the System.Threading namespace at the top of the form, as is done with the Imports statement. Threading is covered in Chapter 24.

This simple form creates a CultureInfo object from the System.Globalization namespace and assigns the culture from the current thread that is running using the Thread.CurrentThread .CurrentCulture.ToString() call. Once the CultureInfo object is populated with the end user’s culture, the details of that culture can be called using a number of available properties that the CultureInfo object offers. Example results of running the form are shown in Figure 6-3.

image from book
Figure 6-3

The CultureInfo object contains a number of properties that provide you with specific culture information. The items displayed are only a small sampling of what is available from the CultureInfo object. From this figure, you can see that the en-US culture is the default setting in which the thread (my thread at this time) executes. In addition to this, you can use the CultureInfo object to get at a lot of other descriptive information about the culture. You can always change a thread’s culture on the overloads provided via a new instantiation of the CultureInfo object, as shown here:

 Imports System.Globalization Imports System.Threading Public Class MyCulture     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Thread.CurrentThread.CurrentCulture = New CultureInfo("th-TH")         Dim ci As CultureInfo = _            System.Threading.Thread.CurrentThread.CurrentCulture         ListBox1.Items.Add("CURRENT CULTURE'S INFO")         ListBox1.Items.Add("Culture's Name: " & ci.Name)         ListBox1.Items.Add("Culture's Parent Name: " & ci.Parent.Name)         ListBox1.Items.Add("Culture's Display Name: " & ci.DisplayName)         ListBox1.Items.Add("Culture's English Name: " & ci.EnglishName)         ListBox1.Items.Add("Culture's Native Name: " & ci.NativeName)         ListBox1.Items.Add("Culture's Three Letter ISO Name: " & _            ci.ThreeLetterISOLanguageName)         ListBox1.Items.Add("Calendar Type: " & ci.Calendar.ToString())     End Sub End Class

In this example, only a couple of lines of code are changed to assign a new instance of the CultureInfo object to the CurrentCulture property of the thread being executed by the application. The culture setting enables the CultureInfo object to define the culture you want to utilize. In this case, the Thai language of Thailand is assigned, and the results produced in the ListBox control are illustrated in Figure 6-4.

image from book
Figure 6-4

From this figure, you can see that the .NET Framework provides the native name of the language used even if it is not a Latin-based letter style. In this case, the results are presented for the Thai language in Thailand, including some of the properties associated with this culture (such as an entirely different calendar than the one used in Western Europe and the United States).




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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