Posted on

ResourceManager動態載入多國語系的實現

事前準備工作
1.[撰寫Ant編譯時所使用的XML檔]
依照原有的XML去設定自己的SDK的位置,並且寫入要載入的語言檔

<?xml version="1.0" encoding="utf-8"?>
    <project name="Example resource bundle builder" basedir="." default="main">
    <!--這邊應設定為自己電腦的flex sdk的位置-->
    <property name="FLEX_HOME" value="C:\Program Files\Adobe\Adobe Flash Builder 4.5\sdks.5.0" />
    <property name="APP_ROOT" value="${basedir}"/>
    <!--這邊需要載入ant編譯as檔案所需的flexTasks.jar以及所有相關編譯所需的函式庫-->
    <taskdef resource="flexTasks.tasks" >
        <classpath> <pathelement path="${FLEX_HOME}/ant/lib/flexTasks.jar"/>
            <pathelement path="${FLEX_HOME}/lib/flexTasks.jar"/>
            <fileset dir="${FLEX_HOME}/lib">
                <include name="**/*.jar"/>
            </fileset>
        </classpath>
    </taskdef>
    <!--定義所有的語言-->
    <target name="main">
        <antcall target="en_US"></antcall>
        <antcall target="zh_TW"></antcall>
    </target>
    <target name="en_US">
        <mxmlc>
            <locale>en_US</locale>
            <source-path>locale/{locale}</source-path>
            <include-resource-bundles>test</include-resource-bundles>
            <output>src/Resources_en_US.swf</output>
        </mxmlc>
    </target>
    <target name="zh_TW">
        <mxmlc keep-generated-actionscript="true">
            <allow-source-path-overlap>true</allow-source-path-overlap>
            <locale>zh_TW</locale>
            <source-path>locale/{locale}</source-path>
            <!--載入相關的函式庫-->
            <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
                <include name="libs" />
                <include name="locale/{locale}" />
            </compiler.library-path>
            <!--定義要載入的語言檔案(可以用很多檔案)-->
            <include-resource-bundles>test</include-resource-bundles>
            <!--<include-resource-bundles>other</include-resource-bundles>-->
            <output>src/Resources_zh_TW.swf</output>
        </mxmlc>
    </target>
</project>

2.[設定Ant環境]
(a)Help->Software Updates更新flash builder的Ant編輯程式
(b)Windows->Show View->Other->Ant->Ant把視窗叫出來
(c)按Ant編譯視窗最左邊的Add Buildfiles,選擇剛剛寫的xml
(d)RUN他

3.[設定要產出的flex專案檔的編譯locale參數]
如果是要中文+英文則為-locale en_US zh_TW,如果少了這個動作在變語言時會發生缺少核心語言CORE檔案的問題。

4. locale檔副檔名是.properties

====================================
1. flex版本的實現及範例下載
http://www.nbilyk.com/flex-localization-example

2. 為flash builder安裝ant
http://www.judahfrangipane.com/blog/2007/12/13/flex-builder-3-ant-support/

3. 官方關於ResourceManager的類別資料
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/resources/ResourceManager.html

4. 在flash裡實現多國語系
http://ticore.blogspot.com/2010/07/as3-project-multi-languages.html

5. 各種實現方式
http://blog.csdn.net/fireson/archive/2010/01/12/5181343.aspx

[學習經歷]
首先先至[flex版本的實現及範例下載]下載多國語系範例
然後到[為flash builder安裝ant]實際編譯ResourceManager的語言檔為swf
再依範例自行試看看如何實現