How to use Properties file/ResourceBundle in flex


In Flex its possible to add a properties file – say ApplicationSettings.properties  – which will contain common inputs to the app at one place. This makes it easy to edit/supply values from one point and populate the changes to all other places in the application. These are called ResourceBundles in Flex.

When we build/launch/deploy such an application( one containing .properties files), these properties go and integrate into the generated .Swf or the .swc.

How to use these??

Generic steps are detailed below:-

1) First setup your properties file in your file system.
It is generally good practice to keep such a .properties file in a folder, so in case  you want to add more variety of properties, you know where to store them and the compiler picks up all of them at once. So create a new folder/directory called “properties” right next to the src folder of your flex application (easy to find it that way). Create a new file with the name ApplicationSettings.properties

2)Fill in the properties
Property names and their values typically sit as pairs. Here is a sample properties file –

# locale/en_US/RegistrationForm.properties
registration_title=Registration
serverURL=http://localhost/

3) Using the properties –

First Step is to add the newly created properties folder to the project’s build path. Do  this by right clicking on the project name -> Properties ->Project Build Path ->Add Source Folders  route.

In an ActionScript class, these ResourceBundles are loaded this way-

import mx.resources.IResourceManager;
import mx.resources.ResourceManager;
[ResourceBundle(“ApplicationSettings”)]
public class MyActionScriptClass {


var trialURLString :String = ResourceManager.getInstance().getString(“ApplicationSettings”,”serverURL”);

}

In an MXML file these are loaded in the <mx:script /> tag , for example on top of the function where you want to access them –
<mx:Script>
<![CDATA[
import mx.resources.IResourceManager;
import mx.resources.ResourceManager;
import mx.controls.Alert;
[ResourceBundle(“ApplicationSettings”)]
public function init():void{
var trialURLString :String = ResourceManager.getInstance().getString(“ApplicationSettings”,”serverURL”);
testPropertiesTextArea.text = trialURLString;
Alert.show(trialURLString);
}
]]>
</mx:Script>

The code above to access the particular property is pretty straight forward. We load a common instance of the ResourceManager class and use the getString function.
getString function has two parameters – 1st : Name of the properties file to look into  and 2nd: Name of the property whose value has to be returned.

Most values are returned as String as the function name suggests.

See this post for a more detailed explanation – http://flex.sys-con.com/node/276863

Advertisements

About vatsalad

Hi, I'm Vatsala
This entry was posted in Adobe Flex, flex, How To... and tagged , , , . Bookmark the permalink.

3 Responses to How to use Properties file/ResourceBundle in flex

  1. gmale says:

    Very helpful post, thanks.

    This can be done in MXML with syntax similar to the following:

    mx:Label text=”@Resource(key=’serverURL’, bundle=’ApplicationSettings’)”

    Also, here is a good resource on doing it staticly (so you don’t have to declare resources in every place that you use them):
    http://hillelcoren.com/2009/07/06/resource-bundles-in-flex-new-thoughts-on-the-topic/

    I hope that helps someone. I’m still researching the best way to go about using Resource Bundles…

  2. agin says:

    how to change the complier settings?Iam using flex builder 3.

    I ma getting null value in trialURLString.

  3. agin says:

    i created a folder named locale/en_US under src and kept my property file there.then in mxml i added above code.then i changed compiler setting by right clicking the project folder Flex compiler–>Additional compiler arguments..there i entered -locale en_US -source-path+=../locale/{locale}.Is this right way to implement this.??I ma getting null value in trialURLString.Can anyone tell why its not wroking.??:(thanks in advance

Comments are closed.