package plugins.CENO;

import freenet.l10n.BaseL10n.LANGUAGE;
import freenet.l10n.PluginL10n;
import freenet.pluginmanager.FredPluginBaseL10n;
import freenet.support.Logger;

public class CENOL10n implements FredPluginBaseL10n {

	private static PluginL10n l10n;

	public CENOL10n(String envVar) {
		CENOL10n.l10n = new PluginL10n(this, getLanguageFromEnvVar(envVar));
	}

	public String getString(String key) {
		return CENOL10n.l10n.getBase().getString(key);
	}

	public void setLanguage(LANGUAGE newLanguage) {
		try {
			CENOL10n.l10n = new PluginL10n(this, newLanguage);
		} catch (Exception e) {
			Logger.warning(this, "Could not set language to " + newLanguage);
		}
	}

	public LANGUAGE getLanguageFromEnvVar(String envVar) {
		String envVal = System.getenv(envVar);
		if (envVal != null && !envVal.isEmpty()) {
			envVal = envVal.split("-")[0];
		}
		LANGUAGE lang = LANGUAGE.mapToLanguage(envVal);
		if (lang == null) {
			lang = LANGUAGE.ENGLISH;
		}
		return lang;
	}

	public String getL10nFilesBasePath() {
		return "/plugins/CENO/resources/l10n/";
	}

	public String getL10nFilesMask() {
		return "${lang}.properties";
	}

	public String getL10nOverrideFilesMask() {
		return "${lang}.override.properties";
	}

	public ClassLoader getPluginClassLoader() {
		return CENOL10n.class.getClassLoader();
	}

	public static String get(String key) {
		if (CENOL10n.l10n == null) {
			return "Localization base not initialized";
		}
		return CENOL10n.l10n.getBase().getString(key);
	}

}

@@ -848,16 +848,45 @@
 -%22)%5B0%5D;%0A
+%09%09%7D else %7B%0A%09%09%09envVal = %22en%22;%0A
 %09%09%7D%0A%09%09LA
@@ -935,62 +935,8 @@
 l);%0A
-%09%09if (lang == null) %7B%0A%09%09%09lang = LANGUAGE.ENGLISH;%0A%09%09%7D%0A
 %09%09re

Fix bug: NullPointerException when envvar is not set