5 Replies Latest reply on Mar 11, 2010 10:13 AM by cheftimo

    Changing locales at runtime and ComboBox

    KapFlorian

      Hi,

       

      I have an ArrayCollection with localized strings used in a ComboBox.

      However, I can't find a better solution than recreating a new instance of the dataProvider in order to update the ComboBox when I change locale at runtime.

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
          xmlns:mx="http://www.adobe.com/2006/mxml"
          layout="vertical"
          applicationComplete="applicationCompleteHandler()"
          >
         
          <mx:Metadata>
              [ResourceBundle("test")]
          </mx:Metadata>
         
          <mx:Script>
              <![CDATA[
                  import mx.events.CollectionEventKind;
                  import mx.events.CollectionEvent;
                  import mx.collections.ArrayCollection;
                 
                  [Bindable]
                  private var locales:Array = [ "fr_FR","en_US" ];


                  [Bindable]
                  public var dates:ArrayCollection;
                 
                  private function applicationCompleteHandler():void
                  {
                      resourceManager.addEventListener(Event.CHANGE, resourceMnager_changeHandler);
                      localeComboBox.selectedIndex = locales.indexOf(resourceManager.localeChain[0]);
                      dates = new ArrayCollection([resourceManager.getString("test", "JANUARY"), resourceManager.getString("test", "FEBRUARY")]);
                  }
                 
                  private function localeComboBox_changeHandler():void {
                      resourceManager.localeChain = [ localeComboBox.selectedItem ];
                  }
                 
                  private function resourceMnager_changeHandler(e:Event):void
                  {
                      datesComboBox.dataProvider = new ArrayCollection([resourceManager.getString("test", "JANUARY"), resourceManager.getString("test", "FEBRUARY")]);;
                  }

              ]]>
          </mx:Script>
         
          <mx:ComboBox id="localeComboBox"
              dataProvider="{locales}"
              change="localeComboBox_changeHandler()"
          />


          <mx:ComboBox
              id="datesComboBox"
              dataProvider="{dates}" />
             
          <mx:Label text="{resourceManager.getString('test', 'JANUARY')}" />

         
      </mx:Application>

       

       

      Any idea?

        • 1. Re: Changing locales at runtime and ComboBox
          cheftimo Level 2

          A while ago, I had what sounds like the same situation you have now in an English-Spanish project.

           

          I have two ArrayCollections (one in each language) of keywords that populate a ComboBox. When the locale changes, the dataProvider = the appropriate keyword list.

           

          Note that I am not a professional programmer/developer. At the time I dealt with this issue, I got advice from a couple of the gurus in these forums; the consensus was that there is nothing wrong with manipulating the dataProvider in this fashion.

           

          If you wish, browse to http://www.timos.com/timos/green/ct/CounterThink.cfm and see this in action.

           

          Have fun,

           

          Carlos

          • 2. Re: Changing locales at runtime and ComboBox
            KapFlorian Level 1

            Hi,

             

            Thanks for replying.

             

            Your workaround is probably suitable for small applications with 2 languages at most.

            In my case, I have to deal with a large applications with several languages.

            I can't imagine having to declare a dataProvider for each language ... definately not a scalable solution

            • 3. Re: Changing locales at runtime and ComboBox
              Arun Lal

              Try using ResourceManager API in Flex. Check the Adobe help site for details.

              • 4. Re: Changing locales at runtime and ComboBox
                KapFlorian Level 1

                Can you be a bit more specific please? I'm already using ResourceManager in my example.

                 

                Thanks.

                • 5. Re: Changing locales at runtime and ComboBox
                  cheftimo Level 2

                  Kap, don't take me too literally. I gave your example code a closer look. It appears that you have the text for the CB labels in a resource bundle: I would see that as a nightmare – I hope I am wrong and that you have all that data in some kind of dataset; I have mine in a DB table.

                   

                  Picture this: when the app starts, you bring the data into an array, let's call it rawData. Then you declare an AC called, say, 'datesAC' and populate it by filtering rawData, for the current locale. Each time the locale changes, you extract the right data from rawData, populate datesAC with that data, sort it and refresh it. Your ResourceBundle does not enter into this picture.

                   

                  So you don't actually declare a dataProvider for each language: all you have to deal with are rawData and datesAC; datesAC has to change each time you change the locale regardless of how you set this up.

                   

                  HTH. If you want more details, I'll be happy to provide them.

                   

                  Best regards,

                   

                  Carlos