Raghavendra Nilekani
Raghavendra Nilekani

Reputation: 406

Adobe Flex code to display multiple language in the same page

I am writing an Adobe flex (version 3.5) based application in which one of the pages should be display a combo box where it should contain different locale names in the respective languages.

For example, the combo box labels should be English (United States), 日本語 and so on.

Following is my sample code,

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">

<mx:Metadata>
    [ResourceBundle("languages")]
</mx:Metadata>

<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.resources.ResourceManager;

        [Bindable]
        public var items:ArrayCollection = new ArrayCollection(
            [   
                {label:ResourceManager.getInstance().getString('languages','label.english'), locale:"en_US"},
                {label:ResourceManager.getInstance().getString('languages','label.japanese'), locale:"ja_JP"},
            ]                
        );
    ]]>
</mx:Script>
<mx:ComboBox dataProvider="{items}" labelField="label"/>
</mx:Application>

With the above code, the combobox displays both the labels either in english or in Japanese language.

I tried ResourceManager.getInstance().localeChain api. But it modifies all the resource strings.

But I m looking at code which displays English (United States) in english language and Japanese (Japan) in Japanese language.

Any suggestion ?

Upvotes: 0

Views: 150

Answers (2)

Rahul Singhai
Rahul Singhai

Reputation: 1339

The problem is that you are trying to create the locale array collection 'items' even before application is created completely. Try adding the logic inside creationComplete method as shown below:

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    width="100%" height="100%"
    creationComplete="onCC()"
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Metadata>
        [ResourceBundle("languages")]
    </fx:Metadata>

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.resources.ResourceManager;

            [Bindable]
            private var acLocales:ArrayCollection;

            private function ddlChangeHandler():void
            {
                resourceManager.localeChain = [ ddlLocale.selectedItem.locale ];
            }

            private function onCC():void
            {
                acLocales = new ArrayCollection();

                var objLocale:Object = new Object();
                objLocale.label = ResourceManager.getInstance().getString('languages', 'label.english', null, "en_US");
                objLocale.locale = "en_US";
                acLocales.addItem(objLocale);

                objLocale = new Object();
                objLocale.label = ResourceManager.getInstance().getString('languages', 'label.japanese', null, "ja_JP");
                objLocale.locale = "ja_JP";
                acLocales.addItem(objLocale);

                ddlLocale.selectedIndex = 0;
            }
        ]]>
    </fx:Script>

    <s:HGroup 
        width="100%"
        horizontalAlign="center"
        verticalAlign="middle">
        <s:DropDownList id="ddlLocale"
            dataProvider="{acLocales}"
            labelField="label"
            change="ddlChangeHandler()"/>
    </s:HGroup>
</s:Application>

Upvotes: 1

Crabar
Crabar

Reputation: 1857

You can write strings in your "languages" file in any language. As I understand user may choose language of application with this combobox. So, I would just create a only one "languages" file for all supported languages. For example:

languages.properties:

label.english=English
label.french=Français

Upvotes: 0

Related Questions