Avg. Rating 4.0

Problem

Obtain currently set locale string (in en_US format).

Solution

Compile swf with all available in sdk locales and use ResourceManager.getInstance().localeChain[0] to access current prefered system locale.

Detailed explanation

 

Common developer mistake in projects where for some reasons full locale string (en_US for example) needed is to complile output swf file without potentially used locale. In this case player "can't recognize" client locale right. So, recipe first step is to add all available in selected sdk ([bulder_installation_folder]\sdks\[number]\frameworks\locale\...) locales into project > properties > Flex Compiler > additional compiler arguments (see attached locales.jpg ):

 

 To access locale string use ResourceManager.getInstance().localeChain[0] . Example application looks like

 

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" creationComplete="init();">

<fx:Script>

      <![CDATA[

      import mx.utils.object_proxy;

      import mx.utils.ObjectProxy;

      import mx.utils.ObjectUtil;

     

      import mx.managers.SystemManager;

      import mx.resources.Locale;

      import mx.resources.ResourceManager;

     

      function init():void{

           

            var r:SystemManager = new SystemManager();

                       

            var t:Locale = (r.topLevelSystemManager) ? Locale.getCurrent(r.topLevelSystemManager) : Locale.getCurrent(r);

            ta.text = new String(' All embed locales: '+Array(ResourceManager.getInstance().localeChain).join(' ; '));

           

            if (t == null) t = new Locale(ResourceManager.getInstance().localeChain[0]);

           

            ta.text += new String('\r\r Current locale: '+t.toString()+" \r\r Capabilities options: language "+Capabilities.language+ " / version "+Capabilities.version+ "\r os "+Capabilities.os+"\r serverString "+Capabilities.serverString);

     

      }

           

      ]]>

</fx:Script>

      <mx:TextArea id="ta" width="600" height="200" verticalCenter="0" horizontalCenter="0" text="wait..." />

</s:Application>

 and attached to this recipe as project.zip (Gumbo project).

locales.jpg
project.zip
[sample project]
Report abuse

Related recipes