blog

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:

Flex: Dynamic, bindable value objects

ActionScript 3 Bindable Dynamic Objects