Explicit Expressions


Another of the new features in ASP.NET 2 is explicit expressions. We have already seen implicit expressions and how property values are implicitly linked to a resource value. An explicit expression serves a similar purpose, with the difference being that a property value is explicitly linked to a resource value. Whereas implicit expressions bind resources to properties that are marked with Localizable(true), explicit expressions bind resources to any property whatsoever.

Colors are one of the subjects that often change from culture to culture. Developers often use colors to convey meaning, but different colors convey different meanings to different cultures. Consequently, color properties are ripe for localization. However, you might have noticed that no color properties of Web controls are marked as Localizable(true). This is where explicit expressions come in. Let's say we want to have a definition for a color that we will call WarningColor. The purpose of this color is to convey a warning to the user. We want to give buttons with a potentially dangerous activity this color to convey some kind of seriousness to the user. Start by adding a new string resource to the page's fallback resource file. The new string resource should have a key of WarningColor and a value of Red. Add the same resource key with a different value to any culture-neutral or culture-specific resource that should have a different color. Now select a button (I have called mine Sell Stock, which I feel is not an action that should be taken lightly), select its "(Expressions)" property in the Properties Window, and click on the ellipses to show the Explicit Expressions Dialog. Select a property (e.g., BackColor) that you want to apply the explicit expression to. In the Expression Type combo box, you can select either AppSettings, Connection-Strings, or Resources. The WarningColor resource is a local resource, so select Resources. In Expression Properties, select ResourceKey and drop down the list of resources. Select WarningColor (see Figure 5.8) and click OK.

Figure 5.8. Assigning an Explicit Expression to a Property


The button's BackColor property is now explicitly bound to the WarningColor resource. This is confirmed in the Properties Window (see Figure 5.9) by the small blue icon next to the property and the ToolTip associated with this icon.

Figure 5.9. Explicit Expressions in the Properties Window


It is further confirmed by the change to the button's definition:

 <asp:Button  runat="server" meta:resourcekey="SellStockButtonResource1" Text="Sell Stock" BackColor="<%$ Resources:WarningColor %>" /> 


The BackColor property is assigned an explicit resource (i.e., "<%$ Resources:WarningColor %>"). You can, of course, bypass the Explicit Expressions Dialog completely and type this same text into the button's definition to achieve the same result. Similarly, you can delete this expression to unbind the property, or, alternatively, you can use the Properties Window to assign a new value to BackColor (which breaks the link to the explicit expression).

Notice that the WarningColor resource is a string, but the BackColor property is a System.Drawing.Color. The .NET Framework performs an implicit conversion from string to Color when the resource is applied. Also note that this is one of those occasions when you should be sure to write a comment next to the resource explaining what its purpose is. Without the comment, the translator may well translate the WarningColor from Red to, say, Rouge (i.e., the French for "red"), which would cause the application to fail.

This feature provides many localization opportunities. Clearly, the essential fact is that it enables us to localize any property, but these properties are not limited simply to appearance. Consider that you might have functionality that is appropriate for one culture but not for others. It may be that this culture requires an extra button say, for calculating some local taxthat other cultures do not want. You could, of course, write code that adds this button to the control tree at runtime, but a better solution would be to use explicit expressions. In this scenario, you would add the button to the fallback page as normal. You would add a Boolean resource called, say, SpecialTaxFunctionality to the fallback resource and give it a value of false. You would bind the button's Enabled or Visible property to the SpecialTax-Functionality resource to ensure that it was either disabled or invisible by default. In the resource file for the culture that needs the button, you would add the SpecialTaxFunctionality resource with a value of true. Now the cultures for which the button is appropriate can use the button, and the cultures for which it is not appropriate either cannot see it or cannot use it.

It is worth pointing out the architectural implication of explicit expressions. Explicit expressions enable us to strike the right balance between making too many properties localizable and making too few properties localizable. In Windows Forms 1.1, all properties that could conceivably be different in a different culture are localized. This results in numerous properties being localized "just in case." This is wasteful because most of these properties are often not localized. The alternative is to provide a subset of properties that is too restrictive and prevents critical properties from being set in certain cultures. The combination of implicit expressions and explicit expressions enables developers to localize only those properties that need to be localized on a case-by-case basis. Thus, implicit expressions provide support for properties that are likely to be localized in the majority of cases (e.g., Text), and explicit expressions provide support for all other cases.




.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