Cultural Applications...

.NET has the capabilities to check language settings at the browser level, and perform some action based on them. This article will concentrate on redirecting the user to a cultural specific site based on these cultural settings.


By: Brian Mains Date: May 23, 2004 Download the code.

If you've ever been on a site similar to http://www.dell.com, you’ve probably noticed that it has web sites specific to each country that it does business with. That site then conforms to the language of choice for the particular region in that country. The site usually determines which language is appropriate for the user, and by default, redirects to the desired site. .NET has the capabilities to check language settings at the browser level, and perform some action based on them. This article will concentrate on redirecting the user to a culture specific site, based on these culture settings.

To determine your default and available culture settings, open up Internet Explorer. Select the Tools menu, and click Internet Options. Select the Languages button, which will show all of the supported languages. You can use the Add and Remove buttons to adjust these settings appropriately. A few of the available languages (and their appropriate language code) are shown below; these languages are typically referred to as Neutral cultures:

Arabic = ar
Chinese = zh
English = en
French = fr
German = de
Italy = it
Japanese = ja
Spanish = es

In addition, each of these languages has region-specific attributes available. These languages are typically referred to as Specific cultures:

English – Australia = en-au
English – Belize = en-bz
English – Canada = en-ca
English – Ireland = en-ie
English – New Zealand = en-nz
English – United Kingdom = en-gb

To access this cultural information from the browser settings, the Request.UserLanguages property is used to retrieve the language code values of the languages specified in the Language settings for the browser. For example, if all of the above examples were added to the Language settings, the Request.UserLanguages property would return an array of the language codes ("ar", "zh", "en-US", etc.). To obtain culture-specific information, the CultureInfo class contains all kinds of properties and methods that the language supports, such as number format, date/time format, calendar information, etc.

The following section of code gets the primary language specified for the user (the first entry in the Language settings list), and assigns it to a local variable. This value is then used for the culture settings of the current thread, as well as redirect to a culture specific site based on those settings:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  'Get the current user language
  Dim strLanguage As String = Request.UserLanguages(0)
  'Set the cultural information for the current thread
  Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strLanguage)
  Thread.CurrentThread.CurrentUICulture = New CultureInfo(strLanguage)

  'Check the specific culture information and redirect based on that;
  'ignore the region-specific information
  Select Case (strLanguage.Substring(0, 2).ToLower())
    Case "es"
      Response.Redirect("es/default.aspx", False)
    Case "de"
      Response.Redirect("de/default.aspx", False)
    Case Else
      Response.Redirect("en/default.aspx", False)
  End Select
End Sub

As shown above, each supported language has its own folder with its own web site. If the current language is Spanish, the browser redirects to the Spanish version of the web site. If the current language is German, the browser redirects to the German version of the web site. Otherwise, the browser redirects to the English version of the web site.

In addition, the items available for language selections can be populated to a drop down list control, which allows the user to select a more specific site for their needs. For the code example attached, the document retrieves all of the languages specified in the language settings option, and adds them to a drop down list. Some of the language codes contained extra text (which appears to be a weighted value) that was in the appearance of “;q=0.8”. This value is not part of the language code, and the code assumes that the language code is invalid. A regular expression Replace method searches for this text, and removes it from the string.

The name of the culture is added to the drop down list, written in the text format specified by the language. Lastly, based on the folder structure scheme, the current path is checked to determine the current default language to display. For example, if the current web site is the Spanish version of the site, the path is checked for the “/es/” folder, and if true, the Spanish language value is selected.

For Each strLanguage As String In Request.UserLanguages
  'The ;q=0.n causes problems, so it can't be there when passed to this function
  strLanguage = Regex.Replace(strLanguage, ";.*", "")

  Dim objInfo As CultureInfo = CultureInfo.CreateSpecificCulture(strLanguage)
  'Add the native name to the drop down list, as well as the language code;
  'for example, for en-US, the native name is “English(United States)”, the name is "en-US"
  DropDownList1.Items.Add(New ListItem(objInfo.NativeName, objInfo.Name))

  'Make sure that for the current page that the default is selected
  If (Request.CurrentExecutionFilePath.IndexOf(“/” & strLanguage.Substring(0, 2) & "/") >= 0) Then
    DropDownList1.Items(DropDownList1.Items.Count - 1).Selected = True
  End If
Next

About the Code

The code attached is an ASP.NET project. Please copy the folder to your wwwroot directory in order to use this example. Additionally, you may have to set this project up as an application for the virtual directory. In IIS, right-click the Caching virtual directory, select Properties. Where it says "Application Name", the button next to it should say "Remove". If it doesn't, hit the create button to create the application.

The default.aspx page at the root redirects to the appropriate cultural version of the site, as specified by the Language settings in the browser. If you want to change these settings and retry the redirection, restart the project. The next section explains how to test for the redirection.

How It Works

To test out the language redirection for this project, follow these instructions:

  1. Open the language settings in the browser using the directions above. Your language settings should appear like the screen below:
  2. Run the web project. The Spanish version of the site will appear in the browser. Unfortunately, I don't know any Spanish, so I can't make my example authentic.
  3. Stop the web project, open the language settings again, and move the German(Switzerland) entry to the top of the list.
  4. Run the web project. The German version of the site will appear in the browser.
  5. Stop the web project, open the language settings again, and move the Arabic (Algeria) entry to the top of the list.
  6. Run the web project. The English version of the site (default) will appear in the browser.
  7. Lastly, try again with the English language setting. Run the project, which the English version of the site will appear in the browser.