Programming Microsoft ASP.NET 4 - Dino Esposito [165]
{
base.InitializeCulture();
UICulture = DetermineLocaleToEnforce();
}
private String DetermineLocaleToEnforce()
{
var language = Cache["Language"] as String;
if (String.IsNullOrEmpty(language))
language = "en-US";
return language;
}
When the user interacts with the user interface in the postback, you simply read the newly selected language, update the storage, and then redirect to the same page for a refresh:
protected void Button1_Click(Object sender, EventArgs e)
{
var languageInfo = GetCurrentLocale();
Cache["Language"] = languageInfo;
Response.Redirect("/private/moneyintl.aspx");
}
private String GetCurrentLocale()
{
return Languages.SelectedValue;
}
This is good enough if your user interface is limited to listing a few image buttons with flags. If you want a drop-down list of languages to choose from, you also must take care of re-indexing the list of items. This translates into some extra code in Page_Load.
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
var languageCode = DetermineLocaleToEnforce();
var item = Languages.Items.FindByValue(languageCode);
Languages.SelectedIndex = Languages.Items.IndexOf(item);
}
}
Nicely enough, implementing the same feature is much simpler in ASP.NET MVC, even though ASP.NET MVC shares exactly the same run-time environment as ASP.NET Web Forms. The postback model, which is great at many things, makes some other things a bit harder than expected. In ASP.NET MVC, you can simply create your own action invoker and then, for each and every controller method, you retrieve the language and set it to the current thread. The action invoker is a way to intercept the execution of action methods in a rather unobtrusive way. In Web Forms, you can achieve the same result by using an HTTP module that kicks in for every request, reads the currently set language, and sets the culture on the current thread.
Note
More and more Web sites check the location from where a user is connected and suggest a language and a culture. This feature requires an API that looks up the IP address and maps that to a country/region and then a culture. Some browsers (for example, Firefox 3.5, Safari, iPhone, and Opera) have built-in geo-location capabilities that work according to the W3C API. (See http://www.mozilla.com/firefox/geolocation.)
To support other browsers (including Internet Explorer), you can resort to third-party services such as Google Gears. Google Gears is a plug-in that extends your browser in various ways, including adding a geo-location API that returns the country/region of the user from the current geographical location. Note that Google returns the ISO 3166 code of the country/region (for example, GB for the United Kingdom) and its full name. From here, you have to determine the language to use. The country/region code doesn’t always match the language. For the United Kingdom, the language is en. To install Google Gears, pay a visit to http://gears.google.com.
Adding Resources to Pages
An ASP.NET page is usually made of a bunch of auxiliary resources including script files, cascading style sheets (CSS), and images. When the browser downloads a page, it usually places a number of independent requests to the Web server and tracks when the document is ready. The display of the document, however, might begin before the entire document (and related links) has been downloaded. Developers of heavy pages made of several resources (a few dozens is not unusual) resort to a number of techniques to optimize the download experience of their pages. Let’s review a few interesting techniques that simplify the management of scripts, images, and other resources.
Using Script Files
The only HTML-supported way of linking script files to a page is via the
-->