qnx.locale.LocaleResourceBundle

LocaleResourceBundle

Properties | Methods | Events | Examples


Packageqnx.locale
Classpublic class LocaleResourceBundle
InheritanceLocaleResourceBundle Inheritance flash.events.EventDispatcher

The LocaleResourceBundle class loads and parses localized String files.

The following parsing rules are supported:

  • Lines starting with "#" or "!" are not parsed and are considered comments.
  • Any whitespace on the left of the "=" symbol is stripped out and the resulting value is used as the key.
  • Any whitespace at the start of the value, to the right of the "=" symbol, is stripped out.
  • To include whitespace at the beginning of your value, add a "\" to the beginning of the String in order to escape it.
  • Any whitespace at the end of a value is preserved and is not stripped out.
  • You can combine multi-line values by adding a "\" at the end of the line.

The Strings that you specify in the resource file can also contain wildcards. Each wildcard is replaced at load-time with the String value that you specify as an argument to the getResource method. For example, the resource String below contains the String (a name) "Westlee Barichuk" and two wildcards, preceding and following the name:

     full-name= %1% Westlee Barichuk  %2%
     

You can replace the wildcards by passing in your own String arguments into the getResource() method:

     myList.addItem({label: LocaleManager.localeManager.getResource("full-name", "Mr.", "esq." )});
     

The arguments in the parameter list follow the order in which they appear in the resource String. The same wildcard can be specified more than once. The example above creates the following String:

     Mr. Westlee Barichuk esq.
     


Class information:
PlayBook Tablet OS Version:   1.0.6

View the examples

See also

Locale
LocaleManager



Public Properties

 PropertyDefined By
  loaded : Boolean
[read-only] Returns true if a property file has been loaded and parsed.
LocaleResourceBundle
  resources : Dictionary
[read-only] Returns the list of resources.
LocaleResourceBundle

Public Methods

 MethodDefined By
  
Creates an instance of a LocaleResourceBundle.
LocaleResourceBundle
  
getResource(key:String, ... args):String
Returns the value for the specified key from the loaded property file.
LocaleResourceBundle
  
load(path:String):void
Loads and parses a property file asynchronously given the path to the resource file.
LocaleResourceBundle
  
Merges the supplied LocaleResourceBundle into this one.
LocaleResourceBundle
  
open(file:File):void
Loads and parses a property file given the path to the file.
LocaleResourceBundle
  
parseData(data:String):void
Parses a String into the resource bundle.
LocaleResourceBundle

Events

 Event Summary Defined By
  Dispatched after a property file has been successfully loaded and parsed.LocaleResourceBundle
  Dispatched if the specified file to load cannot be found.LocaleResourceBundle

Property Detail

loaded


loaded:Boolean  [read-only]

Returns true if a property file has been loaded and parsed.


Implementation

public function get loaded():Boolean

resources


resources:Dictionary  [read-only]

Returns the list of resources.


Implementation

public function get resources():Dictionary

Constructor Detail

LocaleResourceBundle()


public function LocaleResourceBundle()

Creates an instance of a LocaleResourceBundle.

Method Detail

getResource()


public function getResource(key:String, ... args):String

Returns the value for the specified key from the loaded property file. If the key is not found in the bundle, the literal key value will be returned instead.

Parameters

key:String — The key for the value in the property file.
... args — Optional parameters that can be used as wildcard replacements in the returned String.

Returns

String — Returns the value for the specified key.

load()


public function load(path:String):void

Loads and parses a property file asynchronously given the path to the resource file. Because this is an asynchronous operation, you must wait for the resource bundle to load before you access it. An Event.COMPLETE event is fired after the file is loaded and parsed.

The following example shows a simple bundle load operation that creates and initializes a resource bundle. An event listener is added to the bundle to listen for the bundle load (Event.COMPLETE) event. The getLocalePath() method is used to specify the path to the resource bundle.

            //Load the resource bundle
            var MyBundle:LocaleResourceBundle = new LocaleResourceBundle();
            MyBundle.addEventListener( Event.COMPLETE, onResourceLoad );
            MyBundle.load(LocaleManager.getLocalePath() + "/resource.properties"); 
                
            myDP = new DataProvider();
            
        }
        
        //This function is called when the resource bundle is loaded. 
        private function onResourceLoad(event:Event):void 
        {
            
            trace(LocaleManager.localeManager.getCurrentLocale());
            //Store the incoming event target as a bundle.
            var bundle:LocaleResourceBundle = event.target as LocaleResourceBundle;
            
            //Update the buttons in the segmented control with the bundle resources
            myDP.setItems([{label: bundle.getResource("day-sunday")},
                           {label: bundle.getResource("day-monday")},
                           {label: bundle.getResource("day-tuesday")},
                           {label: bundle.getResource("day-wednesday")},
                           {label: bundle.getResource("day-thursday")},
                           {label: bundle.getResource("day-friday")},
                           {label: bundle.getResource("day-saturday")}]);
            
            
            mySegmented.dataProvider = myDP;
                            
        }
        

