Question

Those day I am trying to find a way to bind a variable to a localised resource and I really do not have any result.

The situation is the following: I have a class ComponentTypes.as, which contains some public variables of type string. I want those variables to be bound to translated resource:

package Model
{
    import mx.resources.IResourceManager;
    import mx.resources.ResourceManager;

    public class ComponentTypes
    {
        private static var resourceManagerInstance:IResourceManager = ResourceManager.getInstance();

        //Areas and Volumes
        [Bindable]
        public static var HEATED_AREA:String = resourceManagerInstance.getString('app','phpx.componenttypes.heatedareas');
        [Bindable]
        public static var HEATED_VOLUME:String = resourceManagerInstance.getString('app','phpx.componenttypes.heatedvolumes');
    }
}

However those variables are not updated when the locale is changed. This is clear, as they are not currently bound.

So my question is: Is there a way that I can bound those variables to he ResourceManager class, so they are updated when the locale is changed?

Was it helpful?

Solution 2

This is how I made it work:

private function getResource(host:IResourceManager):String
{
    var result:String = '';

    result = host.getString('text', 'test');

    return result;
}

protected function windowedapplication1_initializeHandler(event:FlexEvent):void
{
    BindingUtils.bindProperty(this,'bindedVariable', resourceManager, {name:'getString', getter:getResource});
}

OTHER TIPS

Setting an object using the object = value notation is run only once, when execution hits that point in the code. In the event of a static variable, that occurs the first time the variable is called (I may be wrong and it may be the first time the Class is called or imported, end result here is the same, though).

So you can't bind that way. The way binding works is when a Bindable variable is physically set, it dispatches an event that objects which are binded to it are listening for. What you do there is set the variable once and never touch it again.

You can bind one of two ways:

  1. In MXML using the {{ property }} notation
  2. In AS3 using BindingUtils. BindingUtils attaches an internal event listener (i.e. you don't have to attach it yourself) and updates the provided property. I am unsure it will work with a static object, however.

I did check the source code for mx.resources.IResourceManager#getString() and verified it is a bindable object, so you are good there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top