In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

【LocalizationManager】多言語化(ローカライズ)

Get Adobe Flash player
by wetcradle 29 May 2010
/**
 * Copyright wetcradle ( http://wonderfl.net/user/wetcradle )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9YVn
 */

package {
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	public class LocalizationManagerTest extends MovieClip {
		
		//[Embed(source = "ja.properties", mimeType="application/octet-stream")]
		private var jaString:String = "hello = ハロー\n";
		//[Embed(source = "en.properties", mimeType="application/octet-stream")]
		private var enString:String = "hello = Hello\nworld = World\n";
		
		private var localizationManager:LocalizationManager;
		
		////////////////////////////////////////////////////////////
		//////////////コンストラクタ
		////////////////////////////////////////////////////////////
		public function LocalizationManagerTest():void {
			super();
			
			var textField:TextField = new TextField();
			addChild(textField);
			
			//外部ファイルの読み込み
			var jaProperties:Properties = new Properties(jaString);
			var enProperties:Properties = new Properties(enString);
			
			//localizationManagerの初期化
			localizationManager = new LocalizationManager();
			localizationManager.chain = [
			new LocalizationItem(Locale.JAPANESE, jaProperties.getItems()),
			new LocalizationItem(Locale.ENGLISH, enProperties.getItems())
			];
			
			//出力
			//ja.propertiesに定義されている"ハロー"が出力される
			textField.appendText(localizationManager.getItem("hello")+"\n");
			//ja.propertiesに定義されておらず、en.propertiesで定義されている"World"が出力される
			textField.appendText(localizationManager.getItem("world")+"\n");
			//どちらにも定義されていないのでnullとなる
			textField.appendText(localizationManager.getItem("!")+"\n");
		}
		
	}
}

//以下は自分のライブラリにあるクラスを内部クラスとしてコピペしたもの

class Properties {
	
	protected var hash:Object;
	
	////////////////////////////////////////////////////////////
	//////////////コンストラクタ
	////////////////////////////////////////////////////////////
	
	public function Properties(value:String=null):void {
		super();
		hash = {};
		if (value != null) {
			load(value);
		}
	}
	
	////////////////////////////////////////////////////////////
	//////////////パブリックメソッド
	////////////////////////////////////////////////////////////
	
	//______________ load ______________//
	/**
	*/
	public function load(value:String):void {
		var lines:Array = value.split(/[\r\n]+/);
		for each (var line:String in lines) {
			var index:int = line.indexOf("=");
			if (index == -1) {
				continue;
			}
			var key:String = StringTools.trim(line.substring(0, index));
			if (!key) {
				continue;
			}
			hash[key] = StringTools.trim(line.substring(index + 1));
		}
	}
	
	//______________ getItem ______________//
	/**
	*/
	public function getItem(key:String):Object {
		return hash[key];
	}
	
	//______________ addItem ______________//
	/**
	*/
	public function addItem(value:Object, key:String):void {
		hash[key] = value;
	}
	
	//______________ getItems ______________//
	/**
	*/
	public function getItems():Object {
		return HashTools.cloneHash(hash);
	}
	
}

class Locale {
	
	public static const JAPANESE:String = "ja";
	public static const ENGLISH:String = "en";
	
}

class LocalizationItem {
	
	protected var _locale:String;
	
	protected var hash:Object;
	
	////////////////////////////////////////////////////////////
	//////////////コンストラクタ
	////////////////////////////////////////////////////////////
	
	//______________ LocalizationItem ______________//
	/**
	*/
	public function LocalizationItem(locale:String=null, items:Object=null):void {
		super();
		hash = {};
		_locale = locale;
		if (items) {
			addItems(items);
		}
	}
	
	////////////////////////////////////////////////////////////
	//////////////プロパティ
	////////////////////////////////////////////////////////////
	
	//______________ locale ______________//
	public function get locale():String {
		return _locale;
	}
	public function set locale(value:String):void {
		_locale = value;
	}
	
	////////////////////////////////////////////////////////////
	//////////////パブリックメソッド
	////////////////////////////////////////////////////////////
	
	//______________ getItem ______________//
	public function getItem(key:String):Object {
		return hash[key];
	}
	
	//______________ addItem ______________//
	public function addItem(value:Object, key:String):void {
		hash[key] = value;
	}
	
	//______________ addItems ______________//
	public function addItems(items:Object):void {
		for (var key:String in items) {
			hash[key] = items[key];
		}
	}
	
	////////////////////////////////////////////////////////////
	//////////////メソッド
	////////////////////////////////////////////////////////////
	
	////////////////////////////////////////////////////////////
	//////////////イベントハンドラ
	////////////////////////////////////////////////////////////
	
}

class LocalizationManager {
	
	protected var _chain:Array;
	
