Search code examples
actionscript-3apache-flexflex4flex4.5resourcebundle

Flex ResourceBundle get keys -> `content` doesn't provide key-value pairs any more


I need to access key-value pairs from a properties file, well a substring of the key and the value to be exact, but that shouldn't change anything.

When you check the Adobe docs and help pages such as this one http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf69084-7f2c.html (last paragraph) you can see that Adobe mentions that the ResourceBundle's content property contains key-value pairs from the given bundle.

The ASDoc of the ResourceBundle also says: "The subclass overrides this method to return an Object that contains key-value pairs for the bundle's resources." However, the ASDoc also says that this property applies for Flex 3.

Anyway, the content property (type Object) doesn't seem to store key-value pairs, as I only get the value, not the key though.

I'm kinda lost here, if anyone has some information regarding this, please shoot!


Solution

  • The ASDoc for ResourceBundle says that this class and it's API is available since Flex 3. So the documentation is still valid for Flex 4 and beyond.

    Make sure you are using a "for in" loop rather than a "for each" loop. The "for in" loop iterates over the property names of an Object, while the "for each" would iterate over the values.

    This code creates a ResourceBundle and iterates over the properties:

    var rb:ResourceBundle = new ResourceBundle("en_US", "bundleName");
    rb.content["foo"] = "Bar";
    rb.content["hello"] = "Howdy!";
    
    for (var key:String in rb.content)
    {
        trace("the key is: ", key);
        trace("the value is: ", rb.content[key]);
    }
    

    While iterating, each key is stored in the var key and it's value can be retrieved from the content Object using content[key].

    In a real application, w/a real resource bundle file, you retrieve the resource bundle from the resource manager (from the docs you linked to):

    var rb:ResourceBundle =  ResourceBundle(resourceManager.getResourceBundle("en_US", "RegistrationForm"))
    

    And then you should be able to iterate over it's content as above.