StandardPropertiesResourceManager


The StandardPropertiesResourceManager exists to solve a problem identified in previous chapters. Several properties (e.g., Font, ImeMode, RightToLeft) sometimes need to be set on an application-wide basis. That is, if you are localizing your application for Arabic, Hebrew, or Persian (Farsi), you should set the Right ToLeft property of each control to Yes (or Inherit for controls and Yes for forms). Your translator/localizer can set these values for every control or form individually, but not only is this labour-intensive, it is also error prone; it is easy to miss a few controls, especially during the maintenance phase, when new controls are added. For this reason, if your application is such that an application-wide setting is appropriate (in other words, there are no places in your application where some controls should look or behave differently from the majority of the application), the StandardPropertiesResourceManager offers a suitable solution.

Like the TRanslationResourceManager in the previous section, the Standard PropertiesResourceManager uses a "workhorse" resource manager to perform the majority of the work of a resource manager. The part that the Standard PropertiesResourceManager adds to the process is that for specific properties it steps in and overrides the workhorse resource manager's value and sets its own value.

Here's how it works. Like the TRanslationResourceManager, the Standard PropertiesResourceManager inherits from the SurrogateResourceManager to handle the job of passing requests on to the "workhorse" resource manager. The essential structure looks like this:

 public class StandardPropertiesResourceManager:     SurrogateResourceManager {     private static Font font;     private static ImeMode imeMode = (ImeMode) -1;     private static RightToLeft rightToLeft = (RightToLeft) -1;     public StandardPropertiesResourceManager(         string baseName, Assembly assembly):         base(baseName, assembly)     {     }     public StandardPropertiesResourceManager(string baseName):         base(baseName)     {     }     public StandardPropertiesResourceManager(Type resourceType):         base(resourceType)     {     }     public static Font Font     {         get {return font;}         set {font = value;}     }     public static ImeMode ImeMode     {         get {return imeMode;}         set {imeMode = value;}     }     public static RightToLeft RightToLeft     {         get {return rightToLeft;}         set {rightToLeft = value;}     } } 


There are a few static properties (Font, ImeMode, RightToLeft) that map onto equivalent static fields to hold the application-wide settings. You can see from this code that it would be a simple matter to add new application-wide settings to this list. The remainder of the class simply overrides the GetObject method to substitute requests for application-wide properties:

 protected virtual bool SubstituteValue(string name,     CultureInfo culture, object value, object substituteValue,     string propertyName) {     if (value == null)         return name.EndsWith("." + propertyName) &&             substituteValue != null;     else         return substituteValue != null &&             value.GetType().IsSubclassOf(substituteValue.GetType()); } protected virtual bool SubstituteValue(     string name, CultureInfo culture, int value,     int substituteValue, string propertyName) {     return name.EndsWith("." + propertyName) &&         substituteValue != -1; } public override object GetObject(string name, CultureInfo culture) {     object obj = base.GetObject(name, culture);     if (SubstituteValue(name, culture, obj, font, "Font"))         return font;     if (SubstituteValue(name, culture,         (int) obj, (int) imeMode, "ImeMode"))         return imeMode;     if (SubstituteValue(name, culture,         (int) obj, (int) rightToLeft, "RightToLeft"))         return rightToLeft;     return obj; } 


The GetObject method gets the requested value from the workhorse resource manager. It then checks to see whether it is one of the application-wide values that it should substitute; if it is, it substitutes its own value.




.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