Adobe Flex 3: Load localization resources at runtime using XML
In a previous post, I wrote an article describing some of the localization weaknesses in Flex 3.
-> Adobe Flex 3.x Localization / Internationalization (i18n) Weaknesses
I had mentioned the concept of loading resources at runtime via a XML formatted document. In this article I will discuss how I accomplished this and provide a working sample and source code.
A fully working example and the source code are provided at the bottom of this article.
The Flex method for creating resource bundles for localization compiling them directly into SWF files works well for many applications and is an efficient method for handling localization. They even go as far as allowing you to load different localization bundle SWFs at runtime to reduce the size of the main application by only loading the resource files that you need. However, this method is not ideal for certain scenarios such as if you are creating a distributable application and want to allow your customers to edit or create resource files. It can also become annoying to have to recompile a SWF just to make a small change to a resource string. So for these types of scenarios you may prefer a resource solution that can load a simple text (XML) file at runtime. Fortunately Flex does provide a means of dynamically creating resource files programmatically.
I have created a XmlResourceLoader class that uses the URLLoader to load the necessary XML resource files at runtime and dynamically generate the resource bundles in the Flex ResourceManager. Since you may want to include a number of different fallback languages the XmlResourceLoader class accepts a chain Array on the load method. This allows you to define a locale specific resource chain such as “en_GB”, “en“. Using this chain, the XmlResourceLoader will load both the “en_GB.xml” resource file and the “en” resource file.
/** * this function will dynamically load the XML resource * files via HTTP if they have not already been * previously loaded into memory. */ public function load(localeChain:Array):void
This function will skip locales in the chain that have already been loaded. So if you allow users to change selected locales at runtime, then subsequent calls to the XmlResourceLoader load function will waste time and resources re-fetching the same content XML.
If you are interested in what specific files were loaded after the load method was invoked, you can access the ArrayCollection of loaded resource files using this method:
/** * returns a list of resource files in use * for the loaded locale chain. */ public function get resourceFiles():ArrayCollection
The XML resource files are simply formatted like this:
- <resources>
- <resourceBundle name="myResources">
- <resource name="myResourceOrigin" value="English Lanugage; Culture Neutral"/>
- <resource name="myLocalizedLabel" value="Hello World!" />
- <resource name="myLocalizedButton" value="Color"/>
- <resource name="myResourceFlagImage" value="assets/images/english-flag.png"/>
- </resourceBundle>
- </resources>
Each XML file may contain one or more resource bundles (<resourceBundle name=”myResources”>). Each resource bundle section may contain one or more resource definitions (<resource name=”myLocalizedButton” value=”Color”/>). Each resource definition contains two attributes: name and value. The name attribute is the resource key name used to resolve the resource string and the value is the localized value string that is returned for the Flex ResourceManager when attempting to resolve a named resource.
Sample Application Links




