6 Replies Latest reply on Aug 8, 2012 10:54 PM by Ticore

    How to get the localeChain for bindings

    Hans Preuer

      Hello,

       

      I have a flex application where I can change the language on the fly. It works well. I've only one issue with switching the image folders. I have the image folders under "images/app/de_DE/image1.png" for German, "images/app/en_US/image1.png" for english and so on. I load the images with

       

      <mx:Image source="images/app/{resourceManager.localeChain.join('')}/image1.png" id="image1" />

       

      The .join Method is only a workaround I have found to get the countycode in string-format.

       

      The image doesn't change on the fly, but when I reload the page, it worked also fine. I also get a warning: Data binding will not be able to detect assignments to "localeChain". It's no big issue because it works for me, but I tried a lot different ways to do this right, but I didn't find "the right way".

      Can anyone show me the "right way"???

       

      Thanks

      Hans

        • 1. Re: How to get the localeChain for bindings
          Paul Reilly Level 1

          My suggestion is to create a local variable for the joined locale chain and mark it with metadata that signals to the compiler that no change events will be dispatched.  For example:

           

            <mx:Script>

              [Bindable("__NoChangeEvent__")]

              private var joinedLocaleChain:String = resourceManager.localeChain.join('');

            </mx:Script>

           

          and then you could do something like this:

           

            <mx:Image source="images/app/{joinedLocaleChain}/image1.png" id="image1" />

           

          Now if you want the image to update when the locale chain changes, you could change the metadata to something like:

           

            [Bindable("localeChainChanged")]

           

          and you could dispatch this event when you know the locale chain has changed.

          • 2. Re: How to get the localeChain for bindings
            Hans Preuer Level 1

            Paul,

             

            Thanks for your reply. I tested your solution, but it's all the same as it was before: The warning still remains and the refresh is like the old one.

            What do I need: I need a method to get the current locale ("de_DE", "en_US", "fr_FR", etc.), so I can use this method in a binding expression and this must be triggered by a new assignment to resourceManager.localeChain (f.e. resourceManager.localeChain = ["de_DE"];). Is this possible?

             

            Thanks

            Hans

            • 3. Re: How to get the localeChain for bindings
              Paul Reilly Level 1

              If you attach a complete and simple test case, I'll try to get it working like I think it should.

              • 4. Re: How to get the localeChain for bindings
                GordonSmith Level 4

                In general, the localeChain can be a list of locales to be searched for localized resources, such as [ "fr_CA", "fr_FR", "en_US" ]. By calling join() you're assuming that there will never be more than one entry in this array, so I don't recommend that. One of the features of the ResourceManager is the ability to do runtime fallback so that, for example, if there isn't a French-for-Canada resource you get the French-for-France resource. You're making that impossible with your approach.

                 

                I recommend letting the ResourceManager do its normal work of searching the localeChain for you... in other words, deal with localized images in the same way that you deal with localized Strings.

                 

                If you are embedding images, then in your fr_CA images.properties file, declare a resource representing the image:

                 

                image1=Embed("images/app/fr_CA/image1.png")

                 

                and use the ResourceManager to fetch the class that got embedded to represent that image:

                 

                <mx:Image source="{resourceManager.getClass('images', 'image1')"/>

                 

                If you are loading images from a URL, then in your fr_CA images.properties file, declare a resource representing the URL String:

                 

                image1=images/app/fr_CA/image1.png

                 

                and use the ResourceManager to fetch that URL as a String:

                 

                <mx:Image source="{resourceManager.getString('images', 'image1')"/>

                 

                Gordon Smith

                Adobe Flex SDK Team

                • 5. Re: How to get the localeChain for bindings
                  Hans Preuer Level 1

                  Gordon,

                   

                  You are absolutly right. Sometimes I can't see the wood for the trees! It's an ordinary string in the resource-files. I just add a new string to each resource-file like:

                   

                  myResources.content["ACT_LOCALE"] = "de_AT";

                   

                  for the german resource-file and changed the image-tag to:

                   

                  <mx:Image source="images/app/{resourceManager.getString('DesignCD', 'ACT_LOCALE')}/design_roads_FGSV.jpg" id="designImage" />

                   

                  and now, there are no warnings and the images refreshes without a reload only by setting the resourceManager.localeChain to the new locale.
                  Thanks for opening my eyes.
                  Hans
                  • 6. Re: How to get the localeChain for bindings
                    Ticore

                    I found a dirty work could binding to current locale.

                     

                     

                    <mx:Image source="images/app/{

                      resourceManager.getString('', ''),

                      (function():*{ return resourceManager.localeChain.concat().shift(); })()

                    }/design_roads_FGSV.jpg" id="designImage" />