Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

All Web Pages Are Local (When You're Reading Them)


Web sites designed to reach an international audience need to be easily localized. For this reason, strings and other auxiliary resources that change based on culture should not be hard-coded but rather placed in a separate resource file. A localizable Web page uses resources instead of hard-coded text to flesh out the user interface of contained controls. Once a resource assembly is linked to the application, ASP.NET can select the correct property at run time according to the user's language and culture. In ASP.NET 1.x, developers had to create satellite assemblies manually to store and deploy resources.

With ASP.NET 2.0 managing resource assemblies is much easier. All that developers have to do is creating resource files—that is XML files with a .resx extension and filled with name/value pairs for ID and text. The ASP.NET runtime will take care of creating and linking satellite assemblies to the application on the fly.

In an ASP.NET 2.0 application there are two types of resources—global and local—located in distinct system folders: App_GlobalResources and App_LocalResources.

A local resource is a resource file specific to a page. A simple naming convention binds the file to the page. If the page is named sample.aspx, its corresponding resource file is sample.aspx.resx. To be precise, this resource file is language neutral and has no culture defined. To create a resource assembly for a specific culture, say French, you need to name the resource file as follows: sample.aspx.fr.resx. Generally, the fr string should be replaced with any other equivalent string that identifies a language, such as de for German or es for Spanish. Here's a very simple page with support for local resources.

<%@ Page
Language="C#" meta:resourcekey="PageResource1" 
         UICulture="auto"
%>
<html> 
<body>
<form
id="Form1" runat="server">
    <asp:Button
ID="btn" Runat="server" 
         meta:resourcekey="BtnResource1"
/>
</form>
</body>
</html>

The first thing to note is the UICulture page attribute set to auto. It means that ASP.NET detects the browser's locale and loads the proper satellite assembly, if any is found. The page itself and each constituent control are given a resource key. The page .resx file contains entries in the form ResourceKey.PropertyName. For example, the Text property of the button is implicitly bound to the BtnResource1.Text entry in the .resx file. You don't have to write a single line of code for this mapping to take place. You are only requested to populate the resource files as outlined.

Global resources are still located in .resx files, except that these resource files are contained in the App_GlobalResources folder and are accessible from within any page in the application. Within the page, you reference global resources as below:

<asp:Label Runat="server"

     Text="<%$
Resources:YourResourceFile, Key1 %>" />

Resources is the keyword that identifies the expression, whereas YourResourceFile is the name of the .resx file that contains the resources. (No extensions and path information is required.) Finally, Key1 is the name of the entry to use.

Declarative localization works automatically, meaning that you don't need to specify how to read information about each property from a resource file. However, at times you need more direct control over how properties are set.

Both the Page and HttpContext classes support a pair of methods to retrieve the content of resources embedded in the application. In particular, GetGlobalResourceObject retrieves a global resource whereas GetLocalResourceObject does the same for a local resource.

msg1.Text = (string)
HttpContext.GetGlobalResourceObject(
    
"Test", "MyString");
msg2.Text = (string)
HttpContext.GetLocalResourceObject(
     "Page.aspx",
"PageResource1.Title");

The first parameter you pass to GetGlobalResourceObject indicates the name of the .resx resource file without extension; the second parameter is the name of the resource to retrieve. As for GetLocalResourceObject, the first argument indicates the virtual path of the page; the second is the name of the resource.

ASP.NET 2.0 builds on the foundation of ASP.NET 1.x localization features and overall improves the functionality available to Web developers, specifically with localization expressions and automatic culture selection.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.