ASP.NET Web Page Resources Overview

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 enables you to create a page that can obtain content and other data based on the preferred language setting for the browser or based on 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

In the ASP.NET Web page, you configure controls to get their property values from resources. At run time, the resource expressions are replaced by resources from the appropriate resource file.

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 key Button1 and the value Submit.

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. The built-in language fallback process then handles loading the neutral or default resource.

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

When you create resource files, you start by creating a base .resx file. For each language that you want to support, create a new file that has the same file name. But in the name, include the language or the language and culture (culture name). For a list of culture names, see the CultureInfo class. For example, you might create the following files:

WebResources.resx

The base resource file. This is the default (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. The UI culture for the thread is set according to the UI culture of the page. For example, if the current UI culture is Spanish, ASP.NET uses the compiled version of the WebResources.es.resx file. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads 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 that gives you 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 or user control (an ASP.NET file that has a file-name extension of .aspx, .ascx, or .master). You put local resource files in folders that have the reserved name App_LocalResources. Unlike the root App_GlobalResources folder, App_LocalResources folders can be in any folder in the application. You associate a set of resources files with a specific Web page by using the name of the resource file.

For example, if you have a page named Default.aspx in the App_LocalResources folder, you might create the following files:

  • Default.aspx.resx. This is the default local resource file (the fallback resource file) if no language match is found.

  • Default.aspx.es.resx. This is the resource file for Spanish, without culture information.

  • Default.aspx.es-mx.resx. This is the resource file for Spanish (Mexico) specifically.

  • Default.aspx.fr.resx. This is the resource file for French, without culture information.

The base name of the file matches the page file name, followed by a language and culture name, and ending with the extension .resx.

Choosing Between Global and Local Resource Files

You can use any combination of global and local resource files in the Web application. Generally, you add resources to a global resource file when you want to share the resources between pages. Resources in global resource files are also strongly typed for when you want to access the files programmatically.

However, global resource files can become large, if you store all localized resources in them. Global resource files can also be more difficult to manage, if more than one developer is working on different pages but in a single resource file.

Local resource files make it easier to manage resources for a single ASP.NET Web page. But you cannot share resources between pages. Additionally, you might create lots of local resource files, if you have many pages that must be localized into many languages. If sites are large with many folders and languages, local resources can quickly expand the number of assemblies in the application domain.

When you make a change to a default resource file, either local or global, ASP.NET recompiles the resources and restarts the ASP.NET application. This can affect the overall performance of your site. If you add satellite resource files, it does not cause a recompilation of resources, but the ASP.NET application will restart.

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.

To use resources to set control property values, you can use implicit localization or explicit localization, as follows:

  • Implicit localization works with local resources and lets you automatically set control properties to matching resources.

  • Explicit localization lets you use a resource expression to set a control property to a specific resource in a local or global resource file.

Implicit Localization with Local Resources

If you have created local resource files for a specific page, you can use implicit localization to fill the property values for a control from the resource file. In implicit localization, ASP.NET reads a resource file and matches resources to property values.

To use implicit localization, you must use a naming convention for resources in the local resource file that uses the following pattern:

Key.Property

For example, if you are creating resources for a Button control named Button1, you might create the following key/value pairs in the local resource file:

Button1.Text
Button1.BackColor
Label1.Text

You can use any name for Key, but Property must match the name of a property of the control that you are localizing.

In the page, you use a special meta attribute in the markup for the control to specify implicit localization. You do not have to explicitly specify which properties are localized. A Button control that is configured for implicit localization might resemble the following:

meta:resourcekey="Button1" />

The resourcekey value matches a key in the corresponding resource file. At run time, ASP.NET matches resources to control properties using the control label as the resourcekey. If a property value is defined in the resource file, ASP.NET substitutes the resource value for the property.

Explicit Localization

Alternatively, you can use explicit localization, where you use a resource expression. Unlike implicit 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 resemble the following:

Text="" />

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. If you want to use resources from a local resource file (one that matches the current page name), you do not have to include a class name. This is because ASP.NET matches the page class to the resource class.

The ResourceID value is the identifier of the resource to read. In the previous example, the Text property for the button is read from the global resource file WebResources.resx (or the appropriate localized version). In that file, ASP.NET uses the value for the resource with the identifier Button1Caption and for the page itself. To set page properties, you can use resource expressions in the @ Page directive.

You can specify an explicit resource expression or an implicit resource expression for a control, but not both.

Localizing Static Text

If a page includes static text, you can use ASP.NET localization by including it in a Localize control, and then using explicit localization to set the static text. The Localize control renders no markup; its only function is to act as a placeholder for localized text. The Localize control can be edited in Design view, not only in the property grid. At run time, ASP.NET treats the Localize control as a Literal control. For example, your page might include the following code:

ID="WelcomeMessage"
Text="Welcome!" meta:resourcekey="LiteralResource1" />

Implicit Localization in Templates

In templated controls, such as the DataList, GridView, and Wizard controls, you localize template style properties by accessing the properties through the parent control's implicit resource expression. You cannot use an implicit resource expression on the template itself.

To localize values for a template property, use the meta attribute and a resource key for the control that the template belongs to. Then use either the Property.Subproperty syntax or the Property-Subproperty syntax in the resource file. For example, the following declarative syntax is valid for a Wizard control:

runat="server"
meta:resourcekey="Wizard1Resource1">
BorderWidth=""/>

runat="server"
Title="Step 1"
meta:resourcekey="WizardStep1Resource1">


The following key/value pairs in the local resource file could be used for the previous example:

Wizard1Resource1.NavigationStyle.BackColor, Red
navborderWidth, 5

Or the following key/value pairs could be used:

Wizard1Resource1.NavigationStyle-BackColor, Red
navborderWidth, 5

Selecting Resource Files for Different Languages

When a page runs, ASP.NET selects the version of the resource file that most closely matches the current UICulture setting for the page. If there is no match, ASP.NET uses resource fallback to obtain a resource. For example, if you are running the Default.aspx page and the current UICulture property is set to es (Spanish), ASP.NET uses the compiled version of the local resource file Default.aspx.es.resx.

ASP.NET can set the UICulture and Culture properties for the page to the language and culture values that are passed by the browser. Alternatively, you can set the UICulture and Culture properties explicitly, either declaratively or in code. You can also set the values declaratively in Web.config files. For detailed information, see How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization.

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1038447/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/13651903/viewspace-1038447/

你可能感兴趣的:(ASP.NET Web Page Resources Overview)