	////////////////////////////////////////////////////////////
	//////////////コンストラクタ
	////////////////////////////////////////////////////////////
	
	//______________ LocalizationManager ______________//
	public function LocalizationManager():void {
		super();
		_chain = [];
	}
	
	////////////////////////////////////////////////////////////
	//////////////プロパティ
	////////////////////////////////////////////////////////////
	
	//______________ chain ______________//
	/**
	LocalizationItem オブジェクトの配列です。先の要素が優先されます。
	*/
	public function get chain():Array {
		return _chain;
	}
	public function set chain(value:Array):void {
		_chain = value;
	}
	
	////////////////////////////////////////////////////////////
	//////////////パブリックメソッド
	////////////////////////////////////////////////////////////
	
	//______________ addLocalizationItem ______________//
	public function addLocalizationItem(item:LocalizationItem):void {
		_chain.push(item);
	}
	
	//______________ addLocalizationItemAt ______________//
	public function addLocalizationItemAt(item:LocalizationItem, index:int):void {
		_chain.splice(index, 0, item);
	}
	
	//______________ getLocalizationItem ______________//
	public function getLocalizationItem(locale:String):LocalizationItem {
		for each (var localizationItem:LocalizationItem in _chain) {
			if (localizationItem.locale == locale) {
				return localizationItem;
			}
		}
		return null;
	}
	
	//______________ getLocalizationItemAt ______________//
	public function getLocalizationItemAt(index:int):LocalizationItem {
		return _chain[index];
	}
	
	//______________ getItem ______________//
	public function getItem(key:String):Object {
		var item:Object;
		for each (var localizationItem:LocalizationItem in _chain) {
			if ((item = localizationItem.getItem(key)) != null) {
				return item;
			}
		}
		return null;
	}
	
	////////////////////////////////////////////////////////////
	//////////////メソッド
	////////////////////////////////////////////////////////////
	
	////////////////////////////////////////////////////////////
	//////////////イベントハンドラ
	////////////////////////////////////////////////////////////
	
}

class HashTools {
	
	//______________ length ______________//
	/**
	*/
	public static function objectLength(object:Object):int {
		var counter:int = 0;
		for (var key:Object in object) {
			counter++;
		}
		return counter;
	}
	
	//______________ keyOf ______________//
	/**
	*/
	public static function keyOf(object:Object, item:Object, strict:Boolean=false):Object {
		for (var key:Object in object) {
			if (strict) {
				if (object[key] === item) {
					return key;
				}
			}
			else {
				if (object[key] == item) {
					return key;
				}
			}
		}
		return null;
	}
	
	//______________ getKeys ______________//
	/**
	hashがnullの場合はnullを返します。
	*/
	public static function getKeys(hash:Object):Array {
		if (hash === null) {
			return null;
		}
		var keys:Array = [];
		for (var key:Object in hash) {
			keys.push(key);
		}
		return keys;
	}
	
	//______________ getValues ______________//
	/**
	hashがnullの場合はnullを返します。
	*/
	public static function getValues(hash:Object):Array {
		if (hash === null) {
			return null;
		}
		var values:Array = [];
		for each (var value:Object in hash) {
			values.push(value);
		}
		return values;
	}
	
	//______________ cloneHash ______________//
	/**
	*/
	public static function cloneHash(object:Object):Object {
		var newObject:Object = new Object();
		for (var key:Object in object) {
			newObject[key] = object[key];
		}
		return newObject;
	}
	
	//______________ hashToString ______________//
	/**
	*/
	public static function hashToString(hash:Object):String {
		if (!hash) {
			return "null";
		}
		var string:String = "{";//}
		var flag:Boolean = false;
		for (var key:Object in hash) {
			if (flag) {
				string += ", ";
			}
			string += key+":"+hash[key];
			flag = true;
		}
		//{
		string += "}";
		return string;
	}
	
}

import flash.text.TextField;
import flash.text.TextFormatAlign;

class StringTools {
	
	//______________ abbrTextField ______________//
	/**
	右端を省略する。
	複数行には未対応。
	*/
	public static function abbrTextField(textField:TextField):void {
		var index:int;
		if (textField.defaultTextFormat.align == TextFormatAlign.RIGHT) {
			//未対応
		}
		else if (textField.defaultTextFormat.align == TextFormatAlign.CENTER) {
			//未対応
		}
		else {
			index = textField.getCharIndexAtPoint(textField.width-5, textField.height-10);
			index--;
			if (index < 0) {
				return;
			}
			textField.replaceText(index, textField.length, "...");
		}
	}
	
	//______________ trim ______________//
	/**
	*/
	public static function trim(string:String):String {
		var newString:String = string.replace(/^\s+/, "");
		newString = newString.replace(/\s+$/, "");
		return newString;
	}
	
}