準(zhǔn)備:
flex3只支持兩種語(yǔ)言,en_US,ja_JP,而flex4中則支持多國(guó)語(yǔ)言,所以可以將flex4中的%FLEX_HOME%\frameworks\locale\zh_CN拷貝至flex3中。
項(xiàng)目中增加國(guó)際化
一.配置
目錄結(jié)構(gòu):
flex_src
--locale
--zh_CN
message.properties
--en_US
message.properties
message.properties內(nèi)容使用UTF-8編碼.
開(kāi)發(fā)環(huán)境配置:
在Eclipse開(kāi)發(fā)環(huán)境中的Flex Compiler/Additional compiler arguments選項(xiàng)
增加如下參數(shù)
-locale zh_CN -locale en_US -source-path=locale/{locale}
二.使用國(guó)際化
Flex中提供了兩種方法使用本地化文件:
1.使用@Resource
<mx:Label text="@Resource(key='name', bundle='message')"/>
其中 key 表示的是要取資源的 key , bundle 表示的是本地化文件,去掉 .properties 之后的名稱
2.使用 ResourceManager
<mx:Label text="resourceManager.getString("bundleName","key")"/>
注意:如果容器中沒(méi)有resourceManager這個(gè)變量,可以使用ResourceManager.getInstance()代替resourceManager,因?yàn)镽esourceManager是單態(tài)的。
編譯檢查:
使用[ResourceBundle('message')]可以為編譯器提供編譯檢查,實(shí)際不需要指定這個(gè)也是可以的。
<mx:Metadata>
[ResourceBundle('message')]
</mx:Metadata>
三.動(dòng)態(tài)修改當(dāng)前語(yǔ)言
ResourceManager.getInstance().localeChain = ['zh_CN'];
四.減少編譯大小
由于flex是使用編譯器將國(guó)際化信息編譯進(jìn)swf中,所以如果將所有的語(yǔ)言全部編譯進(jìn)swf,會(huì)導(dǎo)致swf文件過(guò)于龐大。而正如國(guó)內(nèi)的環(huán)境,如果你的用戶大部分都是中文用戶,實(shí)在沒(méi)有必要為了少量英文用戶而增加swf文件的大小。
所以最好的效果是 *獨(dú)立編譯*。即編譯出:
main_zh_CN.swf
main_en_US.swf
參考: http://www.nbilyk.com/flex-localization-example
當(dāng)然這樣處理會(huì)麻煩一點(diǎn),請(qǐng)具體參考你編譯出來(lái)的swf大小,相差不大的請(qǐng)也可以忽略此項(xiàng)。
五. ant編譯
<property name="FLEX_HOME" value="${env.FLEX_HOME}"/>
<property name="FLEX_SWC" value="${basedir}/flex_libs"/>
<property name="FLEX_SRC" value="${basedir}/flex_src"/>
<target name="compile-flex">
<echo message="FLEX_HOME:${FLEX_HOME}"/>
<!-- tasks: mxmlc,compc,html-wrapper -->
<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar" />
<!-- 具體編譯參數(shù)請(qǐng)參考:http://www.k-zone.cn/zblog/post/flex-compiler-parameter.html -->
<mxmlc
file="${basedir}/flex_src/${flex.application.name}.mxml"
output="${basedir}/dist/web/flex/${flex.application.name}.swf"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="false"
incremental="true"
compiler.show-actionscript-warnings="true"
compiler.show-binding-warnings="true"
compiler.show-unused-type-selector-warnings="true"
compiler.strict="true">
<!-- Get default compiler options. -->
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<!-- List of path elements that form the roots of ActionScript class hierarchies. -->
<source-path path-element="${FLEX_SRC}"/>
<!-- 需要編譯的locale -->
<locale>zh_CN</locale>
<locale>en_US</locale>
<source-path path-element="${FLEX_SRC}/locale/{locale}"/>
<!-- 消除編譯警告,允許源代碼路徑重疊或是包含現(xiàn)象 -->
<allow-source-path-overlap>true</allow-source-path-overlap>
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="locale/{locale}" />
</compiler.library-path>
<!-- 自定義或第三方包 -->
<compiler.library-path dir="${basedir}" append="true">
<include name="flex_libs" />
</compiler.library-path>
<compiler.debug>false</compiler.debug>
</mxmlc>
</target>