Flex 国际化(flex Localize)

先说编译到主程序中去的方法:

1.创建资源文件夹

譬如可以在src文件夹下创建Locale文件夹,然后在此文件夹再次创建每个地区的资源文件夹,譬如de_DE,zh_CN.

然后分别创建后缀名为.properties的资源文件,分别放到各个地区的文件夹中。资源文件是可以包括任何事情,从字符串,数字,格式化和图片到样式。每个地区可以生成一个单独的文件。参考:CreateLocaleFiles.png

2.创建资源文件

创建了名为resources.properties的资源文件,内容为:

 Properties代码

 1 ## resources.properties file for locale de_DE ##   
 2   
 3 # contact-form labels and assets   
 4 contact.title=Contact Form   
 5 contact.flagImg=assets/us.gif   
 6 contact.submit=Submit   
 7   
 8 # contact-form fields   
 9 contact.field.name=Name   
10 contact.field.streetAddress=Street Address   
11 contact.field.city=City   
12 contact.field.state=State   
13 contact.field.zipCode=ZIP Code   
14 contact.field.country=Country  

3.创建mxml文件

 Flex代码

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"   
 3           xmlns:s="library://ns.adobe.com/flex/spark"   
 4           xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">  
 5     <fx:Metadata>  
 6         [ResourceBundle("resources")]  
 7     </fx:Metadata>  
 8     <fx:Script>  
 9         <![CDATA[  
10             private var locales:Array = [{label:"English (United States)", locale:"en_US"},  
11                 {label:"中文 (中国)", locale:"zh_CN"},  
12                 {label:"German (Denmark)", locale:"de_DE"},  
13                 {label:"French (France)", locale:"fr_FR"},  
14                 {label:"Japanese (Japan)", locale:"ja_JP"}];  
15               
16             private function comboChangeHandler():void  
17             {  
18                 resourceManager.localeChain = [localeComboBox.selectedItem.locale];  
19             }  
20         ]]>  
21     </fx:Script>  
22       
23     <s:Panel title="{resourceManager.getString('resources','contact.title')}" color="black" borderAlpha="0.15" width="350">  
24         <s:layout>  
25             <s:VerticalLayout horizontalAlign="center" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" />  
26         </s:layout>  
27           
28         <mx:Form width="100%" color="0x323232">  
29             <mx:FormItem label="{resourceManager.getString('resources','contact.field.name')}">  
30                 <s:TextInput />  
31             </mx:FormItem>  
32             <mx:FormItem label="{resourceManager.getString('resources','contact.field.streetAddress')}">  
33                 <s:TextInput />  
34             </mx:FormItem>  
35             <mx:FormItem label="{resourceManager.getString('resources','contact.field.city')}">  
36                 <s:TextInput />  
37             </mx:FormItem>  
38             <mx:FormItem label="{resourceManager.getString('resources','contact.field.state')}">  
39                 <s:TextInput />  
40             </mx:FormItem>  
41             <mx:FormItem label="{resourceManager.getString('resources','contact.field.zipCode')}">  
42                 <s:TextInput />  
43             </mx:FormItem>  
44             <mx:FormItem label="{resourceManager.getString('resources','contact.field.country')}">  
45                 <s:TextInput />  
46             </mx:FormItem>  
47             <mx:FormItem>  
48                 <s:Button label="{resourceManager.getString('resources','contact.submit')}" />  
49             </mx:FormItem>  
50         </mx:Form>  
51     </s:Panel>  
52     <mx:Spacer height="15" />  
53       
54     <s:HGroup width="350" verticalAlign="middle">  
55         <mx:Spacer width="100%" />  
56         <mx:Image source="{resourceManager.getString('resources','contact.flagImg')}"/>  
57         <mx:ComboBox id="localeComboBox" dataProvider="{locales}" change="comboChangeHandler()"/>  
58     </s:HGroup>  
59 </s:VGroup>  

 4.配置Flex 编译参数

选中项目名称右键-Properties-Flex Complier-Addtional compiler arguments中添加

Configure代码  收藏代码
  1. -locale=en_US,zh_CN -allow-source-path-overlap=true -source-path=Locale/{locale}  

 这样就可以了,你可以通过切换发现可以实现国际化了,哈哈,不过只有两种地区的简单demo。

再谈谈用module分别加载不同地区资源

到底怎样加载这些资源文件决定于你的程序许多支持多少地区本地化。

a.如果你仅仅只是支持一到两个地区的本地化,那么一般来说就是直接编译到程序中。

b.如果要支持很多种本地化,一般选择在运行时加载所需的本地化资源。资源模块和编译入主程序相比,资源模块是一种比较好的本地化方式,因为它指定的资源模块可以在运行时加载。这样可以减小主程序的文件大小,但是如你所见你必须为每个资源模块加载一个单独的swf文件。这样的话会增加网络请求并且合成的主程序的大小比直接编译入主程序的方式更大。尽管如此,如果你有很多本地化的需求,分别单独的加载它们从长远来看会节省资源。

1.2 和上面一样

