• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Localization not working as expected

Explorer ,
Mar 06, 2013 Mar 06, 2013

Copy link to clipboard

Copied

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!

TOPICS
Development

Views

8.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 07, 2013 Mar 07, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

Can you tell us your solution?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 26, 2013 Mar 26, 2013

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 27, 2013 Mar 27, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 27, 2013 Mar 27, 2013

Copy link to clipboard

Copied

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/IResourceManager.htm...

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2013 Apr 04, 2013

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2013 Apr 04, 2013

Copy link to clipboard

Copied

Cool!

From what i see we have more or less the same solution now 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 05, 2013 Apr 05, 2013

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2013 Apr 05, 2013

Copy link to clipboard

Copied

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;

    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 + ": More than 2 tokens found");

    }

}

resources[resource] = dict;

dispatchEvent(new Event(LOCALE_RESOURCE_LOADED));

Cheers.

valley

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 05, 2013 Apr 05, 2013

Copy link to clipboard

Copied

Thanks for posting, valley!

Looks like we're coding in-sync.

Happy weekend (almost there)!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 06, 2013 Apr 06, 2013

Copy link to clipboard

Copied

Thank you. Same to you.

Do you also live in Switzerland ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 06, 2013 Apr 06, 2013

Copy link to clipboard

Copied

Yep! I'll send you a PM shortly…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 19, 2014 Jan 19, 2014

Copy link to clipboard

Copied

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,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 09, 2015 Feb 09, 2015

Copy link to clipboard

Copied

LATEST

I also have a problem with it. I believe

  <fx:Metadata>

    [ResourceBundle("bundle")]

  </fx:Metadata>

isnt support yet?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines