Translating Values and Behaviors


In the process of globalizing your .NET application, you may notice a number of aspects that are done differently than building an application that is devoid of globalization, including how dates are represented and how currencies are shown. This section looks at some of these issues.

Understanding Differences in Dates

Different cultures specify dates and time very differently. For instance, take the following date as an example:

 08/11/2007

What is this date exactly? Is it August 11, 2007 or is it November 8, 2007? Again, when storing values such as date/time stamps in a database or other type of back-end system, always use the same culture (or invariant culture) for these items so that you avoid any mistakes. It should be the job of the business logic layer or the presentation layer to convert these items for use by the end user.

Setting the culture at the server level in ASP.NET or within a Windows Forms application, as shown in the earlier samples, enables your .NET application to make these conversions for you. You can also simply assign a new culture to the thread in which the application is running. For instance, consider the following code:

  Imports System.Globalization Imports System.Threading Public Class Differences     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Dim dt As DateTime = New DateTime(2007, 8, 11, 11, 12, 10, 10)         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")         ListBox1.Items.Add( _           Thread.CurrentThread.CurrentCulture.EnglishName & " : " & _           dt.ToString())         Thread.CurrentThread.CurrentCulture = New CultureInfo("ru-RU")         ListBox1.Items.Add( _           Thread.CurrentThread.CurrentCulture.EnglishName & " : " & _           dt.ToString())         Thread.CurrentThread.CurrentCulture = New CultureInfo("fi-FI")         ListBox1.Items.Add( _           Thread.CurrentThread.CurrentCulture.EnglishName & " : " & _           dt.ToString())         Thread.CurrentThread.CurrentCulture = New CultureInfo("th-TH")         ListBox1.Items.Add( _           Thread.CurrentThread.CurrentCulture.EnglishName & " : " & _           dt.ToString())     End Sub End Class  

In this case, a Windows Forms application is used again and four different cultures are utilized in the output. The date/time construction used by the defined culture is written to the ListBox control. The result from this code operation is presented in Figure 6-7.

image from book
Figure 6-7

As you can see, the formats used to represent a date/time value are dramatically different from one another - and one of the cultures, the Thai culture (th-TH), even uses an entirely different calendar that labels 2007 as 2550.

Understanding Differences in Numbers and Currencies

In addition to date/time values, numbers are constructed quite differently from one culture to the next. How can a number be represented differently in different cultures? Well, it has less to do with the actual number (although certain cultures use different number symbols) and more to do with how the number separators are used for decimals or for showing amounts such as thousands, millions, and more. For instance, in the English culture of the United States (en-US), numbers are represented in the following fashion:

 5,123,456.00

From this example, you can see that the en-US culture uses a comma as a separator for thousands and a period for signifying the start of any decimals that might appear after the number is presented. It is quite different when working with other cultures. The following code block shows an example of representing numbers in other cultures:

  Imports System.Globalization Imports System.Threading Public Class Differences     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click          Dim myNumber As Double = 5123456.0         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("n"))         Thread.CurrentThread.CurrentCulture = New CultureInfo("vi-VN")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("n"))         Thread.CurrentThread.CurrentCulture = New CultureInfo("fi-FI")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("n"))         Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-CH")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("n"))     End Sub End Class 

Running this short example produces the results presented in Figure 6-8.

image from book
Figure 6-8

Clearly, other cultures show numbers in quite a different format than that of the en-US culture. The second culture listed in the figure, vi-VN (Vietnamese in Vietnam), constructs a number exactly the opposite from the way it is constructed in en-US. The Vietnamese culture uses periods for the thousand separators and a comma for signifying decimals. Finnish uses spaces for the thousand separators and a comma for the decimal separator, whereas the French-speaking Swiss use a high comma for separating thousands and a period for the decimal separator. As you can see, it is important to “translate” numbers to the proper construction so that users of your application can properly understand the numbers represented.

Another scenario in which you represent numbers is when working with currencies. It is one thing to convert currencies so that end users understand the proper value of an item, but it is another to translate the construction of the currency just as you would a basic number.

Each culture has a distinct currency symbol used to signify that a number represented is an actual currency value. For instance, the en-US culture represents a currency in the following format:

 $5,123,456.00

