﻿/**
 * jp.nium Classes
 * 
 * @author Copyright (C) taka:nium, All Rights Reserved.
 * @version 3.1.62
 * @see http://classes.nium.jp/
 * 
 * jp.nium Classes is (C) 2007-2009 taka:nium and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
package jp.nium.lang {
	import flash.system.Capabilities;
	import flash.utils.Dictionary;
	import jp.nium.core.errors.ErrorMessageConstants;
	import jp.nium.events.EventIntegrator;
	
	/**
	 * <p>Locale クラスは、多言語テキストを制御するためのクラスです。
	 * Locale クラスを直接インスタンス化することはできません。
	 * new Locale() コンストラクタを呼び出すと、ArgumentError 例外がスローされます。</p>
	 * <p>The Locale class is a class to handle muilti languages.
	 * Locale class can not instanciate directly.
	 * When call the new Locale() constructor, the ArgumentError exception will be thrown.</p>
	 * 
	 * @example <listing version="3.0" >
	 * </listing>
	 */
	public class Locale {
		
		/**
		 * <p>言語設定が英語になるよう指定します。</p>
		 * <p>Set the language configuration to "English".</p>
		 */
		public static const EN:String = "en";
		
		/**
		 * <p>言語設定が日本語になるよう指定します。</p>
		 * <p>Set the language configuration to "Japanese".</p>
		 */
		public static const JA:String = "ja";
		
		
		
		
		
		/**
		 * <p>現在設定されている言語を取得または設定します。
		 * デフォルト設定は、Flash Player が実行されているシステムの言語コードになります。</p>
		 * <p>Get or set the current language.
		 * The default setting will be same as System language code which executing the Flash Player.</p>
		 */
		public static function get language():String { return _language; }
		public static function set language( value:String ):void { _language = value || Capabilities.language; }
		private static var _language:String = Capabilities.language;
		
		/**
		 * 指定された言語に対応したストリングが存在しなかった場合に、代替言語として使用される言語を取得します。
		 */
		public static function get defaultLanguage():String { return _defaultLanguage; }
		private static var _defaultLanguage:String = Locale.EN;
		
		/**
		 * EventIntegrator インスタンスを取得します。
		 */
		private static var _integrator:EventIntegrator = new EventIntegrator();
		
		/**
		 * Dictionary インスタンスを取得します。
		 */
		private static var _dictionary:Dictionary = new Dictionary();
		
		
		
		
		
		/**
		 * @private
		 */
		public function Locale() {
			throw new ArgumentError( ErrorMessageConstants.getMessage( "ERROR_2012", "Locale" ) );
		}
		
		
		
		
		
		/**
		 * <p>指定した id に関連付けられたストリングを現在設定されている言語表現で返します。</p>
		 * <p>Returns the string which relate to the specified id by the current language expression.</p>
		 * 
		 * @param id
		 * <p>ストリングに関連付けられた識別子です。</p>
		 * <p>The identifier relates to the string.</p>
		 * @return
		 * <p>関連付けられたストリングです。</p>
		 * <p>Related string.</p>
		 */
		public static function getString( id:String ):String {
			return getStringByLang( id, language );
		}
		
		/**
		 * <p>指定した id と言語に関連付けられたストリングを返します。</p>
		 * <p>Returns the string which relates to the specified id and language.</p>
		 * 
		 * @param id
		 * <p>ストリングに関連付けられた識別子です。</p>
		 * <p>The identifier relates to the string.</p>
		 * @param language
		 * <p>ストリングに関連付けられた言語です。</p>
		 * <p>The language relates to the string.</p>
		 * @return
		 * <p>関連付けられたストリングです。</p>
		 * <p>Related string.</p>
		 */
		public static function getStringByLang( id:String, language:String ):String {
			// 指定された言語で登録されていれば返す
			if ( _dictionary[language] ) { return _dictionary[language][id] || ""; }
			
			// デフォルト言語で登録されている情報を返す
			return _dictionary[_defaultLanguage][id] || "";
		}
		
		/**
		 * <p>ストリングを指定した id と言語に関連付けます。</p>
		 * <p>Relate the specified string to the language.</p>
		 * 
		 * @param id
		 * <p>ストリングに関連付ける識別子です。</p>
		 * <p>The identifier relates to the string.</p>
		 * @param language
		 * <p>ストリングに関連付ける言語です。</p>
		 * <p>The language relates to the string.</p>
		 * @param value
		 * <p>関連付けるストリングです。</p>
		 * <p>Related string.</p>
		 */
		public static function setString( id:String, language:String, value:String ):void {
			// 初期化されていなければ初期化する
			_dictionary[language] ||= new Dictionary();
			
			// 設定する
			_dictionary[language][id] = value;
		}
	}
}
