Flex 国际化

Flex 国际化(demo)

 

在项目中新建一个locale的目录(src目录下);

在locale目录下创建需要支持语言的子目录,如locale/zh_CN, locale/en_US;

在这些子目录下新建properties文件(en_US.properties,zh_CN.properties),各目录下的属性文件需要 同名。属性文件的格式与java的属性文件相同,每行以"key=value"的形式列出键值对,以utf-8编码保存文件;

编译mxml文件时加上 -locale=en_US,zh_CN -source-path=/locale/{locale}的选项;



在flex的sdk下加入中文库(C:\Program Files\Adobe\Flex Builder 3\sdks\3.2.0\frameworks\locale)zh_CN



Localizator.as文件内容如下:



package util { 

     

    import flash.events.EventDispatcher; 

    import flash.events.IEventDispatcher; 

   import flash.events.Event; 

    import mx.resources.ResourceBundle; 

     

    public class Localizator extends EventDispatcher { 

         

        //采用单例模式 

        private static var _instance : Localizator; 

         

        private var _language : String; 

         

        //这里的resource名应与.properties文件名相同 

       [ResourceBundle("en_US")] 

        private var lang_en_US:ResourceBundle; 

        

        [ResourceBundle("zh_CN")] 

        private var lang_zh_CN:ResourceBundle;          

        [Bindable] 

        private var currRes:ResourceBundle; 

         

        public function Localizator(language : String = "en_US") { 

            selectLanguage(language); 

        } 

         

       public static function getInstance(language : String = "en_US"):Localizator { 

            if (_instance == null) { 

                _instance = new Localizator(language); 

            } 

            return _instance;       

             } 

        private function selectLanguage(language : String):void { 

            this._language = language; 

             

            if (_language == "en_US") { 

                this.currRes = lang_en_US; 

            } else if (_language == "zh_CN") { 

                this.currRes = lang_zh_CN; 

           } else { 

               this.currRes = lang_en_US; 

            } 

        } 

         

        [Bindable(event="languageChange")] 

        public function getText(key:String):String { 

            return this.currRes.getString(key); 

       } 

         

        public function get language():String { 

            return this._language; 

       } 

           public function set language(language : String):void { 

            if (this._language != language) { 

                selectLanguage(language); 

                dispatchEvent(new Event("languageChange")); 

            } 

        } 

   } 

}



 



internation.mxml文件如下:



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

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preinitialize="init()">



 <mx:Script>

  <![CDATA[

   import util.Localizator;

   import mx.controls.Alert;

   [Bindable]

   private var localizator : Localizator = Localizator.getInstance();

  

  private function init():void {

   //初始化时设置的语言是用户系统的语言

    var crrlanguage:String =  Capabilities.language.toString();//获去用户系统语言

    if(crrlanguage == 'zh-CN' ){

     localizator.language = 'zh_CN';

    }else if(crrlanguage == 'en-US'){

     localizator.language = 'en_US';

    }else{

     Alert.show(crrlanguage+"其他语言");

    }

    

    }

  

   private function changeLanguage(language:String):void {

    localizator.language = language;

   }

  

  ]]>

 </mx:Script>



 <mx:Label id="label1" x="10" y="10" text="{localizator.getText('label1')}" width="152" height="20" fontSize="12" fontWeight="bold"/>

 <mx:Label id="label2" x="10" y="38" text="{localizator.getText('label2')}" width="144" height="22" fontSize="12" fontWeight="bold"/>

 

 <mx:Button x="10" y="68" label="Chinese" click="changeLanguage('zh_CN')"/>

 <mx:Button x="88" y="68" label="English" click="changeLanguage('en_US')"/>

 

  

</mx:Application>



用到的文件在demo.rar里
posted on 2010-05-13 10:22  念时  阅读(944)  评论(0编辑  收藏  举报

细节决定成败!态度决定一切!