ASP.NET 2.0 Unleashed
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
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
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
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
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
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
|
Категории