Fletch
Fletch

Reputation: 5219

Is there an easy way to automatically format dates according to locale in Flex 4?

I understand that one can use local-specific ResourceBundles in combination with DateFormatters to format dates according to locale. However this is a manual process - is there an automatic way to do this or to set a default for the application?

In Java for example, all your dates will automatically appear in dd/mm/yy or mm/dd/yy format simply by setting the locale. In Flex, a default date output will always be in US format unless manually formatted otherwise. I'm looking for a way to get closer to the Java functionality.

Upvotes: 0

Views: 923

Answers (3)

purpledave
purpledave

Reputation: 11

I did this recently using flah.globalization classes: see its very informative about getting locale etc.. http://www.adobe.com/devnet/flashplayer/articles/flash_globalization_package.html here's my code:

remember to call init(); on creation complete !

<fx:Script>
    <![CDATA[

        import flash.globalization.DateTimeFormatter;
        import flash.globalization.DateTimeStyle;
        import flash.globalization.StringTools;

        import mx.collections.ArrayCollection;

        import spark.events.IndexChangeEvent;


        [Bindable]
        private var listColl:ArrayCollection;

        private var localeList:Array = new Array("en-US", "fr-FR", "es-ES","ja-JP", "hi-IN","ru-RU");


        private var country:String;


        private function init():void{

            // set the dp for drop down;
            listColl = new ArrayCollection(localeList);
            country = localeList[0];
        }


    private function doDateLabel(item:Date):String {

    trace("input = " + item);

    if(item != null) {


        var locale:String = country;
        if(locale != null){

        var dtf:DateTimeFormatter = new DateTimeFormatter(locale);

            dtf.setDateTimeStyles(DateTimeStyle.SHORT, DateTimeStyle.NONE);
        /*
            DateTimeSyle.MEDIUM
        DateTimeSyle.LONG   

            */


            var shortDate:String = dtf.format(item);
            trace(shortDate + " (" + dtf.getDateTimePattern() + ")");

            }    
        }
    return shortDate;
    }

        protected function dropDownList_changeHandler(evt:IndexChangeEvent):void {
            country = countryList.selectedItem;
        }




    ]]>
</fx:Script>


<s:HGroup width="100%" height="100%" gap="20" top="50"  left="50">

    Hope that's what you were after 
    <mx:DateField id="begin" width="200"
                  showToday="true"
                  labelFunction="doDateLabel" 
                  parseFunction="null"/>

    <s:DropDownList id="countryList"
                    requireSelection="true" prompt="Please select an Country" 
                    horizontalCenter="0" top="20" dataProvider="{listColl}" 
                    change="dropDownList_changeHandler(event);">

    </s:DropDownList>
</s:HGroup>

Upvotes: 1

JabbyPanda
JabbyPanda

Reputation: 871

It is a feature of Flash Player 10.1 placed in flash.globalization package

The flash.globalization package in Flash Player: Cultural diversity without complexity

Upvotes: 0

Related Questions