|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today!
SYS-CON.TV SYS-CON.TV WEBCASTS |
MXDJ TOP LINKS YOU MUST CLICK ON ! Flex
Internationalizing Adobe Flex Apps
One of the many features available in Flex 2.0 is a ResourceBundle class
By: Jeff Tapper
May. 13, 2007 10:00 AM
Digg This!
One of the many features available in Flex 2.0 is a ResourceBundle class, which allows for a standardized approach for internationalizing applications. Many recent projects of mine have required that applications be built that can easily be ported to other languages. Traditionally, I've used a series of XML files for this, one for each of the various languages that need to be supported. This strategy is still viable, and I still use it on some of my projects. However, I've recently discovered a different approach, which is available natively in Flex. Flex provides a ResourceBundle class, which allows you to set up your text in properties files (the identical structure that you would use for internationalizing Java applications). These properties files are arranged in a folder structure, relating to the language and country, so the properties file for US English would be in a folder called en_US, the file for the UK would be en_UK, while the French would be in fr_FR. The structure of the files is very simple; they look like this:
Or, in the French version: hello = Bonjour Monde To use these in an application, you have two options: you can use the @Resource command each time you need a value, or you can declare a variable for the ResourceBundle and The remaining trick is to tell the compiler which language to use, and where to find the files. You can do this from the command line like this: mxmlc -locale en_UK -sp ../locales/{locale} -o HelloWorld_en_UK.swf I18N_HelloWorld.mxml Or, you can specifiy compiler arguments in Flex Builder: -locale en_US -sp ../locales/{locale}
As we have already discussed, the native use of the ResourceBundle class requires separate compiled SWFs for each language. This is not always desirable, and there are times when you may want to allow for switching of languages at runtime. One strategy I've used successfully for this is to trick the Flex compiler and have several different properties in the same locale folder, and to create separate instances of the ResourceBundle class for each of them. This way, it's a fairly simple process to determine what the current locale is, and to pull the labels from that ResourceBundle. To start, I took three properties files and placed them in a single directory. Listing 2 is a simple example of how to get it working. I named each file based on the language it was there to support (helloWorld_fr.properties, helloWorld_uk.properties, helloWorld_us.properties). Notice that there is ResourceBundle instance for each of the three files. I've also added some simple functions to get the data from these files (geti18nText, geti18nDate). Keep in mind, this is a simplistic example. In real world apps, I tend to have a singleton responsible for embedding and retrieving the data from the files. But, even in this simple case, you can see the power of it - switching the selected language in the combo box instantly translates the labels and dates to the appropriate format. Remember to add a compiler argument to specify the proper directory for the locale files. In my case, all three files were in a locales/multi directory, so I added the argument: -sp ../locales/multi Look for the follow-up parts to this article in the next issue of Web Developer's & Designer's Journal. LATEST FLEX STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||