Dealing with multilingual content and mx.lang.Locale
October 13, 2005
A few time ago we had to make a small flash site with multilingual content. The site itself was really simple, nothing complicated, with the only “dynamic” feature to support more than one language in the textfields. So we thought this would be the golden opportunity to try out the strings panel which is built into flash since mx2004 and is probably the simplest way to add more languages to your file. No loading and reading xml files or databases etc.. Just enter your strings in different languages in the strings panel and your’re done. Unfortunately it turned out that these strings things are quite useless. Here’s why: (Note that I’m speaking of FlashMX 2004 here and not Flash 8).
- It’s quite inconvenient to enter longer strings in the strings panel as there’s no word wrapping in the table fields (fixed in flash 8 I believe).
- For an unexplainable reason flash didn’t create the folders on my machine properly so errors occured when the fla file was reopened (at my colleagues machine the exact same fla worked perfectly!).
- You have to use the folder structure that flash creates for you.
- All textfields must be in the first frame of your movie.
- If activated flash compiles some language detection code automatically into the swf which basically loads the proper xml file (via mx.lang.Locale) so you haven’t any control over preloading.
If you want a bit more control over the whole xml selection and loading process, you could use mx.lang.Locale manually…well, you could…:
mx.lang.Locale is full of static methods which is bad if you want to subclass. Why would you want to subclass? For example, if you want to add a method for setting the xml file to load manually (user clicks a language selection button and you want to load the proper xml file). So, I copied the class and modified it to better suit our needs. Here are the changes:
- I called the class LocaleSwitcher, removed the statics and made it a singleton.
- I added a method called “setXMLLang” that lets you choose which language to load.
- I used Delegate and EventDispatcher to fire an event “onLangLoaded” when xml loading is finished (instead of the callback version in mx.lang.Locale).
Then you can set the fla name and the paths to the xml files manually, have a language selection button, combobox or whatever which sets the xml file, load the xml file and start your movie when the xml file is loaded. Then you call loadString() (in any frame!) to set the content of your textfields. This is maybe a better and more flexible solution than the built-in auto-dectect-language-and-auto-set-all-textfields-in-first-frame-solution but I don’t like the whole mx.lang.Locale and strings panel thing. The xml files it generates use id numbers and are very unintuitive to edit.
The best solution we came up for small websites is this: Create your xml files, one for each language, with descriptive tag names and a label attribute and, write your code for selecting the right xml file and preloading. In every textfield you need a multilingual string access it – and now comes the important part – via the very cool XMLShortcuts component by Arul Kumaran. Just put the component into your library and select the xml nodes by their tag name (no more firstChild.firstChild.childNodes bla…), e.g.: language.about.headline._label (even mixed node access is possible: language.firstChild.headline._label). The latest blog entries in Arul’s blog explain in detail how it works, what the advantages are regarding other components/approaches for accessing xml nodes easily etc.. You don’t want to miss XMLShortcuts when working with xml files…But with ActionScript 3 and E4X everything is also going great, right? ;-)
Filed under: Flex/AS3