Small Gotcha With As3localelib

I just started messing around with the as3localelib library in a project I'm working on which needs to be localized to several languages.  I found a few code samples around the web and both use the following two lines of code to implement the library:

var sortedLocales:Array = LocaleUtil.sortLanguagesByPreference(['en_US', 'zh_CN', 'zh_TW'], flash.system.Capabilities.languages, "en_US");
ResourceManager.getInstance().localeChain = sortedLocales;

In AIR, this produced the desired result.  However, running this code in a normal Flex app will bomb out.  One quick look at the ActionScript 3 documentation reveals the issue:  The languages property is available only in AIR.  The language property is available in Flex and AIR.  I ended up modifying my code to look like this:

if(Capabilities.playerType == "Desktop")
{
var sortedLocales:Array = LocaleUtil.sortLanguagesByPreference(['en_US', 'zh_CN', 'zh_TW'], flash.system.Capabilities.languages, "en_US");
ResourceManager.getInstance().localeChain = sortedLocales;
}
else
{
sortedLocales = LocaleUtil.sortLanguagesByPreference(['en_US', 'zh_CN', 'zh_TW'], [flash.system.Capabilities.language], "en_US");
ResourceManager.getInstance().localeChain = sortedLocales;
}

Now, it works as expected. Damn those AIR only properties ;)

, , ,

%d bloggers like this: