Programmatic Resource Access


Implicit expressions and explicit expressions cover a large part of localizing an application, but it will always be necessary to access resources programmatically. In the ASP.NET 1.1 half of this chapter, we saw that we can create a ResourceManager and call its GetString or GetObject method to access these resources. In ASP.NET 2, you could adopt this same approach, but this would bypass ASP.NET 2's resource provider mechanism. ASP.NET 2 enables you to replace its default resource provider with a different resource provider (see Chapter 12, for example). If you were to use a ResourceManager directly, your application would not be capable of taking advantage of this feature effectively. Instead of using ResourceManager.GetObject, we can use Page.GetLocalResourceObject. Let's say we have a page with a Label called LoggedInTimeLabel, and we want to access a local string resource called LoggedInTime, which is this:

 You first logged in at {0} 


In the page's Load event, we can initialize the Label like this:

 LoggedInTimeLabel.Text = String.Format((string)     GetLocalResourceObject("LoggedInTime"),     Session["LoggedInTime"].ToString()); 


GetLocalResourceObject gets the LoggedInTime resource object from this page's resources. Note that there is no requirement to specify where the resources are. This is in preference to the ResourceManager constructor, which demands this information and is consequently slightly more fragile. In the same vein, you can access global resources using the page's GetGlobalResourceObject:

 string warningColor = GetGlobalResourceObject(     "GlobalColors", "WarningColor").ToString(); ColorConverter colorConverter = new ColorConverter(); SellStockButton.BackColor =     (Color) colorConverter.ConvertFromString(warningColor); 


This example gets the WarningColor from the GlobalColors global resource and converts it from a string to a System.Drawing.Color before assigning it to the SellStockButton. The drawback to using GetGlobalResourceObject in this way is that it identifies the resource entry using a string identifier. Such access is fragile and subject to error. A better alternative to using GetGlobalResourceObject is to use the automatically generated, strongly-typed resource class that corresponds to the global resx file. This automatically generated class is created in a Resources namespace and is given the same name as the resx file (without the extension). So the first line of the previous example could be rewritten as follows:

 string warningColor = Resources.GlobalColors.WarningColor; 


Not only is this syntax neater, but it is checked at compile time instead of runtime.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

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