3.建立mxml文件

 Mxml代码

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"   
 3           xmlns:s="library://ns.adobe.com/flex/spark"   
 4           xmlns:mx="library://ns.adobe.com/flex/mx"   
 5           width="100%"   
 6           height="100%"   
 7           creationComplete="initApp()">  
 8     <fx:Script>  
 9         <![CDATA[  
10         import mx.controls.Alert;  
11         import mx.events.ResourceEvent;  
12         import mx.resources.ResourceBundle;  
13           
14         [Bindable]  
15         private var locales:Array = [ "zh_CN","de_DE" ];  
16           
17         private function initApp():void {  
18             /* Set the index to -1 so that the prompt appears   
19             when the application first loads. */  
20             localeComboBox.selectedIndex = -1;              
21         }  
22           
23         private function registrationComplete():void {  
24             Alert.show(resourceManager.getString('resources', 'thanks'));  
25         }    
26           
27         private function comboChangeHandler():void {  
28             var newLocale:String = String(localeComboBox.selectedItem);  
29               
30             /* Ensure that you are not loading the same resource module more than once. */  
31             if (resourceManager.getLocales().indexOf(newLocale) != -1) {  
32                 completeHandler(null);  
33             } else {  
34                 // Build the file name of the resource module.  
35                 var resourceModuleURL:String = newLocale + "_ResourceModule.swf";  
36                   
37                 var eventDispatcher:IEventDispatcher =   
38                     resourceManager.loadResourceModule(resourceModuleURL);  
39                 eventDispatcher.addEventListener(ResourceEvent.COMPLETE, completeHandler);  
40             }              
41         }  
42           
43         private function completeHandler(event:ResourceEvent):void {  
44             resourceManager.localeChain = [ localeComboBox.selectedItem ];  
45               
46             /* This style is not bound to the resource bundle, so it must be reset when   
47             the new locale is selected. */  
48             var test:String = resourceManager.getString('resources','contact.field.city');  
49             b1.setStyle("downSkin", resourceManager.getClass("RegistrationForm", "flag"));              
50         }          
51     ]]></fx:Script>  
52     <s:Image source="{resourceManager.getClass('resources', 'flag')}"/>  
53     <mx:ComboBox id="localeComboBox"   
54                  prompt="Select One..."  
55                  dataProvider="{locales}"  
56                  change="comboChangeHandler()"/>  
57     <mx:Form width="100%" color="0x323232">  
58         <mx:FormItem label="{resourceManager.getString('resources','contact.field.name')}">  
59             <s:TextInput />  
60         </mx:FormItem>  
61         <mx:FormItem label="{resourceManager.getString('resources','contact.field.streetAddress')}">  
62             <s:TextInput />  
63         </mx:FormItem>  
64         <mx:FormItem label="{resourceManager.getString('resources','contact.field.city')}">  
65             <s:TextInput />  
66         </mx:FormItem>  
67         <mx:FormItem label="{resourceManager.getString('resources','contact.field.state')}">  
68             <s:TextInput />  
69         </mx:FormItem>  
70         <mx:FormItem label="{resourceManager.getString('resources','contact.field.zipCode')}">  
71             <s:TextInput />  
72         </mx:FormItem>  
73         <!--<mx:FormItem label="@Resource(key='contact.field.zipCode', bundle='resources')">  
74             <s:TextInput />  
75         </mx:FormItem>-->  
76         <mx:FormItem>  
77             <s:Button label="{resourceManager.getString('resources','contact.submit')}" />  
78         </mx:FormItem>  
79     </mx:Form>  
80       
81     <s:Button id="b1"   
82               label="{resourceManager.getString('resources','submit_button')}"   
83               click="registrationComplete()"/>  
84 </s:VGroup>  

 

4.确定需要哪些资源束

在你编译资源模块之前,你必须知道那些资源束应该放到它里面。换句话说,你必须要知道那些资源是你的主程序-包括它的框架-真正需要的。这不仅仅包括你所创建程序需要的的资源,而且也包括程序框架需要的资源

在你的flex bulider中找到mxmlc.exe 文件,例如我的在:C:Program FilesAdobeAdobe Flash Builder 4.6sdks4.6.0in,执行

Mxmlc命令代码 :
 
mxmlc -locale= -resource-bundle-list=myresources.txt F:demoDemosrcview esourceResourceMoudleDemo.mxml  

当你使用resource-bundle-list选项时,你还必须设置locale的值为空。

myresource.txt 就是待会要把你所有要国际化的页面和框架所需的所有文件名输出到这个文件中,地址就在mxmlc.exe文件的同一目录

F:demoDemosrcview esourceResourceMoudleDemo.mxml 就是你要国际化的页面的绝对路径

执行完后到myresource.txt中查看,内容为:

其中的resources就是自定义的资源文件,其他就是框架所用的文件了

5.把所有的文件都编译到一个swf中

命令行代码  
  1. mxmlc -locale=de_DE  
  2. -source-path=F:demoDemosrcLocale{locale} -include-resource-bundles=resources,collections,components,containers,controls,core,effects,layout,skins,styles,  
  3. textLayout -output de_DE_ResourceModule.swf  

 这个时候就会在mxmlc.exe中相同的目录中生成de_DE_ResourceModule.swf。

然后你再改变下-locale=zh_CN,生成zh_CN_ResourceModule.swf。

然后把这两个swf放到主程序相同目录下,测试加载即可。

原文地址:https://www.cnblogs.com/wahsonleung/p/3270476.html