Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Introducing Resource Files in ASP.NET

Posted by Ravi Varma Thumati on March 30, 2010

If you create Web pages that will be read by speakers of different languages, you must provide a way for readers to view the page in their own language.

One approach is to re-create the page in each language. However, that approach can be labor intensive, error prone, and difficult to maintain as you change the original page.

ASP.NET provides a way for you to create a page that can obtain content and other data based on the preferred language setting for the browser or the user’s explicit choice of language.

Content and other data is referred to as resources and such data can be stored in resource files or other sources

Resource Files

A resource file is an XML file that contains the strings that you want to translate into different languages or paths to images.

The resource file contains key/value pairs. Each pair is an individual resource. Key names are not case sensitive. For example, a resource file might contain a resource with the Button1 key and the Submit value.

You create a separate resource file for each language (for example, English and French) or for a language and culture (for example English [U.K.], English [U.S.]). Each localized resource file has the same key/value pairs;

The only difference is that a localized resource file can contain fewer resources than the default resource file.

Resource files in ASP.NET have a .resx extension. At run time, the .resx file is compiled into an assembly, which is sometimes referred to as a satellite assembly. The .resx files are compiled dynamically, in the same manner as ASP.NET Web pages. The compilation condenses several similar-language resource files into the same assembly.

When you create resource files, start by creating a base .resx file. For each language that you want to support, create a new file that has the same basic file name, but with a language or language and culture (culture name) as part of it. For example, you might create the following files:

  • WebResources.resx (The base resource file. This is the default, or fallback, resource file.)
  • WebResources.es.resx (A resource file for Spanish.)
  • WebResources.es-mx.resx (A resource file for Spanish (Mexico),specifically.)
  • WebResources.de.resx (A resource file for German.)

At run time, ASP.NET uses the resource file that is the best match for the setting of the CurrentUICulture property.

If there is no match for the current UI culture, ASP.NET uses resource fallback; starting with resources for a specific culture, then a neutral culture, and then the default resource file. In this example, the default resource file is WebResource.resx.

Creating Resource Files for ASP.NET Web Sites

In ASP.NET, you can create resource files that have different scope. You can create resource files that are global, which means that you can read the resource file from any page or code that is in the Web site. You can also create local resource files, which store resources for a single ASP.NET Web page (.aspx file).

Global Resource Files

You create a global resource file by putting it in the reserved folder App_GlobalResources at the root of the application. Any .resx file that is in the App_GlobalResources folder has global scope. Additionally, ASP.NET generates a strongly typed object, which gives developers a simple way to programmatically access global resources.

Local Resource Files

A local resources file is one that applies to only one ASP.NET page (an ASP.NET page with the extension of .aspx, .ascx, .master, and so on). You put local resource files in folders that have the reserved name App_LocalResources. App_LocalResources folders can exist in any folder in the application, unlike the root App_GlobalResources folder.

Working with Resources in Web Pages

After you create resource files, you can use them in ASP.NET Web pages. You typically use resources to fill the property values of controls on the page. For example, you might use resources to set the Text property of a Button control, instead of hard-coding the property to a specific string.

Explicit Localization

You must use a resource expression for each property that you want to set.

A Button control that is configured to set the Text property from a global resource file might look similar to the following:

<asp:Button ID=”Button1″ runat=”server”

Text=”<%$ Resources:WebResources, Button1Caption %>” />

The resource expression takes the following form, where Class is optional, unless the resource is a global one, and ResourceID is required:

<%$ Resources:Class, ResourceID %>

The Class value identifies the resource file to use when you use global resources. When .resx files are compiled, the base file name, without extensions, is used as the class name of the resulting assembly, explicitly.

The ResourceID value is the identifier of the resource to read. In the preceding example, the Text property for the button is read from the global resource file WebResources.resx.

You can specify an explicit resource expression or an implicit resource expression for a control, but not both. The following declarative syntax on a Button control will result in a parser error:

<asp:Button runat=”server” meta:resourcekey=”Button1Resource1″ Text=”<%$ Resources:WebResources, Button1Caption %>” />

Advertisements

One Response to “Introducing Resource Files in ASP.NET”

  1. John said

    There’s a Visual Studio add-in at http:://www.hexadigm.com that will pull out all strings from your .NET solution and create a single file out of them. The file can then be translated using a free application at the site (available for download). The add-in will then incorporate the localized strings back into the solution.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: