Packageqnx.locale
Classpublic class LocaleManager
InheritanceLocaleManager Inheritance flash.events.EventDispatcher

The LocaleManager class manages system locale changes. It enables you to both listen for system locale changes and set the locale of an application.

You can place a resource String file in the following folder in your application directory:

     locale/<locale id>/resource.properties 
     
where <locale id> is a valid locale ID, such as en_US. You can place any other localized assets in this folder, such as images and fonts. The getLocalePath() function returns the path to the current locale directory.

A resource file has the following format:

     key1=resource_string_1
     key2=resource_string_2
     key3=resource_string_3
     

where key1 is a resource key, and resource_string_1 is the translatable, locale-specific String. See the LocaleResourceBundle class for more information about resource bundles.

Note: Currently only the US English locale (en_US) is supported.

View the examples

See also

Locale
LocaleResourceBundle


Public Properties
 PropertyDefined By
  localeManager : LocaleManager
[static] [read-only] Returns the Singleton instance of the LocaleManager.
LocaleManager
Public Methods
 MethodDefined By
  
LocaleManager(enforcer:SingletonEnforcer)
Do not instantiate the LocaleManager by using this constructor.
LocaleManager
  
Returns the current locale of the application.
LocaleManager
  
Returns the current locale of the device.
LocaleManager
  
getLocalePath():String
[static] Returns the path from the application directory to the current locale directory in the format, locale/{locale id}/.
LocaleManager
  
getResource(key:String, ... args):String
Returns the resource for the specified key from the currently loaded localization file.
LocaleManager
  
setLocale(locale:String):void
Sets the locale of the application, given the locale ID.
LocaleManager
Events
 Event Summary Defined By
  Dispatched when the locale has been changed and the associated localization resources have finished loading.LocaleManager
Public Constants
 ConstantDefined By
  DEFAULT_LOCALE : String = en_US
[static] A String representing the default locale of the device.
LocaleManager
  LOCALE_FOLDER : String = locale/
[static] A String representing the location of the locale folder.
LocaleManager
Property Detail
localeManagerproperty
localeManager:LocaleManager  [read-only]

Returns the Singleton instance of the LocaleManager. You can only instantiate a single instance in an application.

The following example returns an instance of the LocaleManager and adds an event listener to listen for locale changes:

        var lm:LocaleManager = LocaleManager.localeManager;
        lm.addEventListener(Event.CHANGE, onLocaleChange);
        

Do not instantiate the LocaleManager from its public constructor.


Implementation
    public static function get localeManager():LocaleManager
Constructor Detail
LocaleManager()Constructor
public function LocaleManager(enforcer:SingletonEnforcer)

Do not instantiate the LocaleManager by using this constructor.

Instead, use the LocaleManager.localeManager property to return the Singleton instance.

Parameters
enforcer:SingletonEnforcer

See also

Method Detail
getCurrentLocale()method
public function getCurrentLocale():String

Returns the current locale of the application.

Returns
String — The ID of the current locale.

See also

getDeviceLocale()method 
public function getDeviceLocale():String

Returns the current locale of the device.

Returns
String — The ID of the current locale.
getLocalePath()method 
public static function getLocalePath():String

Returns the path from the application directory to the current locale directory in the format, locale/{locale id}/.

You can place any localized asset in the application's locale directory. This path allows you to reference an asset in the currently selected locale. In the example below, the getLocalePath() method is called to load a locale-specific image in an IconButton instance:

        private function initializeIconButton():void
        {
            myIconButton = new IconButton();
            myIconButton.setIcon( LocaleManager.getLocalePath() + 
                "/icon.gif");
            myIconButton.width = 100;
            myIconButton.setPosition(30, 30);
            
            this.addChild(myIconButton);
                            
        }
        

Returns
String — The path to the current locale folder.

See also

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

Returns the resource for the specified key from the currently loaded localization file.

In the following example, the getResource method is called to populate the label for a SegmentedControl button.

        mySegmented.addItem({label: LocaleManager.localeManager.getResource("day-sunday")});
        

Parameters

key:String — The key of the resource to be returned.
 
... args — Optional parameters that can be used as wildcard replacements in the returned String.

Returns
String — A String representing the value of the key in the localization file.
setLocale()method 
public function setLocale(locale:String):void

Sets the locale of the application, given the locale ID.

This function dispatches a flash.events.Event.CHANGE event when the current locale resources have been loaded and parsed.

Parameters

locale:String — The ID of the locale.

Event Detail
change Event
Event Object Type: flash.events.Event
Event.type property = flash.events.Event.CHANGE

Dispatched when the locale has been changed and the associated localization resources have finished loading.

Constant Detail
DEFAULT_LOCALEConstant
public static const DEFAULT_LOCALE:String = en_US

A String representing the default locale of the device.

LOCALE_FOLDERConstant 
public static const LOCALE_FOLDER:String = locale/

A String representing the location of the locale folder.

Examples

The following example uses the LocaleManager to load a set of locale-specific resource Strings. A list is instantiated and loaded with resource Strings and an event listener is added to listen for locale changes. When a locale change occurs, or when the application loads, the onLocaleChange function is called to load the resource strings for the current locale. You can add this same functionality to your application in order to globalize your Strings or images.

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    import qnx.locale.LocaleManager;
    import qnx.ui.data.DataProvider;
    import qnx.ui.listClasses.List;
    import qnx.ui.listClasses.ListSelectionMode;
    import qnx.ui.text.Label;
    
    
    [SWF(height="600", width="1024", frameRate="30", backgroundColor="#FFFFFF")]
    public class MyLocaleChangeSample extends Sprite
    {
        
        private var myList:List;
        private var myLabel:Label;
        private var myDP:DataProvider;
        
        public function MyLocaleChangeSample()
        {
            initializeUI();
        }
        
        private function initializeUI():void
        {
            
            //Listen for locale changes.
            var lm:LocaleManager = LocaleManager.localeManager;
            lm.addEventListener(Event.CHANGE, onLocaleChange);
            
            //Create a label.
            myLabel = new Label();    
            myLabel.width = 200;
            myLabel.setPosition(100, 150);    
            stage.addChild(myLabel);
            
            //Create a list.
            myList = new List();
            myList.setPosition(100, 200);            
            myList.width = 400;
            myList.height = 500;            
            myList.selectionMode = ListSelectionMode.MULTIPLE;
            stage.addChild(myList);
            
            myDP = new DataProvider();
        }
        
        //This function is called when the locale of the device changes.
        private function onLocaleChange(event:Event):void{
            
            myDP.setItems([{label: LocaleManager.localeManager.getResource("list-corn")},
                           {label: LocaleManager.localeManager.getResource("list-potato")},
                           {label: LocaleManager.localeManager.getResource("list-asparagus")},
                           {label: LocaleManager.localeManager.getResource("list-bacon")},
                           {label: LocaleManager.localeManager.getResource("list-celery")}]);
            
            myList.dataProvider = myDP;
            
            myLabel.text = LocaleManager.localeManager.getResource("label-title");
            
        }
        
    }
}
    

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:

list-potato=Potato 
list-asparagus=Asparagus 
list-bacon=Bacon 
list-celery=Celery
list-corn=Corn
label-title=Grocery List