13.2. 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 settings coming from the server in which the application is run or from a setting applied by the end user (the client). 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, and Languages) that specifies a language and region using two-letter codes separated by a dash. The following table provides examples of some culture definitions.

Culture CodeDescription
en-USEnglish language; United States
en-GBEnglish language; United Kingdom (Great Britain)
en-AUEnglish language; Australia
en-CAEnglish language; Canada

This table defines four distinct cultures that have some similarities and some differences. All four cultures speak the same language (English). For this reason, the language code of en is used in each culture setting. After the language setting comes 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 that is used in the United Kingdom, and so forth. Beyond language, differences exist in the way dates and numerical values are represented. This is why a culture's language and region are presented together.

The differences do not 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.

13.2.1. Understanding Culture Types

The culture definition you have just seen 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 is associated with a specified neutral culture. For instance, the English language cultures shown in the previous table are separate, but they also all belong to one neutral culture EN (English). The diagram presented in Figure 13-1 illustrates how these culture types relate to one another.

Figure 13-1. Figure 13-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.

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

13.2.2. Understanding ASP.NET Culture

When the end user requests an ASP.NET page, this Web page is executed on a thread from the thread pool. The 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. You can view information about the thread in which it runs by printing out some information from the server. This is illustrated in Listing 13-1.

Example 13-1. Viewing the thread's culture in ASP.NET
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization"%>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
        Response.Write("<b><u>CURRENT CULTURE'S INFO</u></b>");
        Response.Write("<p><b>Culture's Name:</b> " + ci.Name + "<br>");
        Response.Write("<b>Culture's Parent Name:</b> " + ci.Parent.Name +
           "<br>");
        Response.Write("<b>Culture's Display Name:</b> " + ci.DisplayName +
           "<br>");
        Response.Write("<b>Culture's English Name:</b> " + ci.EnglishName +
           "<br>");
        Response.Write("<b>Culture's Native Name:</b> " + ci.NativeName +
           "<br>");
        Response.Write("<b>Culture's Three Letter ISO Name:</b> " +
           ci.Parent.ThreeLetterISOLanguageName + "<br>");
        Response.Write("<b>Calendar Type:</b> " + ci.Calendar + "</p >");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Viewing the thread's culture</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

Running this page shows the details of the culture in the thread in which ASP.NET is running. This bit of code in the Page_Load() event checks the CurrentCulture property. You can place the result of this value in a CultureInfo object. To get at this object, you import the System.Globalization namespace in your code. The CultureInfo object contains a number of properties that provides you with specific culture information. The following items, which are displayed in a series of simple Response.Write() statements, are only a small sampling of what is actually available. Running this page produces results similar to what is shown in Figure 13-2.

Figure 13-2. Figure 13-2

From this figure, you can see that the en-US culture is the default setting in which the ASP.NET thread executes. In addition to this, you can use the CultureInfo object to get at a lot of other descriptive information about the culture.

If you are interested in controlling the culture of the thread yourself, there are a number of ways to accomplish this task. One way to do this is to programmatically change the thread's culture using the CultureInfo object. This is shown here in Listing 13-2.

Example 13-2. Changing the thread culture programmatically
protected void Page_Load(object sender, EventArgs e)
{

 // Now I am defining the culture to Thai
 System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH");

 CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
 Response.Write("<b><u>CURRENT CULTURE'S INFO</u></b>");
 Response.Write("<p><b>Culture's Name:</b> " + ci.Name + "<br>");
 Response.Write("<b>Culture's Parent Name:</b> " + ci.Parent.Name +
    "<br>");
 Response.Write("<b>Culture's Display Name:</b> " + ci.DisplayName +
    "<br>");
 Response.Write("<b>Culture's English Name:</b> " + ci.EnglishName +

"<br>");
 Response.Write("<b>Culture's Native Name:</b> " + ci.NativeName +
    "<br>");
 Response.Write("<b>Culture's Three Letter ISO Name:</b> " +
    ci.Parent.ThreeLetterISOLanguageName + "<br>");
 Response.Write("<b>Calendar Type:</b> " + ci.Calendar + "</p>");
}

In this case, only a single line of code is added to assign a new instance of the CultureInfo object to the CurrentCulture property of the thread being executed by ASP.NET. 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 browser are illustrated in Figure 13-3.

Figure 13-3. Figure 13-3

From this figure, you can see that the .NET Framework goes so far as to provide 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, and some of the properties that are associated with this culture (such as an entirely different calendar than the one used in Western Europe and the United States). Remember that you reference System.Globalization to get at the CultureInfo object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset