Localization Like the Pros—Globalization and Localization with ASP.NET

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

Localize Like the Pros

© 2004 Wiley Publishing, Inc.

Globalization and Localization with ASP.NET

With ASP.NET applications, localization happens in a similar way to Windows applications. In Chapter 25, we discuss the functionality of ASP.NET applications as this is done in; in this section, we discuss the localization issues of ASP.NET applications. Visual Studio .NET 2003 does not provide the same support for ASP.NET as it does for Windows Forms applications. However, adapting globalization and localization is not rocket science.

With .ASPX files you can assign cultural settings to complete Web sites or to specific pages. Configuring the cultural setting of the Web site in the configuration file web.config makes it independent of the installed operating system. The culture can be configured with the <globalization> element as can be seen in the XML section below. The XML attribute culture defines the current culture of the thread that is used for formatting, while uiCulture defines the culture used by the resource manager:

<configuration>
   <system.web>
      <globalization culture=”en-US” uiCulture=”en-US” />
   </system.web>
</configuration>

If different pages should be accessed from users within different cultures, you can configure the cultural formatting and language output with the page directive that is in the first line of an ASPX file.

<%Page Language=”C#” Culture=”en-US” UICulture=”en-US” %> 

If you want the cultural setting for a Web page to be dependent on the user’s browser settings, you can specify the cultural setting of the thread programmatically with the CurrentCulture and CurrentUICulture properties of the Thread class. The best place to do this is in the file global.asax where the method Application_BeginRequest() is invoked with every request of a page. When this method is invoked, the thread that handles the user request is known, and this thread will flow through all pages to fulfill the user request. Request.UserLanguages returns an array of language strings the user has configured with the browser. Here we use the first language of the list, and pass it to the static method CultureInfo.CreateSpecificCulture(). Internet Explorer sends the string de if the configured culture is German (Germany). Because the string de represents a neutral culture instead of a specific culture, we have to create a specific culture with this method, because it is not allowed to set neutral cultures with the culture of the thread. CreateSpecificCulture() returns the default specific culture if a neutral culture is passed. The default specific culture for de is de-DE. Using the browser, you can also configure a custom language string. If we get a string that is not supported as a culture, we deal with in the exception-handling block by creating a default culture en-US.

protected void Application_BeginRequest(Object sender, 
                                              EventArgs e)
      {
         CultureInfo ci = null;
         try
         {
            ci = CultureInfo.CreateSpecificCulture(
                                Request.UserLanguages[0]);
         }
         catch
         {
            // default for bad user setting
            ci = new CultureInfo("en-US");
         }
         Thread.CurrentThread.CurrentCulture = ci;
         Thread.CurrentThread.CurrentUICulture = ci;
      }

Other than these issues, ASP.NET applications are no different from Windows applications when it comes to localization. You can use formatting and satellite assemblies as resources in the same way as you have done it earlier in this chapter.


Previous_Page_.gif Next_Page_.gif


Personal tools