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.