Parameters

path:String — The path to the resource file to be loaded.

See also

open()

merge()


public function merge(locale:LocaleResourceBundle):void

Merges the supplied LocaleResourceBundle into this one. If a resource does not exist it is replaced by the resource in the supplied locale.

Parameters

locale:LocaleResourceBundle — The LocaleResourceBundle to merge.

open()


public function open(file:File):void

Loads and parses a property file given the path to the file. This is a synchronous operation.

The following example shows a simple function that creates and opens a resource bundle. The applicationDirectory class and resolvePath() method are used to specify the location of the resource bundle. The applicationDirectory is used to specify the root application folder, and to pass a File object to the open method.

        public function bundleOpen():void
        {
            bundle = new LocaleResourceBundle();
            bundle.open( File.applicationDirectory.resolvePath( "locale/en_US/resource.properties" ) );        
        }
        

Parameters

file:File — The File object to open and parse.

See also

load()

parseData()


public function parseData(data:String):void

Parses a String into the resource bundle. This is a synchronous operation.

Parameters

data:String — The string buffer to parse.

Event Detail

complete


Event Object Type: flash.events.Event
Event.type property = flash.events.Event.COMPLETE

Dispatched after a property file has been successfully loaded and parsed.

See also

open()

ioError


Event Object Type: flash.events.IOErrorEvent
IOErrorEvent.type property = flash.events.IOErrorEvent.IO_ERROR

Dispatched if the specified file to load cannot be found.

See also

load()

Examples

In the following example, a LocaleResourceBundle class is instantiated and a resource file is loaded and parsed in order to populate a SegmentedControl instance.

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    import qnx.locale.LocaleManager;
    import qnx.locale.LocaleResourceBundle;
    import qnx.ui.buttons.SegmentedControl;
    import qnx.ui.data.DataProvider;
    [SWF(height="600", width="1024", frameRate="30", backgroundColor="#FFFFFF")]
    public class MyLocaleSample extends Sprite
    {
        
        private var mySegmented:SegmentedControl;
        private var myDP:DataProvider;
        
        public function MyLocaleSample()
        {
            initializeUI();
        }
        
        private function initializeUI():void
        {
            
            //Create a segmented control
            mySegmented = new SegmentedControl();
            mySegmented.setPosition(100, 500);            
            mySegmented.width = 750;
            mySegmented.height = 50;    
            stage.addChild(mySegmented);
            
            //Load the resource bundle
            var MyBundle:LocaleResourceBundle = new LocaleResourceBundle();
            MyBundle.addEventListener( Event.COMPLETE, onResourceLoad );
            MyBundle.load(LocaleManager.getLocalePath() + "/resource.properties"); 
            
            myDP = new DataProvider();
        
        }
        
        //This function is called when the resource bundle is loaded. 
        private function onResourceLoad(event:Event):void 
        {
            
            trace(LocaleManager.localeManager.getCurrentLocale());
            //Store the incoming event target as a bundle.
            var bundle:LocaleResourceBundle = event.target as LocaleResourceBundle;
                    
            //Update the buttons in the segmented control with the bundle resources
            myDP.setItems([{label: bundle.getResource("day-sunday")},
                           {label: bundle.getResource("day-monday")},
                           {label: bundle.getResource("day-tuesday")},
                           {label: bundle.getResource("day-wednesday")},
                           {label: bundle.getResource("day-thursday")},
                           {label: bundle.getResource("day-friday")},
                           {label: bundle.getResource("day-saturday")}]);
            
            
            mySegmented.dataProvider = myDP;
                            
        }
    
    }
}
     

The example above uses a resource file (resource.properties) which is located in the English language locale directory (locale/en_US). The file is listed below:

day-monday=Monday
day-tuesday=Tuesday
day-wednesday=Wednesday
day-thursday=Thursday
day-friday=Friday
day-saturday=Saturday
day-sunday=Sunday