The Ultimate Resource Manager

How would you like your resousrce manager to be able to access bundle content as easy as RM.bundleName.resourceName no functions used, no parameters, even better with a binding enabled? You like it? Me too. Lets have a look at the implementation.

First we need dynamic variables to be bindable. There are more existing and working solutions for this based on Proxy class. We have to handle getProperty() method and implement IEventDispatcherin order to dispatch events. The final class may looks something like BindableDynamics.

Next, lets do some ResourceManager magic to obtain localized values for our dynamic properties. We do not expect the properties will dispatch changes within one locale, but when locale changes (e.g. switch from en_US to fr_FR) all properties should get updated. A single artificial “propertyChange” event will do a good job for us. Lets call this DynamicResourceManager.

Final step can be custom. For example I like implementation with static (no initialization required) constants (constant works with mxml binding without throwing a warning) that looks something like…

package resources
{
    import mx.resources.ResourceManager;
    
    import sk.yoz.resources.DynamicResourceManager;
    
    [ResourceBundle("uiStrings")]
    [ResourceBundle("errStrings")]
    public class RB
    {
        public static const uiStrings:DynamicResourceManager = 
            new DynamicResourceManager("uiStrings");
        
        public static const errStrings:DynamicResourceManager = 
            new DynamicResourceManager("errStrings");
        
        public function RB()
        {
        }
        
        public static function setLocale(value:String):void
        {
            ResourceManager.getInstance().localeChain = [value];
        }
    }
}

With this implementation, the usage is really easy. In order to change locale, a simple setLocale() call will handle all the necessary:

<s:Label text="{RB.errStrings.missingFile}" />
<s:Button label="fr_FR" click="RB.setLocale('fr_FR')" />

Where to go from here:

5 comments so far

  1. JabbyPanda February 14, 2011 16:12

    We are using *exactly* the same approach for Resource Bundles, just never cared enough to share this approach with the rest of the world.

    Although there is a fundemental problem with bindable property if it is defined as a variable, not a String const, e.g “RB.errString(myVariable)”. In this case the binding will not work, sigh.., we just live on with this matter of life.

  2. […] This post was mentioned on Twitter by JabbyPanda, Jozef Chutka. Jozef Chutka said: The Ultimate Resource Manager http://goo.gl/wCkio […]

  3. Jozef Chúťka February 17, 2011 00:32

    JabbyPanda, I can see your point. I think you should be able to create a method within DynamicResourceManager class that would take an argument and return this[value]. If you place Bindable metatag over that function you should be good to go

  4. Roz March 2, 2011 19:22

    Although there is a fundemental problem with bindable property if it is defined as a variable, not a String const, e.g “RB.errString(myVariable)”. In this case the binding will not work, sigh.., we just live on with this matter of life.
    +1

  5. Jozef Chúťka March 15, 2011 11:15

    Roz, what about using something like:
    RB.errStrings.getVariable(myVariable)

    with function defined:
    [Bindable(“change”)]
    public function getVariable(resourceName:String):String

    dispatching change event on ResourceManager change:
    dispatchEvent(new Event(“change”));

Leave a comment

Please be polite and on topic. Your e-mail will never be published.