アクションプロンプトメッセージマネージャ
ソースコードは下にダウンロードされます.
system_msg.txtファイル:
文書クラスのテスト:
メッセージマネージャ:
文字列ツールクラス:
package
system_msg.txtファイル:
#
props.buyCode.0000= , {0}{1}!
# ,
props.buyCode.0001= , !
# ,
props.buyCode.0002= , !
文書クラスのテスト:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
/**
* ...
* @author ongsh
*/
public class Main extends Sprite
{
private var urlLoader:URLLoader;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
urlLoader.load(new URLRequest("system_msg.txt"));
}
private function loadComplete(e:Event):void
{
MessageManager.load("system",urlLoader.data);
trace(MessageManager.parseMessage("system",'props.buyCode.0000',[100,' ']));
trace(MessageManager.parseMessage("system", 'props.buyCode.0001'));
}
}
}
メッセージマネージャ:
package
{
import flash.utils.Dictionary;
/**
* ...
* @author ongsh
*/
public class MessageManager
{
public static var isDebug:Boolean = false;
private static var messages:Dictionary = new Dictionary();
/**
*
*
* @param contentName
* @param content
*/
public static function load(contentName:String,content:String):void
{
var lines:Array = content.split("
");
for(var i:int=0; i< lines.length ; i ++)
{
var line:String = lines[i];
line = StringUtil.trim(line);
if(line == "")
{
//debugTrace(contentName," "+(i+1)+" !");
continue;
}
if(isAnnotation(line))
{
//debugTrace(contentName," "+(i+1)+" !");
continue;
}
var key:String;
var value:String;
var sep:String = "=";
var firstSepIndex:int = line.indexOf(sep);
if(firstSepIndex!=-1)
{
key = line.substring(0,firstSepIndex);
value = line.substring(firstSepIndex+1);
if(messages[contentName]==null)
{
messages[contentName] = new Dictionary();
}
messages[contentName][key] = value;
}else{
debugTrace(contentName," "+(i+1)+" !");
}
}
}
/**
*
*
* @param contentNam
*/
public static function unload(contentName:String):void
{
if(messages[contentName]!=null)
{
messages[contentName] = null;
}
}
/**
*
* @param contentName
* @param key
* @param argsArray ({0},{1}...)
* @return
*/
public static function parseMessage(contentName:String,key:String,argsArray:Array=null):String
{
if(messages[contentName])
{
var value:String = messages[contentName][key];
if(value==null)
{
debugTrace(contentName,key," ");
return null;
}else{
var str:String = StringUtil.replacePlaceholder(value,argsArray);
return str;
}
}else{
debugTrace(contentName," ");
return null;
}
}
/**
*
*
* @param str
* @return
*/
private static function isAnnotation(str:String):Boolean
{
if(str.charAt(0) == "#")
{
return true;
}else{
return false;
}
}
private static function debugTrace(...args):void
{
if(isDebug)
{
trace.apply(null,args);
}
}
}
}
文字列ツールクラス:
package
{
/**
* ...
* @author ongsh
*/
public class StringUtil
{
/**
* {0} {1}
* @param source
* @param args
* @return
*/
public static function replacePlaceholder(source:String, args:Array):String
{
var pattern:RegExp = /{(\d)}/g;
source = source.replace(pattern, function():String {
return args[arguments[1]];
});
return source;
}
/**
*
* @param source
* @param sep
* @return
*/
public static function replaceBlankSep(source:String,sep:String):String
{
//
source = trim(source);
return source.replace(/\s+/g,sep);;
}
/**
*
* @param str
* @return
*/
public static function trim(str:String):String
{
return str.replace(/(^\s+)|(\s+$)/g,"");
}
}
}