Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [164]

By Root 5317 0
expected, you can comment out the setting of CurrentCulture.

const String culture = "it-IT";

var cultureInfo = CultureInfo.CreateSpecificCulture(culture);

Thread.CurrentThread.CurrentCulture = cultureInfo;

Thread.CurrentThread.CurrentUICulture = cultureInfo;

Note that the two culture properties might or might not have the same value. For example, you can switch the language of text and messages according to the browser’s configuration while leaving globalization settings (such as dates and currency) constant.

Note

Culture names are a combination of two pieces of information: the language and the country/region that you intend to refer to. The two strings are combined with a dash symbol (-). Often, but not necessarily, the two strings coincide. For example, it-IT means the Italian culture for the country of Italy, whereas en-US indicates the English culture for the United States, which is expected to be different from en-GB or en-SA.

Setting the Current Culture in ASP.NET Pages


If you’re writing an ASP.NET Web Forms application, you don’t need to deal with the Thread class. In ASP.NET, you have culture properties ready-made on the Page class. They are string properties named Culture and UICulture.

The default value being assigned to both properties is auto, meaning that ASP.NET automatically detects the browser’s language for the thread in charge of the request. The getter method of both properties is defined as shown here:

public String UICulture

{

get { return Thread.CurrentThread.CurrentUICulture.DisplayName; }

set { ... }

}

When the auto mode is on for the page culture, the end user is ultimately responsible for determining the language of the pages. All the developers need to do is ensure that proper resource files are available. If no suitable resource file is found for the detected culture, ASP.NET will fall back to the neutral (default) culture.

Obviously, a specific culture can be enforced programmatically or declaratively. You can employ a global setting for the culture by using the section of the web.config file:

A global and fixed setting for culture, however, is hardly what you want most of the time. Most of the time, instead, you want the ability to set the culture programmatically and the ability to change it on the fly as the user clicks an icon or requests a culture-specific URL.

Changing Culture on the Fly


To change the culture programmatically, you need to satisfy two key requirements. First, define how you’ll be retrieving the culture to set. The culture can be a value you read from some database table or perhaps from the ASP.NET cache. It can also be a value you retrieve from the URL. Finally, it can even be a parameter you get via geo-location—that is, by looking at the IP address the user is using for connecting.

After you have the culture ID to set, you have to set it by acting on the current thread, as mentioned earlier. Note that the culture must be set for each request because each request runs on its own thread.

If you intend to read and set the culture as part of the page initialization work, note that the following code, which might appear obvious at first, just won’t work:

void Page_Load(Object sender, EventArgs e)

{

Culture = "IT";

UICulture = "it-IT";

}

The Page_Load handler is fired too late to be effective. The recommended approach consists of overriding the InitializeCulture method on the Page class:

protected override void InitializeCulture()

{

base.InitializeCulture();

Culture = "IT";

UICulture = "it-IT";

}

The setter method of both culture properties will then take care of setting culture information on the current thread. Setting the thread directly does work, but it’s unnecessary to do so.

Changing the language on the fly as the user clicks on a link is a bit trickier. The idea is that you override the InitializeCulture method so that the page reads the language to use from global storage—for example, the ASP.NET Cache or Session.

protected override void

Return Main Page Previous Page Next Page

®Online Book Reader