Setting up Resource Bundles

rollsofcoins.jpg

In last week's Flash Friday post I wrote about using resource bundles to handle embedded fonts. I also left out any instructions about how to set up your project to use resource bundles, instead telling the reader to search the interwebs. I also said I would provide one of my own instructions so here it is.

Project Setup

Your project needs to be able to find your resource bundles so they need to live in your source path. You could place the bundles in the same folder as your source code, but I find that feels a little disorganized. Instead create a folder for your resource bundles and add it to your source path.

In other tutorials they talk about using resource bundles for localization. If that is the case you can add the files as follows. I'll assume you are using the en_US locale, otherwise replace all instances of en_US with the locale you are using.

  1. create folder path in your project of: localeen_US
  2. add the source folder locale{locale}
  3. add the following to your compiler arguments: -locale=en_US

Now you can place your resource bundles in the localeen_US folder.

Property Files

Resource bundles are sometimes referred to as property files because you create them in files with a .properties extenstion. The part before the extension is the bundle name, and you would use that to get the information you wanted. For example, you you had a bundle named fonts you would create a file called fonts.properties. If you were paying attention to my previous article you probably saw me use this same bundle name.

Inside the property file

The property file is fairly simple. It is just a key/value pair separated by an equal sign(=). A simple resource bundle that stores text strings might be called text.properties and look like the following.

# text.properties

hello=Hello World!
goodbye=Good-bye cruel world!

You can look back at my font embedding article to see how I embedded fonts. You can use the same method to embed anything that you can embed using the Embed metatag.

Using the properties in the file

The first thing you need to do is add the Resource Bundle metatag somewhere in your project. You only need to use it once per project for every bundle you are going to use. However, once you do this it gets compiled into the project, even if you don't use the resources. So I would recommend using it in the class(es) that use(s) the bundle.

	[ResourceBundle("fonts")]
	public class MyFontsBundleUsingClass {
		...

Then to use the resources you just need to get an instance of the ResourceManager use one of its functions to get the resource. Here is an example for grabbing a String from my text.properties file I created earlier.

	var resources:IResourceManager = ResourceManager.getInstance();
	var hello:String = resources.getString("text", "hello");
	var goodbye:String = resources.getString("text", "goodbye");
    Of course you can get more than just strings from a property file. Just choose from the following list of functions in the IResourceManager interface.

  • getString
  • getBoolean
  • getInt
  • getUint
  • getNumber
  • getStringArray
  • getClass
  • getObject

And that is my basic primer on using ResourceBundles in actionscript projects. In the grand tradition of this line of posts I am leaving out many important bits of information. Loading resource bundles at runtime is one of them. I'll leave that as a web search exercise for the reader until I write an article on that in the future. If there are other bits of information you would like clarity on let me know in the comments.

Previous
Previous

Its funny 'cause its true

Next
Next

2012 - The Year of the Warrior?