package me.coley.recaf.util;

import java.util.HashMap;
import java.util.Map;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;

import me.coley.recaf.Logging;
import me.coley.recaf.config.impl.ConfDisplay;

/**
 * Simplistic language translation loader.
 * 
 * @author Matt
 */
public class Lang {
	private static final Map<String, String> map = new HashMap<>();
	private final static boolean DEBUG = true;

	/**
	 * Get translated string.
	 * 
	 * @param key
	 *            Translation key.
	 * @return Translated string.
	 */
	public static String get(String key) {
		if (DEBUG && !map.containsKey(key)) {
			Logging.error("\t\"" + key + "\": \"missing_translation\",");
		}
		return map.getOrDefault(key, key);
	}

	/**
	 * Load language from file by given name.
	 * 
	 * @param lang
	 *            Name of file <i>(without extension)</i>.
	 */
	public static void load(String lang) {
		// If this is crashing you, add **/*.json to your source filters.
		// The assets need to be on the class-path.
		// Refresh the project when that's done.
		try {
			String prefix = "/resources/lang/";
			String file = prefix + lang + ".json";
			// FxWindow.logging.fine("Loading language from: " + file, 1);
			String jsStr = Streams.toString(Lang.class.getResourceAsStream(file));
			JsonObject json = Json.parse(jsStr).asObject();
			json.forEach(v -> {
				map.put(v.getName(), v.getValue().asString());
			});
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	static {
		load(ConfDisplay.instance().language);
	}
}
@@ -1497,16 +1497,249 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Insert a translation key-value pair.%0A%09 * %0A%09 * @param key%0A%09 *            Translation key.%0A%09 * @param value%0A%09 *            Translation text.%0A%09 */%0A%09public static void load(String key, String value) %7B%0A%09%09map.put(key, value);%0A%09%7D%0A%0A
 %09static 

Allow lang util to register translations internally