package risk.engine.translation;

import java.util.ResourceBundle;
import java.util.Locale;



/**
 * just a small class keeping the only resource
 * bundle needed in memory
 *
 * @author Christian Weiske <cweiske@cweiske.de>
 */
public class TranslationBundle
{
	private static ResourceBundle resBundle = null;
	private static String strLanguage = null;
	private static Locale loc = null;



	/**
	 * returns the resource bundle
	 *
	 * It should be accessed statically as
	 * this causes only one instance of the resource
	 * bundle to load, which can be reused in all
	 * parts of the program
	 */
	static public ResourceBundle getBundle()
	{
		if (TranslationBundle.resBundle == null) {
			TranslationBundle.loadBundle();
		}
		return TranslationBundle.resBundle;
	}//static public ResourceBundle getBundle()



	/**
	 * loads the translation bundle
	 */
	static private void loadBundle()
	{
		//load the default or a forced locale
		if (TranslationBundle.strLanguage == null) {
			loc = Locale.getDefault();
		} else {
			loc = new Locale(TranslationBundle.strLanguage);
		}

		try {
			TranslationBundle.resBundle = ResourceBundle.getBundle("risk.engine.translation.Risk", loc,TranslationBundle.class.getClassLoader());
		} catch( java.util.MissingResourceException e) {
			throw new RuntimeException( "Error: Can't find a translation file.",e);
			//System.exit( 2);
		}
	}//static private void loadBundle()



	/**
	 * sets the language to use
	 *
	 * should be called from a main method after parsing the
	 * command line
	 */
	static private void setLanguage(String strLanguage)
	{
		TranslationBundle.strLanguage = strLanguage;
	}//static private void setLanguage(String strLanguage)



	/**
	 * parses the command line arguments for
	 * language settings
	 *
	 * you need to call the program with a parameter like
	 * "--lang=en" or "--lang=de" to set the english or german
	 * language
	 */
	static public void parseArgs(String[] args) {

		for (int nA = 0; nA < args.length; nA++ ) {
			if (args[nA].length() > 7 && args[nA].substring( 0, 7).equals( "--lang=")) {
				//set the language to the given string
				TranslationBundle.setLanguage( args[nA].substring( 7));
			}
		}
	}//static public void parseArgs(String[] args)

}//public class TranslationBundle
