Manual Culture Recognition for Individual Pages


ASP.NET's automatic culture recognition is very helpful, but there are some situations that it does not cater to. In these circumstances, you need to take a more active role in initializing the culture. As you saw earlier in ASP.NET 1.1, you had to choose a suitable moment in the initialization of the application to set the culture by overriding a method or adding an event that occurred soon enough in the request pipeline. In ASP.NET 2, you are given a method that you can override solely for the purpose of initializing the culture: the Page.InitializeCulture method. One of the problems with the automatic culture-detection process is that it looks at only the first of the user's languages preferences. If the preference is bad, it doesn't check subsequent, alternative language choices. The InitializeCulture method is overridden like this:

 protected override void InitializeCulture() {     if (Request.UserLanguages != null &&         Request.UserLanguages.GetLength(0) > 0)     {         foreach (string userLanguage in Request.UserLanguages)         {             CultureInfo cultureInfo =                 GetCultureInfo(userLanguage, true);             if (cultureInfo != null)             {                 Thread.CurrentThread.CurrentUICulture = cultureInfo;                 Thread.CurrentThread.CurrentCulture =                     CultureInfo.CreateSpecificCulture(                     cultureInfo.Name);                 break;             }         }     } } protected virtual CultureInfo GetCultureInfo(     string userLanguage, bool useUserOverride) {     int semiColonIndex = userLanguage.IndexOf(";");     if (semiColonIndex != -1)         userLanguage = userLanguage.Substring(0, semiColonIndex);     try     {         return new CultureInfo(userLanguage, useUserOverride);     }     catch (ArgumentException)     {         return null;     } } 


This InitializeCulture method gets the user's preference from the Request object and calls GetCultureInfo to get a culture from the user's language preference. The GetCultureInfo method strips any weighting setting from the user's language string (the weighting is specified after the semicolon) and then attempts to create a CultureInfo from the string, ensuring that true is passed for the useUserOverride parameter. The attempt to create the new CultureInfo is enclosed in a TRy/catch block to smother any exception that might result from a bad culture name.

Page.InitializeCulture is called immediately after the assignment of the Culture and UICulture properties. This means that (1) InitializeCulture can be supplementary to the page's Culture and UICulture attributes, and (2) InitializeCulture is called during the FrameworkInitialize method and, therefore, before all page events.


Another variation of the previous theme would be to search through the user's language preferences for one of the languages supported by the application. So if the user's language preferences are Japanese, then French, and then English, and the application supports German, French, and English, the user should get French because this is the supported language that mostly closely matches the user's preferences. ASP.NET applications do not have a global store of the languages supported by the application, so you would need to hard-wire this list in code or in the web.config, or discover it programmatically by probing directory names (e.g., "de", "fr," and "en," in this example). The code to implement this is the same as in the previous example, with the addition of an if to determine whether the user's language preference matches a supported language.

Yet another reason why you might want to override the InitializeCulture method is to set the culture based upon an LCID instead of culture name. See the "Alternate Sort Orders" section of Chapter 6 for more details.

The examples in this section make an implicit assumption that the Culture and UICulture attributes are auto. It should be noted that there is no facility in ASP.NET to determine the value of the Culture and UICulture attributes. Recall from the "How It Works" section that the values of the Culture and UICulture attributes are built into the page's generated class and assigned to the Culture and UICulture properties.

These properties translate this temporary value (i.e., "auto") into a CultureInfo object. The fact that the attribute is "auto" is lost in this translation process, and it is not possible to interrogate these properties to determine what actual value was assigned to them.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net