Sunday, March 6, 2011

Flex Localization: refresh DataProvider values

I have a ToggleButtonBar with a DataProvider setup like this:

   <mx:ToggleButtonBar itemClick="clickHandler(event);" selectedIndex="0">
    <mx:dataProvider>
     <mx:String>{resourceManager.getString('dashboard','daily')}</mx:String>
     <mx:String>{resourceManager.getString('dashboard','monthly')}</mx:String>
     <mx:String>{resourceManager.getString('dashboard','quarterly')}</mx:String>
     <mx:String>{resourceManager.getString('dashboard','yearly')}</mx:String>
    </mx:dataProvider>
   </mx:ToggleButtonBar>

To switch locale to Chinese, I have a combobox with this handler:

resourceManager.localeChain = "zh_CN";

My problem is that on locale change, while the labels for all the other controls on the screen dynamically reload for the new locale, the dataProvider values don't refresh. I can manually reset them in code, but is there a cleaner solution?

From stackoverflow
  • Maybe if you make a getter bindable to a custom event for ex: "langChange"

    [Bindable("langChange")]
    public function get dataProviderToggleB():ArrayCollection
    {
        var arr :ArrayCollection = new ArrayCollection();
    
        arr.addItem(resourceManager.getString('dashboard','daily'));
        arr.addItem(resourceManager.getString('dashboard','monthly'));
    
        return arr; 
    }
    

    and in your "resourceManager.localeChain" setter you dispatch:

    dispatchEvent(new Event("langChange"));
    

    and you can used like this:

    <mx:ToggleButtonBar dataProvider="{dataProviderToggleB} itemClick="clickHandler(event);" selectedIndex="0">
    

    I hope this would help you.

  • I would abstract out the data for your data provider into a bindable variable, then just reset the data provider when you change locals.

    <mx:Script>
        <![CDATA[
         [Bindable]
         myArray:Array = new Array(
            [resourceManager.getString('dashboard','daily')]
          , [resourceManager.getString('dashboard','monthly')]
          , [{resourceManager.getString('dashboard','quarterly')]
          , [resourceManager.getString('dashboard','yearly')]);
    
        ]]>
    </mx:Script>
    
    
    <mx:ToggleButtonBar itemClick="clickHandler(event);" 
    selectedIndex="0" id="myToggleButtonBar" dataprovider="{myArray}" />
    

    Then you can just say

    myToggleButtonBar.dataProvider = myArray;
    

    after you swap the locals and it should work.

    Disclaimer, there may be some minor errors in my code, I obviously am not able to test it and I don't have flex builder available right now to even check my syntax so I hope I didn't make any spelling mistakes. But this should get you in the ballpark.

  • You should keep 'daily', ... in your array and use a labelFunction to translate the label. When the resourceManager sends a change event you should do a combo.labelFunction = labelFunction

0 comments:

Post a Comment