package mytown.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

/**
 * Loads and handles Localization files
 * 
 * @author Joe Goett
 */
public class Localization {
	/**
	 * The localization map
	 */
	Map<String, String> localizations;
	Reader reader = null;

	/**
	 * Specifies the {@link Reader} to use when reading the localization
	 * 
	 * @param r
	 */
	public Localization(Reader r) {
		reader = r;
		localizations = new HashMap<String, String>();
	}

	/**
	 * Specifies the file to load via a {@link File}
	 * 
	 * @param file
	 * @throws FileNotFoundException
	 */
	public Localization(File file) throws FileNotFoundException {
		this(new FileReader(file));
	}

	/**
	 * Specifies the file to load via a filename
	 * 
	 * @param filename
	 * @throws FileNotFoundException
	 */
	public Localization(String filename) throws FileNotFoundException {
		this(new File(filename));
	}

	/**
	 * Do the actual loading of the Localization file
	 * 
	 * @throws IOException
	 */
	public void load() throws IOException {
		BufferedReader br = new BufferedReader(reader);

		String line;
		while ((line = br.readLine()) != null) {
			if (line.startsWith("#") || line.trim().isEmpty()) {
				continue; // Ignore comments and empty lines
			}
			String[] entry = line.split("=");
			if (entry.length < 2) {
				continue; // Ignore entries that are not formatted correctly (maybe log later)
			}
			localizations.put(entry[0], entry[1]);
		}

		br.close();
	}

	private String getLocalizationFromKey(String key) {
		String localized = localizations.get(key);
		return localized == null ? key : localized;
	}

	/**
	 * Returns the localized version of the given unlocalized key
	 * 
	 * @param key
	 * @param args
	 * @return
	 */
	public String getLocalization(String key, Object... args) {
		if (args.length > 0)
			return String.format(getLocalizationFromKey(key), args);
		else
			return getLocalizationFromKey(key);

	}
}
@@ -1294,16 +1294,117 @@
 null) %7B%0A
+            line = line.trim(); // Trim it in-case there is spaces before the actual key-value pairs%0A
 %09%09%09if (l
@@ -1435,15 +1435,8 @@
 ine.
-trim().
 isEm
@@ -1679,16 +1679,23 @@
 entry%5B0%5D
+.trim()
 , entry%5B
@@ -1696,16 +1696,23 @@
 entry%5B1%5D
+.trim()
 );%0A%09%09%7D%0A%0A
@@ -2189,12 +2189,103 @@
 (key);%0A%0A
-
 %09%7D
+%0A%0A    public Map%3CString, String%3E getLocalizationMap() %7B%0A        return localizations;%0A    %7D
 %0A%7D

Trim each line before doing anything else. Allows having spaces before comments, but still will ignore them completely. As well as make sure all keys and values are trimmed before storing in the Map.