| Flex 2 Developer's Guide > Customizing the User Interface > Localizing Flex Applications > Creating a localized application > Using the ResourceBundle API > Using localized objects and embedded assets | |||
Revised: 10/10/06. Replaced all text on this page with new information
You can use any ActionScript class as a localized resource. For example, you can have two different classes, one for each locale, that perform localized tasks in your Flex application. Rather than creating a properties file that acts as the repository of Strings for the resource bundle, you create a new class that references all the classes in the resource bundle. You extract a reference to the localized class from this new class rather, similar to the way you extract a String from a properties file.
To use localized classes, you must perform the following steps:
These steps are described in the sections that follow.
You can also use localized embedded assets such as images and sound files. To do this, you perform all the steps described in this section as well as the additional step described in “Using embedded assets”.
To use localized objects or embedded assets, such as images, in an application, you get a reference to the resource bundle using the [ResourceBundle] metadata tag. You then call the ResourceBundle.getObject() method to get a particular class from the resource bundle, as the following example shows:
<?xml version=”1.0”?> <!-- l10n/SimpleClassApp.mxml --> <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” creationComplete=”initApp()”>
<mx:Script><![CDATA[
import mx.resources.ResourceBundle;
[Bindable]
[ResourceBundle(“MyObjectClassesBundle”)]
private var rb:ResourceBundle;
private function initApp():void {
var sc:Object = rb.getObject(“SimpleClass”);
b1.label = sc.returnString();
}
]]></mx:Script>
<mx:Button id=”b1”/>
</mx:Application>
In this example, MyObjectClassesBundle is the name of a ResourceBundle subclass that you create to embed your localized ActionScript classes, as described in “Creating a bundle class”.
To use embedded assets in a resource bundle, you must create a bundle class. This class must extend ResourceBundle. You typically include a constructor that only calls super(). This class also returns an object in the getContent() method. This object references all the classes that are required in the ResourceBundle.
You create one version of this class for each supported locale and store it in the locale/{locale} directories.
The following example shows the MyObjectClassesBundle class, which imports the localized ActionScript classes and adds a reference to them in the returned content object:
package {
import mx.resources.ResourceBundle;
import MyBird;
import SimpleClass;
public class MyObjectClassesBundle extends ResourceBundle {
public function MyObjectClassesBundle() {
super();
}
override protected function getContent():Object {
var contentObj:Object = new Object();
// Add a reference to the localized ActionScript class. contentObj[“SimpleClass”] = SimpleClass;
return contentObj; } } }
In this case, the localized ActionScript class is a simple class that has a method that returns a String, as the following example shows:
package {
public class SimpleClass {
// Constructor
public function SimpleClass() {
}
public function returnString():String {
return “hello from SimpleClass”;
}
}
}
You can store the localized ActionScript class files in your locale/{locale} directories, or any directory that is accessible by the bundle class.
In addition, you can use embedded classes in resource bundles. These embedded assets can include images, sound files, or any type of file that can be embedded in a Flex application.
When you want to use embedded assets in resource bundles, you must create an ActionScript wrapper class for each embedded asset or object. This class extends a class that matches the kind of asset. For an image, the wrapper class typically extends BitmapAsset.
In your wrapper class, you only need to include an empty constructor and an [Embed] metadata tag. In this class, you must use the class-based version of Embed and not the @Embed directive.
You must create a class for each asset that you want to embed. For example, to localize a GIF image, you create the following MyBird class:
package {
import mx.core.BitmapAsset;
[Embed(source=’bird.gif’)]
public class MyBird extends BitmapAsset {
public function MyBird() {
// Empty constructor.
}
}
}
You add a wrapper class for each localized embedded asset to your locale/{locale} directories.
Your bundle class is the same as the one you created to use localized ActionScript classes.
package {
import mx.resources.ResourceBundle;
import MyBird;
import SimpleClass;
public class MyEmbeddedClassesBundle extends ResourceBundle {
public function MyEmbeddedClassesBundle() {
super();
}
override protected function getContent():Object {
var contentObj:Object = new Object();
// Add a reference to the embedded graphic asset. contentObj[“MyBird”] = MyBird;
// Add a reference to the localized ActionScript class. contentObj[“SimpleClass”] = SimpleClass;
return contentObj; } } }
In your Flex application, you instantiate the ResourceBundle the same way you did when using localized ActionScript classes. You also use the getObject() method to get the embedded asset. The following example gets an image class named MyBird:
<?xml version=”1.0”?> <!-- l10n/EmbeddedAssetApp.mxml --> <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” creationComplete=”initApp()”> <mx:Script><![CDATA[ import mx.resources.ResourceBundle;
[Bindable] [ResourceBundle(“MyEmbeddedClassesBundle”)] private var rb2:ResourceBundle;
[Bindable] [ResourceBundle(“MyObjectClassesBundle”)] private var rb:ResourceBundle;
[Bindable] private var bird:Object = rb2.getObject(“MyBird”);
private function initApp():void {
var sc:Object = rb.getObject(“SimpleClass”);
b1.label = sc.returnString();
}
]]>
</mx:Script>
<mx:Panel id=”panel1” title=”Image from Resource Bundle”>
<mx:Image id=”image1” source=”{bird}”/>
</mx:Panel>
<mx:Button id=”b1”/>
</mx:Application>
To make the conversion to run-time loading easier, you should not make direct references to localized classes. Because of this, you should add any visual children through ActionScript, as the following example shows:
<?xml version=”1.0”?> <!-- l10n/EmbeddedAssetAppAddChild.mxml --> <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” creationComplete=”initApp()”> <mx:Script><![CDATA[ import mx.resources.ResourceBundle; import mx.controls.Image;
[Bindable] [ResourceBundle(“MyEmbeddedClassesBundle”)] private var rb2:ResourceBundle;
[Bindable] [ResourceBundle(“MyObjectClassesBundle”)] private var rb:ResourceBundle;
[Bindable] private var bird:Object = rb2.getObject(“MyBird”);
private function initApp():void {
var sc:Object = rb.getObject(“SimpleClass”);
b1.label = sc.returnString();
var i:Image = new Image(); i.source = bird; panel1.addChild(i); } ]]></mx:Script>
<mx:Panel id=”panel1” title=”Image from Resource Bundle”> </mx:Panel>
<mx:Button id=”b1”/> </mx:Application>
Flex 2
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flex/2/docs/00000902.html