package org.javarosa.core.services.locale;

import java.util.Hashtable;

import org.javarosa.core.reference.ReferenceDataSource;

public class Localization {
	private static Localizer globalLocalizer;
	
	public static String get(String key){
		return get(key, new String[]{});
	}
	
	public static String get(String key, String[] args) {
		checkRep();
		return globalLocalizer.getText(key, args);
	}
	
	public static String get(String key, Hashtable args) {
		checkRep();
		return globalLocalizer.getText(key, args);
	}
	
	public static void registerLanguageFile(String localeName, String resourceFileURI) {
		init();
		if(!globalLocalizer.hasLocale(localeName)){ 
			globalLocalizer.addAvailableLocale(localeName);
		}
		globalLocalizer.registerLocaleResource(localeName, new ResourceFileDataSource(resourceFileURI));
		if(globalLocalizer.getDefaultLocale() == null) {
			globalLocalizer.setDefaultLocale(localeName);
		}
	}
	
	public static void registerLanguageReference(String localeName, String referenceUri) {
		init();
		if(!globalLocalizer.hasLocale(localeName)){ 
			globalLocalizer.addAvailableLocale(localeName);
		}
		globalLocalizer.registerLocaleResource(localeName, new ReferenceDataSource(referenceUri));
		if(globalLocalizer.getDefaultLocale() == null) {
			globalLocalizer.setDefaultLocale(localeName);
		}
	}
	
	public static Localizer getGlobalLocalizerAdvanced() {
		init();
		return globalLocalizer;
	}
	
	public static void setLocale(String locale) {
		checkRep();
		globalLocalizer.setLocale(locale);
	}
	
	public static void setDefaultLocale(String defaultLocale) {
		checkRep();
		globalLocalizer.setDefaultLocale(defaultLocale);
	}
	
	/**
	 * 
	 */
	private static void init() {
		if(globalLocalizer == null) {
			globalLocalizer = new Localizer(true,false);
		}
	}
	/**
	 *  
	 */
	private static void checkRep() {
		init();
		if(globalLocalizer.getAvailableLocales().length == 0) {
			throw new LocaleTextException("There are no locales defined for the application. Please make sure to register locale text using the Locale.register() method");
		}
	}
}

@@ -623,32 +623,37 @@
 eURI) %7B%0D%0A%09%09init(
+false
 );%0D%0A%09%09if(!global
@@ -1047,32 +1047,37 @@
 eUri) %7B%0D%0A%09%09init(
+false
 );%0D%0A%09%09if(!global
@@ -1437,24 +1437,29 @@
 ) %7B%0D%0A%09%09init(
+false
 );%0D%0A%09%09return
@@ -1740,38 +1740,37 @@
 %0D%0A%09 * %0D%0A%09 */%0D%0A%09p
-rivate
+ublic
  static void ini
@@ -1771,16 +1771,29 @@
 id init(
+boolean force
 ) %7B%0D%0A%09%09i
@@ -1809,32 +1809,41 @@
 ocalizer == null
+ %7C%7C force
 ) %7B%0D%0A%09%09%09globalLo
@@ -1889,16 +1889,124 @@
 %09%7D%0D%0A%09%7D%0D%0A
+%09%0D%0A%09public static void setLocalizationData(Localizer localizer) %7B%0D%0A%09%09globalLocalizer = localizer;%0D%0A%0D%0A%09%7D%0D%0A%09%0D%0A
 %09/**%0D%0A%09 
@@ -2058,16 +2058,21 @@
 %0A%09%09init(
+false
 );%0D%0A%09%09if

allow reinitializing the localizer (necessary for in-place updates and such).