How to localize a Flex application easily
// August 27th, 2009 // Flex framework
The LocalizableApplication is a tiny class I’ve written to facilitate the task of localizing a Flex application. A demo is shown below
Exploiting the LocalizableApplication class, writing a localizable application is as easy as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="utf-8"?> <wg:LocalizableApplication xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:wg="com.webgriffe.core.*" layout="vertical"> <mx:Metadata> [ResourceBundle("resource")] </mx:Metadata> <mx:Script> <![CDATA[ [Bindable] private var _locales:Array = ["en_US", "it_IT"]; private function changeLocale():void { var newLocale:String = myCombo.selectedItem as String; setLocale(newLocale); } ]]> </mx:Script> <mx:Label text="{__s('MY_TITLE')}" /> <mx:ComboBox id="myCombo" dataProvider="{_locales}" change="changeLocale()" /> <mx:Label text="{__s('MY_TEXT')}" /> </wg:LocalizableApplication> |
The LocalizableApplication provides some helper functions which let you use something like __s('MY_TITLE') instead of resourceManager.getString("resource", "MY_TITLE"). Faster and easier, isn’t it?
Click here to download full source code.
Enjoy!




Hi Alessandro,
I’m always interested in simplifying localization. Anyway, doesn’t your approach only help on the application level? If you want to create a component using the same technique, you would have to add (copy) those methods to the components super class, which is not so nice …
What do you think?
Hi Norbert,
consider that a Flex Application acts as a Singleton, so those methods are always available within a Flex app, you don’t necessarily have to copy them.
Obviously this way your components won’t be loosely coupled, this is a nasty compromise expecially if you have to distribute your components.
Thank you for your comment, it gives me motivation to think to a different approach.
Best wishes, Alessandro
I did something similar, except that I just monkey patched ResourceManager to accept either a string or it’s full parameter set, If just a string was passed to the getString() method, it would pull the localized value from a default “strings” bundle for the app. If more than one argument was passed to the method, it would work normally.