The en-US culture uses a U.S. dollar symbol ($), and the location of this symbol is just as important as the symbol itself. For en-US, the $ symbol directly precedes the currency value (with no space in between the symbol and the first character of the number). Other cultures use different symbols to represent currency and often place those currency symbols in different locations. Try changing the previous code block so that it now represents the number as a currency. The necessary changes are shown here:

 Imports System.Globalization Imports System.Threading Public Class Differences     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Dim myNumber As Double = 5123456.0         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("c"))         Thread.CurrentThread.CurrentCulture = New CultureInfo("vi-VN")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("c"))         Thread.CurrentThread.CurrentCulture = New CultureInfo("fi-FI")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("c"))         Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-CH")         ListBox1.Items.Add(Thread.CurrentThread.CurrentCulture.EnglishName & _            " : " & myNumber.ToString("c"))     End Sub End Class

Running this example shows how these cultures represent currency values (see Figure 6-9).

image from book
Figure 6-9

Not only are the numbers constructed quite differently from one another, but the currency symbol and the location of the symbol in regard to the number are quite different as well.

When working with currencies, note that when you are using currencies on an ASP.NET page, you have provided an automatic culture setting for the page as a whole (such as setting the culture in the @Page directive). You must specify a specific culture for the currency that is the same in all cases unless you are actually doing a currency conversion. For instance, if you are specifying a U.S. dollar currency value on your ASP.NET page, you don’t want to specify that the culture of the currency is something else (for example, the euro). An exception would be if you actually performed a currency conversion and showed the appropriate euro value along with the culture specification of the currency. Therefore, if you are using an automatic culture setting on your ASP.NET page and you are not converting the currency, you perform something similar to what is illustrated in the following code for currency values:

  Dim myNumber As Double = 5123456.00 Dim usCurr As CultureInfo = New CultureInfo("en-US") Response.Write(myNumber.ToString("c", usCurr)) 

Understanding Differences in Sorting Strings

You have learned to translate textual values and alter the construction of the numbers, date/time values, currencies, and more when you are globalizing an application. You should also take note when applying culture settings to some of the programmatic behaviors that you establish for values in your applications. One operation that can change based upon the culture setting applied is how .NET sorts strings. You might think that all cultures sort strings in the same way (and generally they do), but sometimes differences exist. To give you an example, the following example shows you a sorting operation occurring in the en-US culture:

  Imports System.Globalization Imports System.Threading Public Class Differences     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")         Dim myList As List(Of String) = New List(Of String)         myList.Add("Washington D.C.")         myList.Add("Helsinki")         myList.Add("Moscow")         myList.Add("Warsaw")         myList.Add("Vienna")         myList.Add("Tokyo")         myList.Sort()         For Each item As String In myList             ListBox1.Items.Add(item.ToString())         Next     End Sub End Class 

For this example to work, you have to reference the System.Collections and the System.Collections.Generic namespaces because this example makes use of the List(Of String) object.

In this example, a generic list of capitals from various countries of the world is created in random order. Then the Sort() method of the generic List(Of String) object is invoked. This sorting operation sorts the strings based upon how sorting is done for the defined culture in which the application thread is running. The preceding code shows the sorting as it is done for the en-US culture. The result of this operation is presented in Figure 6-10.

image from book
Figure 6-10

This is pretty much what you would expect. Now, however, change the previous example so that the culture is set to the Finnish culture:

 Imports System.Globalization Imports System.Threading Public Class Differences     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Thread.CurrentThread.CurrentCulture = New CultureInfo("fi-FI")         Dim myList As List(Of String) = New List(Of String)         myList.Add("Washington D.C.")         myList.Add("Helsinki")         myList.Add("Moscow")         myList.Add("Warsaw")         myList.Add("Vienna")         myList.Add("Tokyo")         myList.Sort()         For Each item As String In myList             ListBox1.Items.Add(item.ToString())         Next     End Sub End Class

If you run the same bit of code under the Finnish culture setting, you get the results presented in Figure 6-11.

image from book
Figure 6-11

If you examine the difference between the Finnish culture sorting done in Figure 6-11 and the U.S. English culture sorting done in Figure 6-10, you see that the city of Vienna is in a different place in the Finnish version. This is because, in the Finnish language, there is no difference between the letter V and the letter W. Because no difference exists, if you are sorting using the Finnish culture setting, then Vi comes after Wa, and thus Vienna comes last in the list of strings in the sorting operation.




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