package org.javarosa.core.services.locale;

import org.javarosa.core.reference.ReferenceDataSource;

import java.util.Hashtable;

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 arg) {
        checkRep();
        return globalLocalizer.getText(key, new String[]{arg});
    }

    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);
    }

    /**
     * Used by J2ME
     */
    public static void registerLanguageFile(String localeName, String resourceFileURI) {
        init(false);
        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(false);
        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(false);
        return globalLocalizer;
    }

    public static void setLocale(String locale) {
        checkRep();
        globalLocalizer.setLocale(locale);
    }

    public static void setDefaultLocale(String defaultLocale) {
        checkRep();
        globalLocalizer.setDefaultLocale(defaultLocale);
    }

    public static void init(boolean force) {
        if (globalLocalizer == null || force) {
            globalLocalizer = new Localizer(true, false);
        }
    }

    private static void checkRep() {
        init(false);
        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");
        }
    }
}

@@ -92,16 +92,72 @@
 aSource;
+%0Aimport org.javarosa.core.util.NoLocalizedTextException;
 %0A%0Aimport
@@ -760,24 +760,428 @@
 gs);%0A    %7D%0A%0A
+    public static String getWithDefault(String key, String valueIfKeyMissing) %7B%0A        return getWithDefault(key, new String%5B%5D%7B%7D, valueIfKeyMissing);%0A    %7D%0A%0A    public static String getWithDefault(String key, String%5B%5D args, String valueIfKeyMissing) %7B%0A        try %7B%0A            return get(key, args);%0A        %7D catch (NoLocalizedTextException e) %7B%0A            return valueIfKeyMissing;%0A        %7D%0A    %7D%0A%0A
     /**%0A    

Add safe localization.get method