Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Internationalize Your ASP.NET Applications (Part 1 of 3)  : Page 5

Not only does ASP.NET simplify Web application development, it also provides comprehensive support for internationalization.


advertisement
As you've seen, using the @Page directive changes the culture for a page for all users. But the application needs to store the culture setting for each user. You could let the user select the language each time they connect to your site and store the result on the server in that user's Session, but more realistically, you need to store the culture setting between visits. Therefore, the example site has a drop-down list that lets users select their preferred language and then stores the user's language choice in a permanent cookie that contains a culture string—thus letting you use the selected settings for all future page requests from that user on that machine.

The Application_BeginRequest() method in the global.asax.cs file uses the cookie value. The ASP.NET framework calls this method for each page request. The method reads the cookie value and sets the culture of the thread appropriately. The page code picks up the setting to display the article in the appropriate culture.

First, grab the cookie value to see if it contains a culture string.

protected void Application_BeginRequest(Object sender, EventArgs e) { string culturePref; if (Request.Cookies["CulturePref"] != null) { culturePref = Request.Cookies["CulturePref"].Value; }

Next a try-catch block attempts to set the thread culture using the cookie value. The error handling protects against invalid cookie values.

try { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (culturePref); } catch { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); } Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; }

The final line of the method sets the CurrentUICulture to the same culture value as the CurrentCulture, because in this application there's no need for them to be different.

Another server control creates an interface that lets visitors select their language (and therefore set the culture cookie). The control displays a list of four cultures in a drop-down list using a form. This form is not a server-side form (it does not have a runat="server" attribute) because only one server side form can be used on each page. All the pages in the Web site include the language chooser so it's important to use a standard HTML form to leave the option of using the server-side form on the site. On submission, the form sends the data to a page called culturechooser.aspx that contains the following code in its Page_Load() method.



CultureInfo culture = CultureInfo.CreateSpecificCulture (Request.QueryString["CultureChoice"].ToString()); HttpCookie cookie = new HttpCookie ("CulturePref", culture.Name); cookie.Expires = DateTime.Now.AddYears(100); Response.Cookies.Add(cookie); string referrer = Request.UrlReferrer.ToString(); Response.Redirect(referrer);

The main purpose of this code is to save the culture name as a cookie, but it also redirects back to the page from which the user came. To the user, it looks like the page simply refreshes with the selected language.

You can add the culture chooser server control to the article viewer page using a standard server control statement:

<NewsSite:CultureControl runat="Server" id="CultureCtrl"> </NewsSite:CultureControl>

After adding the control and refreshing the page, you can see the new drop-down list (see Figure 4).

If you select French, the code sets the cookie redirects your browser back to the article page, which now displays the content in French.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date