加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex 国际化(flex Localize)

发布时间:2020-12-15 04:53:52 所属栏目:百科 来源:网络整理
导读:项目中用到flex国际化,虽然以前也用过,也是直接嵌入到程序中,现在有要求要放到外面模块中去,虽然网上教程多多,但是遇到还是遇到很多问题,籍此写下此文,加深印象之用。如果有哪位仁兄只好也用到此,希望能有所帮助,废话至此,正文开始。 先说说编译到

项目中用到flex国际化,虽然以前也用过,也是直接嵌入到程序中,现在有要求要放到外面模块中去,虽然网上教程多多,但是遇到还是遇到很多问题,籍此写下此文,加深印象之用。如果有哪位仁兄只好也用到此,希望能有所帮助,废话至此,正文开始。

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

1.创建资源文件夹

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

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

2.创建资源文件

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

?

## resources.properties file for locale de_DE ## 

# contact-form labels and assets 
contact.title=Contact Form 
contact.flagImg=assets/us.gif 
contact.submit=Submit 

# contact-form fields 
contact.field.name=Name 
contact.field.streetAddress=Street Address 
contact.field.city=City 
contact.field.state=State 
contact.field.zipCode=ZIP Code 
contact.field.country=Country

?

3.创建mxml文件

?

<?xml version="1.0" encoding="utf-8"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
		  xmlns:s="library://ns.adobe.com/flex/spark" 
		  xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">
	<fx:Metadata>
		[ResourceBundle("resources")]
	</fx:Metadata>
	<fx:Script>
		<![CDATA[
			private var locales:Array = [{label:"English (United States)",locale:"en_US"},{label:"中文 (中国)",locale:"zh_CN"},{label:"German (Denmark)",locale:"de_DE"},{label:"French (France)",locale:"fr_FR"},{label:"Japanese (Japan)",locale:"ja_JP"}];
			
			private function comboChangeHandler():void
			{
				resourceManager.localeChain = [localeComboBox.selectedItem.locale];
			}
		]]>
	</fx:Script>
	
	<s:Panel title="{resourceManager.getString('resources','contact.title')}" color="black" borderAlpha="0.15" width="350">
		<s:layout>
			<s:VerticalLayout horizontalAlign="center" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" />
		</s:layout>
		
		<mx:Form width="100%" color="0x323232">
			<mx:FormItem label="{resourceManager.getString('resources','contact.field.name')}">
				<s:TextInput />
			</mx:FormItem>
			<mx:FormItem label="{resourceManager.getString('resources','contact.field.streetAddress')}">
				<s:TextInput />
			</mx:FormItem>
			<mx:FormItem label="{resourceManager.getString('resources','contact.field.city')}">
				<s:TextInput />
			</mx:FormItem>
			<mx:FormItem label="{resourceManager.getString('resources','contact.field.state')}">
				<s:TextInput />
			</mx:FormItem>
			<mx:FormItem label="{resourceManager.getString('resources','contact.field.zipCode')}">
				<s:TextInput />
			</mx:FormItem>
			<mx:FormItem label="{resourceManager.getString('resources','contact.field.country')}">
				<s:TextInput />
			</mx:FormItem>
			<mx:FormItem>
				<s:Button label="{resourceManager.getString('resources','contact.submit')}" />
			</mx:FormItem>
		</mx:Form>
	</s:Panel>
	<mx:Spacer height="15" />
	
	<s:HGroup width="350" verticalAlign="middle">
		<mx:Spacer width="100%" />
		<mx:Image source="{resourceManager.getString('resources','contact.flagImg')}"/>
		<mx:ComboBox id="localeComboBox" dataProvider="{locales}" change="comboChangeHandler()"/>
	</s:HGroup>
</s:VGroup>

?

?4.配置Flex 编译参数

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

?

-locale=en_US,zh_CN -allow-source-path-overlap=true -source-path=Locale/{locale}

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

?

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

?

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

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

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

