Localization, Translation in Flash Builder 4 - part 2

 

This is part 2 in my tutorial on localization in Flash Builder 4, Flex 4 and FlashBuilder 4.5. You need to know how to set up you Flash project for localization to follow along in this part of the tutorial. If you do not already know that then I recommend that you start by reading part 1 on localization that you find here.

Part 1 of this tutorial on localization showed you how to localize strings but that is not the only resource that can be localized in Flash Builder 4.5. Resources can also reference binary assets such as images, audio files, video files and SWF files.

You use the Embed() directive to reference these assets in your properties files.

currentLanguageFlag=Embed("images/sweden.jpg")

It is also possible to extract symbols from an embedded SWF file in your properties files.

currentLanguageFlag=Embed(source="Flags.swf", symbol="sweden")

You can even include custom classes in the localization.

MyClass=ClassReference("se.flashutvecklaren.swedishClass")

@Resource

There is an alternative to using the ResourceManager class to access the values in resource bundles. In MXML, you can use the @Resource directive to access your resource bundles. This method is simple to use but also limited in how it can be used. You can only use the @Resource directive in MXML, you can not change locales at run time and this directive only returns Strings.

<s:Label text="@Resource(key='username', bundle='default')" />
  • key is the label of the string in the resource bundle
  • bundle is the name of the resource bundle

You can use the @Resource directive anywhere a String might supply the location of the image.

<mx:Image source="@Resource(key='currentLanguageFlag', bundle='default')"/>

ResourceManager

All components that extend UIComponent, Formatter, or Validator have a resourceManager property which lets you access this manager. If you create classes that do not extend these classes then you can call the ResourceManager.getInstance() method to get a reference to the ResourceManager singleton.

The ResourceManager dispatches a change event whenever you set the value of the localeChain property. The change event causes binding expressions to methods of the ResourceManager to be updated. Methods available in the ResourceManager for getting localized values are

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

The localeChain property is an Array so that the ResourceManager can support incomplete locales. This means that you do not have to duplicate information if one language have a lot in common with another language. For example US English and British English. In this case it is enough to only specify the differences between the the British and US localization if you already have a complete localization for the US localization.

We set the localChain property to both localizations with descending priority to use incomplete localizations.

resourceManager.localeChain = [en_GB, en_US];

In this case the ResourceManager searches for a resource first in the en_GB bundle. If the resource do not exist in the en_GB bundle then the ResourceManager searches for the resource in the en_US bundle.

This is the end of part 2 on how to localize your flex application in Flash Builder 4.5. Learn more about ActionScript, Flash Builder 4.5 and the Flex framework in our other tutorials on this site. I hope you found this AS3 guide to be helpful.