Creating Global Resources


A local resource is scoped to a particular page. A global resource, on the other hand, can be used by any page in an application. Any localized content that you need to share among multiple pages in your website should be added to a global resource file.

You create global resource files by adding the files to a special folder named App_GlobalResources. This folder must be located in the root of your application.

For example, the file in Listing 24.20 is a global resource file.

Listing 24.20. App_GlobalResources\Site.resx

Name

Value

Title

My website

Copyright

Copyright © 2006 by the Company


The page in Listing 24.21 uses the entries from the global resource file (see Figure 24.9).

Figure 24.9. Displaying global resource entries.


Listing 24.21. ShowGlobalPage.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>     <asp:Literal                  Text="<%$ Resources:Site,Title %>"         Runat="Server" />     </title> </head> <body>     <form  runat="server">     <div>     <br />Page Content     <br />Page Content     <br />Page Content     <br />Page Content     <hr />     <asp:Literal                  Text="<%$ Resources:Site,Copyright %>"         Runat="Server" />     </div>     </form> </body> </html> 

Just as you can with a local resource file, you can localize a global resource file by adding culture names to the file name. For example, the page in Listing 24.22 is localized to Spanish.

Listing 24.22. App_GlobalResources\Site.es.resx

Name

Value

Title

Mi Website

Copyright

Copyright &copy; 2006 de la compañía


If you modify the UICulture attribute contained in the <%@ Page %> directive in Listing 24.21 to the value es, then the resource file in Listing 24.22 will be used with the page. Alternatively, you can set UICulture to the value auto and change your browser's language settings.

Retrieving Global Resources Programmatically

You can retrieve a global resource entry programmatically from any page by using the GetGlobalResourceObject() method. For example, the page in Listing 24.23 grabs the Title entry from the Site resource file and displays the value of the entry in a Label control.

Listing 24.23. ProgramGlobal.aspx

[View full width]

<%@ Page Language="VB" UICulture="auto" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD /xhtml11.dtd"> <script runat="server">     Sub Page_Load()         lblMessage.Text = CType(GetGlobalResourceObject("Site", "Title"), String)     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Program Global</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

The GetGlobalResourceObject() method requires two parameters: the name of the resource class and the name of an entry. The resource class corresponds to the global resource filename.

Using Strongly Typed Localization Expressions

The ASP.NET Framework automatically converts global resources into compiled classes behind the scenes. This enables you to use strongly typed expressions when working with global resources in your code.

When you create a resource, a new class is added automatically to the Resources namespace. The class exposes all the entries of the resource file as properties.

For example, the page in Listing 24.24 retrieves the Title entry from the Site global resource file (Site.resx and its culture-specific variations).

Listing 24.24. ProgramGlobalTyped.aspx

<%@ Page Language="VB" UICulture="auto" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     sub Page_Load()         lblMessage.Text = Resources.Site.Title     end sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Program Global Typed</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

Notice that you can use the following expression magically to refer to the Title entry in the Site resource file:

lblMessage.Text = Resources.Site.Title





ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

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