Localization with ASP.NET


With ASP.NET applications, localization happens in a similar way to Windows applications. Chapter 32, “ASP.NET Pages,” discusses the functionality of ASP.NET applications; this section discusses the localization issues of ASP.NET applications. ASP.NET 2.0 and Visual Studio 2005 have many new features to support localization. The basic concepts of localization and globalization are the same as discussed before. However, some specific issues are associated with ASP.NET.

As you’ve already learned, with ASP.NET you have to differentiate between the user interface culture and the culture used for formatting. Both of these cultures can be defined on a Web and page level, as well as programmatically.

To be independent of the Web server’s operating system, the culture and user interface culture can be defined with the <globalization> element in the configuration file web.config:

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

If the configuration should be different for specific Web pages, the Page directive allows assigning the culture:

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

The user can configure the language with the Browser. With Internet Explorer, this setting is defined with the Language Preference options (see Figure 20-22).

image from book
Figure 20-22

If the page language should be set depending on the language setting of the client, the culture of the thread can be set programmatically to the language setting that is received from the client. ASP.NET 2.0 has an automatic setting that does just that. Setting the culture to the value Auto sets the culture of the thread depending on the client’s settings.

  <%Page Language="C#" Culture="Auto" UICulture="Auto" %> 

In dealing with resources, ASP.NET differentiates resources that are used for the complete Web site and resources that are only needed within a page.

If a resource is used within a page, you can create resources for the page by selecting the Visual Studio 2005 menu Tools image from book Generate Local Resource in the design view. This way the subdirectory App_LocalResources is created where a resource file for every page is stored. These resources can be localized similarly to Windows applications. The association between the Web controls and the local resource files happen with a meta:resourcekey attribute as shown here with the ASP.NET Label control. LabelResource1 is the name of the resource that can be changed in the local resource file:

  <asp:Label  Runat="server" Text="Label"     meta:resourcekey="LabelResource1"></asp:Label> 

For the resources that should be shared between multiple pages, you have to create a subdirectory, Appl_GlobalResources. In this directory you can add resource files, for example, Messages.resx with its resources. To associate the Web controls with these resources, you can use Expressions in the property editor. Clicking the Expressions button opens the Expressions dialog (see Figure 20-23). Here you can select the expression type Resources, set the name of the ClassKey (which is the name of the resource file - here, a strongly typed resource file is generated), and the name of the ResourceKey, which is the name of the resource.

image from book
Figure 20-23

In the ASPX file, you can see the association to the resource with the binding expressions syntax <%$:

  <asp:Label  Runat="server"     Text="<%$ Resources:Messages, String1 %>"> </asp:Label> 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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