View comments | RSS feed
Packagemx.resources
Classpublic class ResourceBundle
InheritanceResourceBundle Inheritance Object

This class can be used to internationalize the framework and applications. To localize an application, you can create one properties file per class or per package or simply a single properties file. This properties file is exactly like a Java-based properties file which has name-value pairs. The properties file name can be the same as the class name, for example, MyAlert.properties corresponding to MyAlert.as, or it can have a different name. Exisiting framework properties files can be found in the frameworks/locale/en_US/ directory. The frameworks/locale/en_US/SharedResources.properties file contains all shared keys and error strings across the framework.

If you want to localize in a particular language, for example French, you would make a copy of frameworks/locale/en_US/ directory and call it fr_FR/ and make the necessary changes to the framework properties files. You can compile your application kept in MyApp using "..\bin\mxmlc.exe" -locale en_US -library-path=..\frameworks\libs -source-path="..\frameworks\locale\{locale}, ..\myLocale\{locale}" main.mxml

Note that the source-path mentions the source for both frmaework properties files as well as myApp's properties files. In this case, you would place MyAlert.properties in the myLocale/en_US and myLocale/fr_FR directories. The locale directory should be part of the compc ActionScript classpath but should not be a part of the ActionScript classpath that is used for mxmlc when building the main SWF file; for example, placing myLocale in the myApp directory would throw a compiler warning. The locale is settable through the xml config file, mxml compiler argument or through Flex builder. However, locale is not settable through the Locale class at runtime.

The framework currently supports en_US and ja_JP only. However, you can create properties files for any language and keep them under a locale folder, such as fr_FR for French.

While getting localizable text, you must specify the filename in the ResourceBundle metadata; for example:

 [ResourceBundle('MyAlert')] 
 private static var rb:ResourceBundle;

and then access it in ActionScript using rb.getString('foo'); or in MXML using @Resource(bundle='MyAlert', key='foo').

See also

mx.resources.Locale


Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined by
  
Constructor.
ResourceBundle
  
getBoolean(key:String, defaultValue:Boolean = true):Boolean
Gets a Boolean from a ResourceBundle.
ResourceBundle
  
Gets a Number from a ResourceBundle.
ResourceBundle
  
Gets an object from a ResourceBundle.
ResourceBundle
  
getResourceBundle(baseName:String, currentDomain:ApplicationDomain = null):ResourceBundle
[static] Gets a ResourceBundle when provided with a className.
ResourceBundle
  
Gets a string from a ResourceBundle.
ResourceBundle
  
Gets an array from a ResourceBundle.
ResourceBundle
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Protected Methods
 MethodDefined by
  
You can override this method to provide your own keys and values for a ResourceBundle or to add objects to your custom ResourceBundle.
ResourceBundle
Constructor detail
ResourceBundle()constructor
public function ResourceBundle()

Constructor.

Method detail
getBoolean()method
public function getBoolean(key:String, defaultValue:Boolean = true):Boolean

Gets a Boolean from a ResourceBundle.

Parameters
key:String
 
defaultValue:Boolean (default = true)

Returns
Boolean

Example
     [ResourceBundle("foo")]
     private static var packageResources:ResourceBundle;
     packageResources.getString("myBooleanKey");
     

getContent()method 
protected function getContent():Object

You can override this method to provide your own keys and values for a ResourceBundle or to add objects to your custom ResourceBundle. The following example adds a jpg image to a custom ResourceBundle. First you create a class called myPic. You then make a custom ResourceBundle called myBundle.

Returns
Object

See also


Example
     package {
     [Embed(source='picture.jpg')]
     public class myPic extends mx.core.SpriteAsset {}
     }
     package {
     import mx.resources.ResourceBundle;
     public class myBundle extends ResourceBundle {
     public function myBundle() { super(); }
     override protected static function getContent():Object {
     var contentObj:Object = new Object(); 
     content.push("myPic", myPic);
     return contentObj; }}}
     

getNumber()method 
public function getNumber(key:String):Number

Gets a Number from a ResourceBundle.

Parameters
key:String

Returns
Number

Example
      [ResourceBundle("foo")]
      private static var packageResources:ResourceBundle;
      packageResources.getString("myNumericKey");
      

getObject()method 
public function getObject(key:String):Object

Gets an object from a ResourceBundle. The folowing example shows how to get a jpg image with the getObject() method.

Parameters
key:String

Returns
Object

See also


Example
   [ResourceBundle("MyBundle")]
   private static var rb:ResourceBundle;
   var sprite:SpriteAsset = SpriteAsset(rb.getObject("myPic"));
   

getResourceBundle()method 
public static function getResourceBundle(baseName:String, currentDomain:ApplicationDomain = null):ResourceBundle

Gets a ResourceBundle when provided with a className.

Parameters
baseName:String
 
currentDomain:ApplicationDomain (default = null)

Returns
ResourceBundle

Example
     private static var rb:ResourceBundle;
     rb = getResourceBundle("fooBundle");
     

getString()method 
public function getString(key:String):String

Gets a string from a ResourceBundle.

Parameters
key:String

Returns
String

Example
         [ResourceBundle("controls")]
  private static var packageResources:ResourceBundle;
  packageResources.getString("okLabel");
  

getStringArray()method 
public function getStringArray(key:String):Array

Gets an array from a ResourceBundle.

Parameters
key:String

Returns
Array

Example
     [ResourceBundle("SharedResources")]
     private static var sharedResources:ResourceBundle;
     sharedResources.getStringArray("monthNames");
     





Comments


bdeitte said on Jul 24, 2006 at 6:55 AM :
The summary talks about a no-longer-valid way to work with resource bundles: "locale directory should be part of the compc ActionScript classpath but should not be a part of the ActionScript classpath that is used for mxmlc when building the main SWF file". That's not true. The locale directory is not used at all in compc (unless creating a SWC), and the locale directory should be a part of the mxmlc classpath.

The discussions of the name being the same, such as "can be the same name references", should be removed or deemphasized. This is
only relevant when using @Resource and is something that Flex has moved away from.

The properties files are not exactly the same as Java properties files-
Flex expects UTF-8 while Java .properties expect Unicode escaped ASCII.
No screen name said on Aug 26, 2006 at 4:06 AM :
In the Flex2 Builder, if you want to build with the property file, you need to set the compiler arguments as this:
-source-path locale/{locale}

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/2/langref/mx/resources/ResourceBundle.html