| Flex 2 Developer's Guide > Customizing the User Interface > Localizing Flex Applications > Creating a localized application > Using the ResourceBundle API | |||
The first step in localizing Flex code is to use the mx.resources.ResourceBundle API to replace all of the hard-coded content that you want to localize. You can use the ResourceBundle API in ActionScript or MXML code.
All content that you localize must be removed from the main application. Calls to the content are accessed with the [ResourceBundle] metadata tag in ActionScript or the @Resource directive in MXML.
You must place the [ResourceBundle] metadata tag directly above a variable of type mx.resources.ResourceBundle, as the ActionScript code in the following example shows:
...
[ResourceBundle("bundlename")]
private static var rb:ResourceBundle;
...
You cannot create a ResourceBundle instance in ActionScript by using the new keyword; you must use the [ResourceBundle] metadata tag.
The compiler automatically instantiates the variable. In your code, you can use the ResourceBundle API to access keys in the resource bundle, as the following example shows:
...
public function myMethod(){
...
rb.getString(myKeyName);
rb.getObject(myClassName);
...
}
...
You can also call the ResourceBundle.getString() or ResourceBundle.getObject() methods in an MXML binding expression, as the following example shows:
...
<mx:Button id="myButton1" label="{rb.getString(myButton1_label)}" click="ti1.text='myButton1.label='+myButton1.label;"/>
...
You can use the @Resource MXML directive instead of the [ResourceBundle] metadata tag. You specify a resource bundle name and key in the @Resource directive to get a value from the resource bundle. If you specify only the key, the current class name is used as the bundle name. The bundle name is the name of the properties file or ResourceBundle subclass that contains the key. These are the two forms of the @Resource directive:
@Resource("key")@Resource(bundle="bundle", key="key")You use this directive to access a key in a resource bundle, as the following example shows:
...
<mx:Button id="myButton1" label="@Resource(bundle='MyButtonResources', key='myButton1_label')" click="ti1.text='myButton1.label='+myButton1.label;"/>
...
You can specify which localized string value to use depending on the locale. For example, the default English properties file could contain the following key:
MYTEXT = {0} and {1}
Another English properties file could contain the following key:
MYTEXT = {1} and {0}
The following example shows string substitution in ActionScript:
...
[ResourceBundle("MyBundle")]
private static var rb:ResourceBundle;
StringUtil.substitute(rb.MYTEXT, "My name is Anant", "I am on the Flex team");
...
Flex 2
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flex/2/docs/00000901.html
Comments
No screen name said on Dec 7, 2006 at 6:13 PM :