Using the CultureInfo Class


Using the CultureInfo Class

The CultureInfo class contains information about more than 150 different cultures. You can use the methods of this class in your code to retrieve information about a specific culture and use the information when formatting values such as dates, numbers, and currency amounts.

To represent a culture with the CultureInfo class, you can instantiate the class by passing a culture name to the class constructor like this:

Dim culture As New CultureInfo("de-DE")


You can also use any of the following methods of the CultureInfo class to retrieve information about a culture or cultures:

  • CreateSpecificCulture Enables you to create a CultureInfo object by supplying the name of a specific culture.

  • GetCultureInfo Enables you to create a CultureInfo object by supplying an identifier, culture name, or CompareInfo and TextInfo object.

  • GetCultureInfoByIetfLanguageTag Enables you to create a CultureInfo object efficiently by supplying a culture name.

  • GetCultures Enables you to retrieve an array of cultures.

The CultureInfo class lives in the System.Globalization namespace. Before you can use the CultureInfo class, you need to import this namespace.

Using the CultureInfo Class to Format String Values

To this point, the culture has been set at the level of an individual ASP.NET page or the level of an entire ASP.NET application. However, you might need to take advantage of locale-specific formatting at a more granular level. You can use the CultureInfo class to format a particular value independent of the Culture set for the page.

When you use the ToString() method to format dates, times, numbers, and currency amounts, you can supply an additional parameter that formats the value in accordance with a specific culture. For example, the page in Listing 24.9 formats two sets of date and time values.

Listing 24.9. ToStringCulture.aspx

<%@ Page Language="VB" %> <%@ Import Namespace="System.Globalization" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Private  Sub Page_Load()         ' Get German Culture Info         Dim gCulture As New CultureInfo("de-DE")         ' Use culture when formatting strings         lblGermanDate.Text = DateTime.Now.ToString("D", gCulture)         lblGermanPrice.Text = (512.33D).ToString("c", gCulture)         ' Get Indonesian Culture Info         Dim iCulture As New CultureInfo("id-ID")         ' Use culture when formatting strings         lblIndonesianDate.Text = DateTime.Now.ToString("D", iCulture)         lblIndonesianPrice.Text = (512.33D).ToString("c", iCulture)     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>ToString Culture</title> </head> <body>     <form  runat="server">     <div>     <h1>German</h1>     Today's date is:     <br />     <asp:Label                  Runat="server" />     <br /><br />     The price of the product is:     <br />     <asp:Label                  Runat="server" />     <h1>Indonesian</h1>     Today's date is:     <br />     <asp:Label                  Runat="server" />     <br /><br />     The price of the product is:     <br />     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

The first date and time is formatted with German cultural conventions, and the second date and time is formatted with Indonesian cultural conventions (see Figure 24.7). Notice that two CultureInfo objects, corresponding to two cultures, are created in the Page_Load() method.

Figure 24.7. Formatting with the ToString() method.


Comparing and Sorting String Values

Different cultures follow different conventions when comparing and sorting string values. If you need to compare or sort string values in your code, then you should use the String.Compare() method and optionally supply the method with an instance of the CultureInfo object.

The String.Compare() method returns one of the following values:

  • Negative Integer The first string is less than the second string

  • Zero The first string is equal to the second string

  • Positive Integer The first string is greater than the second string

For example, the following conditional compares two strings, using the current culture set for the page:

If String.Compare("Hello", "Hello") = 0 Then   lblResult.Text = "The strings are the same!" End If 


The following conditional uses a specific culture to perform a string comparison:

If String.Compare("Hello", "Hello", True, New CultureInfo("de-DE")) = 0 Then   lblResult.Text = "The strings are the same!" End If 


In this case, the first two parameters passed to the String.Compare() method are the strings being compared. The third parameter indicates whether the comparison performed should be case sensitive or not. Finally, the last parameter represents a CultureInfo object.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

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