Flex 3 Localization

ActionScript, Flex, tips 'n tricks Add comments

With Flex 3 comes the ability to do runtime localization. Compared to Flex 2 where you actually had to recompile your application for each locale, you can now have one version of your application that contains different resource bundles.

Today I implemented this in one of the applications we are working on and ran into some issues. Here is how I got it to work in a nutshell. (Note: for a full instruction on implementing localization in Flex 3, I recommend the article Flex 3:Feature Introductions: Runtime Localization by Gordon Smith.)

- create a folder in your project to store your resource bundles (e.g. "locale")
- create a subfolder for each locale you want to implement (e.g. "locale\nl_BE" and "locale\fr_BE")
- in the locale subfolder, create a resources.properties file and save it as UTF-8
- enter the resources in the form of "key=value", like Ant property files
- update the compiler settings: -locale=nl_BE,fr_BE -source-path=locale/{locale}

You now also need to create localized framework resources. To do this, take the following steps:

- open a command line (run as administrator in Vista!)
- go to the "bin" folder in your Flex SDK installation folder
- run the copylocale.exe tool for each locale, this will copy the localized framework files from one locale to another (copylocale en_US nl_BE)
- check FLEX_HOME\frameworks\locale to see if the folder for your new locale has been created
- if you get an error "could not find resource bundle charts", search for a file called charts_rb.swc and copy it to the new locale folder in FLEX_HOME\frameworks\locale

A simple example of asking for a localized string:

Actionscript:
  1. // tell the compiler what resourcebundle to use
  2. <mx:Metadata>
  3.   [ResourceBundle("resources")]
  4. </mx:Metadata>
  5.  
  6. // if you are in a subclass of UIComponent
  7. var myString:String = resourceManager.getString("resources", "myKey");
  8.  
  9. // ...or if you are not in a subclass of UIComponent
  10. var myString:String = ResourceManager.getInstance().getString("resources", "myKey");

You can put these calls in a binding expression and switch your locale at runtime by setting the localeChain property on the resourceManager:

Actionscript:
  1. resourceManager.localeChain = ["nl_BE"];

That's about it. Have fun!


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

7 Responses to “Flex 3 Localization”

  1. German Says:

    Christophe,
    Nice work, but i’m still trying to solve something out. How do i detect the language of the user and set it at runtime. I’ve done something with javascript but the navigator.userLanguage (and others) seem to be undefined for Firefox and Explorer. I’m able to change the language with a combo that displays the available languages but thats all. Thanks in advance.

  2. Christophe Says:

    Hi German,

    maybe this will be of any help: http://www.actionscript.org/forums/showthread.php3?t=162137

  3. Dewey Says:

    This is very similar to the approach I’m taking in an application. I have a few additional requirements re: i18n that are giving me pause. Perhaps they are more related to project structure. I have 3 places where my localized resources must be used. 1) The main application SWF, 2) modules which are used by the application (including possibly RSL), and 3) component SWCs that we are using which will cross application boundaries. I’ve broken the bundles into two, System and Application to allow the SWCs to be able to access common system level translations like OK, Cancel and the like. The issue I’ve having is how to get the projects to recognize the resources without manually copying them into each project. When I add the MXML Meta tag for the resource in the SWC library project components ie. [ResourceBundle(”System”)], the compiler can’t resolve the reference. How should I be thinking about this??? Thanks fro any light you can add to this problem.

  4. Andras Says:

    Hi,

    I want to create aflex app. using ResourceBundles but without using the copylocale command, so i want to use just the property files loaded.

    Flex 3 documentation says: The localeChain property is an Array so that the ResourceManager can support incomplete locales. … If the resource is not found there, then the ResourceManager searches for the resource in the en_US bundle…

    It seems to me that isn’t working, if i compile my flex app with -locale en_US,en_HU , it’s always searching for locale\en_HU framework.swc files.

    Do you think i misunderstanded something or do something wrong?

    Thanks,
    Andrew

  5. Andras Says:

    problem solved.
    flex builder needs an empty locale directory (like: C:\Program Files\Adobe\Flex\frameworks\locale\hu_HU\ ), but not any swc files.

  6. Johan Says:

    Ho do I get a class to update when the bundles is changed. I can get the class to use the resouces like this

    [ResourceBundle(”forms”)]
    public class MultiFileUpload extends UIComponent { … }

    and set values in the class with resourceManager.getString(’form’,'MY_KEY’)
    but how/can do I get them to update AFTER the class has been instantiated?

    For example I have a FileUpload class and want UI elements (column titles of a file list data grid) to changes if the language is changed.

  7. Shawn Says:

    I’ve got the multi-language localization with RSL’s to work just fine.
    But as with most applications, everything doesn’t reside in the
    application page, so what I would like to do is somehow put the resource
    bundle into the model (in Cairngorm) so it can be accessed in all
    components that use the modellocator, instead of having to use the
    application scope because thats where the ResourceBundle Metadata is…
    I could use Application.application.resourceManager… for everything
    but I would rather have it in the model and be able to change the
    localeChain of the resourceManager in the model, some way ????

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login