Using the LocaleID class

The LocaleID class, available in Flash Player 10.1, provides methods and properties for using locale IDs. Locale IDs are strings that represent a particular locale. They can be useful when deciding whether to change the layout direction of an application.

The LocaleID class includes the isRightToLeft() method, which returns true if the currently set locale uses an RTL character code set, but otherwise returns false. You can use this method to set the values of the layoutDirection and direction properties. When you change locales in the following example, the application checks the isRightToLeft() method and sets the layoutDirection and direction properties on the Application object accordingly:
<?xml version="1.0" encoding="utf-8"?>
<!-- mirroring\LocaleIDMirroringExample.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx"
            width="300" height="300"
            layoutDirection="{localeBasedDirection}"
            direction="{localeBasedDirection}"
            creationComplete="updateSelection()">
    <fx:Script>
        <![CDATA[
            import flash.globalization.LocaleID;
            import mx.collections.ArrayCollection;          

            [Bindable]
            public var localeBasedDirection:String;

            [Bindable]
            public var locID:LocaleID;

            [Bindable]
            public var myDP:ArrayCollection = new ArrayCollection(
                [ {name:"US", locale:"en_US"},
                    {name:"Israel", locale:"he_IL"},
                    {name:"France", locale:"fr_FR"} ]);
            
            private function updateSelection():void {
                locID = new LocaleID(localeIdList.selectedItem.locale);                
                isRTLLabel.text = "RTL: " + locID.isRightToLeft().toString();
                getRegionLabel.text = "Region: " + locID.getRegion();
                getLanguageLabel.text = "Language: " + locID.getLanguage();
                
                if (locID.isRightToLeft()) {
                    localeBasedDirection = "rtl";
                } else {
                    localeBasedDirection = "ltr";
                }
            }
        ]]>
    </fx:Script>

    <s:Panel title="Select Locale" height="200" width="250">
        <s:layout>
            <s:VerticalLayout paddingLeft="10" paddingRight="10" paddingTop="10"/>
        </s:layout>
        <s:VGroup>
            <s:DropDownList id="localeIdList" selectedIndex="0" 
                dataProvider="{myDP}"
                labelField="name"
                change="updateSelection();"/>   
            <s:Label id="isRTLLabel"/>
            <s:Label id="getRegionLabel"/>
            <s:Label id="getLanguageLabel"/>
        </s:VGroup>
    </s:Panel>
</s:Application>

The executing SWF file for the previous example is shown below:

This example requires classes in the flash.globalization.* package, which are available in the 10.1 or later playerglobal.swc file. When compiling this example, be sure to set the target player version to 10.1 or later. In Flash Builder, this option is on the Compiler properties page. On the command-line, you can set the target Player version by using the target-player option; for example:
mxmlc -target-player=10.1 MyApp.mxml
In your applications, you will typically have some additional logic that sets the locale in your application, such as looking at the OS language code or accepting a user-configured setting. For information on setting locales in your application, see Setting the locale. For more information about using the classes in the flash.globalization.* package, see Flash Player: Cultural diversity without complexity.