1.2 和上面一样

3.建立mxml文件

?

<?xml version="1.0" encoding="utf-8"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
		  xmlns:s="library://ns.adobe.com/flex/spark" 
		  xmlns:mx="library://ns.adobe.com/flex/mx" 
		  width="100%" 
		  height="100%" 
		  creationComplete="initApp()">
	<fx:Script>
		<![CDATA[
		import mx.controls.Alert;
		import mx.events.ResourceEvent;
		import mx.resources.ResourceBundle;
		
		[Bindable]
		private var locales:Array = [ "zh_CN","de_DE" ];
		
		private function initApp():void {
			/* Set the index to -1 so that the prompt appears 
			when the application first loads. */
			localeComboBox.selectedIndex = -1;            
		}
		
		private function registrationComplete():void {
			Alert.show(resourceManager.getString('resources','thanks'));
		}  
		
		private function comboChangeHandler():void {
			var newLocale:String = String(localeComboBox.selectedItem);
			
			/* Ensure that you are not loading the same resource module more than once. */
			if (resourceManager.getLocales().indexOf(newLocale) != -1) {
				completeHandler(null);
			} else {
				// Build the file name of the resource module.
				var resourceModuleURL:String = newLocale + "_ResourceModule.swf";
				
				var eventDispatcher:IEventDispatcher = 
					resourceManager.loadResourceModule(resourceModuleURL);
				eventDispatcher.addEventListener(ResourceEvent.COMPLETE,completeHandler);
			}            
		}
		
		private function completeHandler(event:ResourceEvent):void {
			resourceManager.localeChain = [ localeComboBox.selectedItem ];
			
			/* This style is not bound to the resource bundle,so it must be reset when 
			the new locale is selected. */
			var test:String = resourceManager.getString('resources','contact.field.city');
			b1.setStyle("downSkin",resourceManager.getClass("RegistrationForm","flag"));            
		}        
	]]></fx:Script>
	<s:Image source="{resourceManager.getClass('resources','flag')}"/>
	<mx:ComboBox id="localeComboBox" 
				 prompt="Select One..."
				 dataProvider="{locales}"
				 change="comboChangeHandler()"/>
	<mx:Form width="100%" color="0x323232">
		<mx:FormItem label="{resourceManager.getString('resources','contact.field.name')}">
			<s:TextInput />
		</mx:FormItem>
		<mx:FormItem label="{resourceManager.getString('resources','contact.field.streetAddress')}">
			<s:TextInput />
		</mx:FormItem>
		<mx:FormItem label="{resourceManager.getString('resources','contact.field.city')}">
			<s:TextInput />
		</mx:FormItem>
		<mx:FormItem label="{resourceManager.getString('resources','contact.field.state')}">
			<s:TextInput />
		</mx:FormItem>
		<mx:FormItem label="{resourceManager.getString('resources','contact.field.zipCode')}">
			<s:TextInput />
		</mx:FormItem>
		<!--<mx:FormItem label="@Resource(key='contact.field.zipCode',bundle='resources')">
			<s:TextInput />
		</mx:FormItem>-->
		<mx:FormItem>
			<s:Button label="{resourceManager.getString('resources','contact.submit')}" />
		</mx:FormItem>
	</mx:Form>
	
	<s:Button id="b1" 
			  label="{resourceManager.getString('resources','submit_button')}" 
			  click="registrationComplete()"/>
</s:VGroup>
?

4.确定需要哪些资源束

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

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

?

mxmlc -locale= -resource-bundle-list=myresources.txt F:demoDemosrcviewresourceResourceMoudleDemo.mxml

?

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

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

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

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

?

bundles = collections components containers controls core effects layout resources skins styles textLayout 

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

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

?

mxmlc -locale=de_DE
-source-path=F:demoDemosrcLocale{locale} -include-resource-bundles=resources,collections,components,containers,controls,core,effects,layout,skins,styles,textLayout -output de_DE_ResourceModule.swf

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读