Flex2 国际化(internationalization)

来源:互联网 发布:chart.js x轴太挤 编辑:程序博客网 时间:2024/05/21 19:38

现在做的这个Flex项目需要进行国际化,这里总结一下

和java一样,Flex2也可以使用.properties文件实现国际化,使用flex的mx.resources.ResourceBundle类来读取properties文件。同样也可以在Flex Builder2中安装Properties Editor插件来写properties文件。

1.创建一个Flex项目
2.在这个项目中创建 locale 文件夹
3.在 locale 文件夹中添加2个properties文件,如下

en_US.properties

  1. label1=Hello World!   
  2. label2=Welcome!  

zh_CN.properties

  1. label1=/u5927/u5bb6/u597d/uff01   
  2. label2=/u6b22/u8fce/uff01  

4.将 locale 文件夹添加为flex项目的source path (两个方法)
a)右击项目 -> properties -> Flex Compiler -> 在additional compiler arguments中添加 -sp locale
b)右击项目 -> properties -> Flex Build Path -> Source path -> add Folder -> 选中locale文件夹即可

现在开始写代码了

 

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"));  
            }  
        }  
   }  

 

主程序zhcn.mxml代码

 

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

 <mx:Script>
  <![CDATA[
   import util.Localizator;
   
   [Bindable]
   private var localizator : Localizator = Localizator.getInstance();
   
   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>

OK,编译运行就可以看到结果了。

另外有些问题需要说明
1.properties文件须使用UTF-8编码

2.(Flex supports static inclusion of localized resources, but not dynamic retrieval of resources at run time.) Flex2是将properties文件编译到swf文件中的,目前还不支持动态读取外部properties文件

3.如果保存或编译代码时,遇到Unable to resolve a class for ResourceBundle: en_US_properties问题,大可放心,这是Flex Builder2的bug,只需要在main menu(顶部菜单)中选 Project -> Clean... 就可以了。或者将上面的Localizator.as随便空一行再保存编译一下,这个error也会消失,呵呵。

参考
Flex 2 Developer's Guide -> Localizing Flex Applications

http://flexme.wordpress.com/2007/07/11/internationalization-in-flex/
http://www.deitte.com/archives/2006/10/using_resource.htm
http://www.zhuoqun.net/article.asp?id=267

原创粉丝点击