15 Replies Latest reply: Feb 9, 2015 1:30 AM by kkm19 RSS

    Localization not working as expected

    valley t Community Member

      Hi there,

       

      i'm struggling with an AIR 3.6 mobile app that should support several languages (for testing 'de_DE' and 'en_US').

      Here is what i do in Flash Builder 4.7:

       

      1. adding resources.properties in ./locale/en_US and ./locale/de_DE with one key 'test' and a translated string in it
      2. adding the folder 'locale/{locale}' in ActionScript Build Path -> Source Path
      3. adding '-locale=de_DE,en_US' in ActionScript Compiler as an additional compiler argument
      4. in source code i add the following ResourceBundle metadata:

                [ResourceBundle("resources")]

               public class Root extends Sprite

       

         5. as a test i use the following code:      

                var sortedLocales:Array = LocaleUtil.sortLanguagesByPreference(["de_DE", "en_US"], Capabilities.languages, "de_DE");

                ResourceManager.getInstance().localeChain = sortedLocales;

                trace("String is: ", ResourceManager.getInstance().getString("resources", "test"));

       

      When switching the language on my iPhone 4 from English to German i would expect that the trace shows the German string, but it's always the English one.

      Also when i try to force the German language with

                 ResourceManager.getInstance().localeChain = ["de_DE"];

       

      it shows the English string.

      Capabilities.languages shows me that German is supported and when selected it's on 1st place in the list.

      I also tried with languages "de" and "en" -> no change

      When i check the packaged files for e.g. iOS i see that my 2 resources.properties files are not included and whatever i try they never get into the package.

       

      What is missing here or is there a misunderstanding in how the language is chosen ?

      Thank you for your help!

        • 1. Re: Localization not working as expected
          valley t Community Member

          Meanwhile i gave up ResourceManager and ResoureBundle and wrote my own solution.

          • 2. Re: Localization not working as expected
            TopoBuilder Community Member

            Can you tell us your solution?

            • 3. Re: Localization not working as expected
              valley t Community Member

              Sure:

               

              1. adding the key/value pairs in /locales/resources.xy_AB.txt files and adding the locales directory as a source directory (locales directory is outside the source folder)
              2. againg using LocaleUtil.sortLanguagesByPreference function to determine the list of most suitable languages
              3. i wrote a new class LocalesManager with a function initData(locale:String, resource:String, format:String = "txt"):void, which  can be called various times for different resources, where the function loads the correct file, parses it and stores the found key/value pairs in a Dictionary, which itself will be stored in a Dictionary that handles the resources
              4. after every successful initData call the function dispatches an Event that the loading is finished
              5. to receive a localized string i wrote another  function getString(resource:String, key:String):String

               

              That's it. When switching the language on my mobile device i get the strings back for the new language.

              • 4. Re: Localization not working as expected
                le rekomat Community Member

                Hi there


                I'm having exactly the same problem here (AIR 3.6, Flash Builder 4.7). But somehow I don't really feel comfortable to write my own LocalizationManager when there's a built-in solution around. So I'm still trying to find the problem.


                • I noticed that when I update the compiler argument (-locale=en_US,de_DE ), the ResourceManager *sometimes* switches the language, but somehow randomly and definitely not based the value set for localeChain.
                • Furthermore, ResourceManager.getInstance().getLocales() does not return anything.
                • The strange thing: Even when I do not include the localization resources ('locale/{locale}' in ActionScript Build Path -> Source Path) the resources in 'locale/en_US' or 'locale/de_DE' are available to the ResourceManager.

                 

                Anyone with similar experiences?

                I'm grateful for any hint!

                • 5. Re: Localization not working as expected
                  valley t Community Member

                  Since i use my own locales handler now i didn't investigate that problem further.

                  You describe exactly the same points i struggled with and couldn't solve. With the new solution i have seen that the .properties files still were not added to the build, therefore i changed them to .txt files.

                  Perhaps you could try that for your project.

                   

                  If that doesn't help i can only encourage you to create your own LocalizationManager class. It took me 1h to write it and works without problems since then.

                  • 6. Re: Localization not working as expected
                    le rekomat Community Member

                    Thanks, valley t!

                     

                    I'm definitely close to build my own localization solution based on your proposal! 

                     

                    As far as I understand, it is expected behaviour that the .properties files do not get packed into the build as their content is compiled into the app with [ResourceBundle("resources")]. This would also explain why the compiled app is slightly smaller in filesize when compiled without the -locale parameter. It looks like it is not neccessary to add the directories "locale/{locale}" into the package (in Properties > ActionScript Build Path > Source Path). That's how I understand the documentation:

                     

                    When your application starts, the ResourceManager is automatically populated with whatever resource bundles were compiled into the application. If you create a code module, by default the resources that its classes need are compiled into the module. When the module is loaded into an application, any bundles that the application does not already have are added to the ResourceManager.

                     

                    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/resources/IResourceM anager.html

                     

                    Btw.: I made a localization test with Flex 4.6, where everything worked as expected.

                    • 7. Re: Localization not working as expected
                      le rekomat Community Member

                      Ok… I finally took care of this issue and ended up – guess what – writing my own solution. It's very basic but that's all I need for the moment. And hey: It's working!

                       

                      As it looks like we're not the only ones facing this issue it seems practical to share the code:

                      https://github.com/rekomat/AS3-LocaleManager

                       

                      Note: The code has not been proven in production yet. Improvements welcome!

                      • 8. Re: Localization not working as expected
                        valley t Community Member

                        Cool!

                        From what i see we have more or less the same solution now ;-)

                        • 9. Re: Localization not working as expected
                          le rekomat Community Member

                          That's good news!

                           

                          I'm not really sure about the parsing part. I currently use String.split() twice: First to break the file contents into single lines and then to break the lines into key/value-pairs. After that, whitespace is cleaned using StringUtil.trim() (lines 242 - 261). Maybe this could be optimized for efficiency.

                           

                          Do you have a better solution for parsing? Would be great if you would let me know!

                           

                          Happy coding!

                          • 10. Re: Localization not working as expected
                            valley t Community Member

                            Actually it's pretty much the same in my code:

                             

                            // first split for line breaks
                            var lines:Array = data.split("\n");
                            // now iterate over each line and populate the dictionary
                            var key:String;
                            var value:String;
                            var tokens:Array;
                            var line:String;
                            for (var i:int = 0; i < lines.length; i++) {
                            
                                line = lines[i];
                                if (line == null || line.length == 0 || line.charAt(0) == "#" || line.charAt(0) == "!")
                                    continue;
                                tokens = line.split("=");
                                if (tokens.length == 2) {
                            
                                    key = tokens[0];
                                    value = tokens[1];
                                    if (!dict.hasOwnProperty(key)) {
                                        dict[key] = value;
                                    } else {
                                        throw new IllegalOperationError("Duplicate insert found for key " + key);
                                    }
                            
                                } else {
                                    throw new IllegalOperationError("Error with line " + lines[i] + ": More than 2 tokens found");
                                }
                            }
                            resources[resource] = dict;
                            dispatchEvent(new Event(LOCALE_RESOURCE_LOADED));
                            

                             

                            Cheers.

                            valley

                            • 11. Re: Localization not working as expected
                              le rekomat Community Member

                              Thanks for posting, valley!

                              Looks like we're coding in-sync.

                               

                              Happy weekend (almost there)!

                              • 12. Re: Localization not working as expected
                                valley t Community Member

                                Thank you. Same to you.

                                Do you also live in Switzerland ?

                                • 13. Re: Localization not working as expected
                                  le rekomat Community Member

                                  Yep! I'll send you a PM shortly…

                                  • 14. Re: Localization not working as expected
                                    CyberProdigy

                                    Probem happened in AIR3.5 change to AIR3.6 and is present in AIR 4.0.

                                     

                                    Here is what I have.

                                    I'm building with ANT script

                                    <mxmlc

                                                                  file="Main.as"

                                                                  output="bin"

                                                                  link-report="link-report.xml"

                                                                  locale="en_US,da_DK,de_DE,nl_NL,sv_SE,nb_NO,pl_PL,fr_FR,fi_FI,en_GB,tr_TR, es_ES"

                                                                  allow-source-path-overlap="true"

                                                        >

                                     

                                    and then latter in the antscript

                                    <source-path path-element="${PROJECT}/src/locale/{locale}"/>

                                     

                                    And then in the code

                                     

                                    [ResourceBundle("myResources")]

                                    public class LocalesManager {

                                    ...

                                    }

                                     

                                    Works as expecte in AIR 3.5, but by simply changing SDK to 3.6 or any higher

                                    compile fails with

                                    Error: Unable to resolve resource bundle 'myResources'

                                    [mxmlc]           [ResourceBundle("myResources")]

                                    [mxmlc]           ^

                                    [mxmlc]

                                    [mxmlc] command line

                                    [mxmlc] Warning: 'compiler.locale' is not fully supported.

                                     

                                    Is it because compiler.locale is not fully supported? Confused,

                                    • 15. Re: Localization not working as expected
                                      kkm19 Community Member

                                      I also have a problem with it. I believe

                                        <fx:Metadata>

                                          [ResourceBundle("bundle")]

                                        </fx:Metadata>

                                      isnt support yet?