Flash国際化プログラミング
リソース管理

Flashのリソース管理の方法は、大きくわけて、mx.resources.ResourceManagerを使ったproeprtiesファイルの方法と、fl.lang.Localeを使ったXLIFFの方法の2つがあります。

また、MXMLの場合は、ResourceManagerを使って直接ラベルをリソース化することができます。

各方法の詳細は、以下のAdobeの開発ガイドをご参照ください。

1. fl.lang.Localeの詳細

2. ResourceManagerのリソースの使用

*fl.lang.LocaleのXLIFF読み込みは非同期であるため、文字列の読み込みはコールバック関数で行う必要があります。
この仕様はコード中の文字列の切り替えなどに適さないため、wwnaviの文字列の外部化では、ResourceManagerを使った処理を使用しています。

*ResourceManagerに関係するクラスやメソッドの多くが現在非推称となっています。以下のコードは現在サポートされていません。
var r:SystemManager = new SystemManager();
var t:Locale = (r.topLevelSystemManager) ? Locale.getCurrent(r.topLevelSystemManager) : Locale.getCurrent(r);
参考)wwnaviによるリソース切り替えのサンプルコード

フラッシュを読み込むHTMLからユーザーロケールを取得して、それに合ったリソースを表示する処理です。
====== WwnaviRs.as (リソースの読み込みクラス) ======
 //$NLS-WWNAVI 2011-06-13T20:24:27+0900
package 
{
	
	import flash.system.Capabilities;
	import flash.external.ExternalInterface;
	import mx.resources.ResourceManager;
	
	/**
	 * This class is automatically generated by wwnavi.
	 */
	
	[ResourceBundle("wwnaviRs")]
	public class WwnaviRs
	{
		private static var isInit:Boolean = false;
		
		private static var  loc:String = null;
		
		public static function init():void
		{
			
			// Get user system language code (without country codes in most cases).
			
			loc = Capabilities.language;
			loc = loc.replace("-", "_");
			
			var cn:String = "";
			
			// For using this code,  the additional compile option, 
			// '-locale=YOUR_LOCALE(e.g. en_US) -source-path=YOUR_RESOURCE_PATH(e.g. locale)/{locale}' is required.
			// If you add other locales, you have to add them to the option above.
			
			switch (loc) {
			case "en":	
			    cn = "US";
			    break;
			case "ja":	
			    cn = "JP";
			    break;
			case "ko":	
			    cn = "KR";
			    break;
			case "zh":	
			    // do nothing. (loc has "_CN or _TW")
			    break;
			// add your locale country codes.
			//case...
			//    break;
			default:			    
			}
			
			if (loc.indexOf("_") == -1) {
				loc = loc + "_" + cn;
			}
			
			// Or get user browser language by your external JavaScript.
			// loc = getBrowserLang();
			
			// In some cases, this code doesn't work.
			ResourceManager.getInstance().localeChain = [loc];
			
			isInit = true;
		}
		
		ロケールをHTMLの引数として受け取ります。
		public static function initByFlashVars(flashVars:Object):void
		{
			loc = flashVars["locale"];
			isInit = true;
		}
		
		public static function getString(id:String):String 
		{
			if (!isInit) init();
			// ResourceManager.getInstance().localeChain = [loc]
			// でロケールが切り替わらない場合があるので安全策として以下の方法にしています。
			var str:String = ResourceManager.getInstance().getString("wwnaviRs", id, null, loc);
			if (str == null) str = ResourceManager.getInstance().getString("wwnaviRs", id);
			return str;
		}
		
	    /*public function getBrowserLang():String {
            var s:String;
            if (ExternalInterface.available) {
               var wrapperFunction:String = "YOUR_JAVASCRIPT_FUNCTION";
               s = ExternalInterface.call(wrapperFunction);
            } else {
               s = "Wrapper not available";
            }
           return s;
        }*/
		
	}
	
}

====== test.html (Flashの読み込み) ======
<html>
<object width="100%" height="100%" >
<!-- Edit locale to the suitable one. -->
<param name="FlashVars" value="locale=ja_JP" />
<!-- Edit locale to the suitable one. -->
<embed  width="100%" height="100%" src="Main.swf" FlashVars="locale=ja_JP" />
</object>
</html>


====== Main.as (HTMLからのロケール取得) ======
Main関数などのエントリーポイントに記述します。
public function Main():void
{     WwnaviRs.initByFlashVars(this.root.loaderInfo.parameters);


====== Main.as (文字列の読み込み処理) ======
...
tx1.text = WwnaviRs.getString("Main.1"); // tx1.text = "This is text 1."; 
...


====== リソースファイルの場所 ======
../locale/en_US/wwnaviRs.properties
          ja_JP/wwnaviRs.properties
           ...
../src/Main.as
        WwnaviRs.as
        
====== リソースファイルの中身 ======
Main.3=This is text 3.
Main.2=This is text 2.
Main.1=This is text 1.
...
Main.3=これはテキスト3です。
Main.2=これはテキスト2です。
Main.1=これはテキスト1です。

====== コンパイルオプション ======
-locale=en_US,ja_JP -source-path=locale/{locale}
 

国際化プログラミングトップへ


Copyright (C) 2012 Kokusaika JP, Inc.
本ドキュメントを無断で転載および引用することを禁じます。