package ethanjones.modularworld.core.localization;

import ethanjones.modularworld.core.system.ModularWorldException;
import ethanjones.modularworld.core.logging.Log;
import ethanjones.modularworld.graphics.asset.AssetManager;

import java.util.HashMap;
import java.util.Locale;
import java.util.Scanner;

public class Localization {

  private static HashMap<String, Language> languages = new HashMap<String, Language>();
  private static Language language;

  private static Language load(String string) {
    try {
      HashMap<String, String> data = new HashMap<String, String>();
      Scanner scanner = new Scanner(string);
      String code = "";
      int i = 0;
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (line.startsWith("#")) continue;
        i++;
        if (i == 1) {
          code = line;
          continue;
        }
        int index = line.indexOf("=");
        if (index == -1) continue;
        String str = line.substring(0, index).toLowerCase();
        String localization = line.substring(index + 1);
        data.put(str, localization);
      }
      scanner.close();
      return new Language(code, data);
    } catch (Exception e) {
      Log.error(new ModularWorldException("Failed to read language file: " + string, e));
    }
    return null;
  }

  public static void load(AssetManager.AssetFolder assetFolder) {
    AssetManager.AssetFolder languageFolder = assetFolder.folders.get("language");
    for (AssetManager.Asset asset : languageFolder.files.values()) {
      try {
        Language language = load(new String(asset.bytes));
        languages.put(language.getCode(), language);
      } catch (Exception e) {
        Log.error(new ModularWorldException("Failed to read language file: " + asset.path, e));
      }
    }
  }

  public static Language getLangauage() {
    if (language == null) {
      Language local = languages.get(Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry());
      if (local != null) {
        language = local;
      } else {
        language = languages.get("en_GB");
      }
    }
    return language;
  }

  public static String get(String str) {
    String s = getLangauage().get(str);
    return s != null ? s : str;
  }
}

@@ -85,36 +85,19 @@
 ore.
-system.ModularWorldException
+logging.Log
 ;%0Aim
@@ -126,35 +126,52 @@
 rworld.core.
-logging.Log
+system.ModularWorldException
 ;%0Aimport eth
@@ -328,16 +328,74 @@
 tion %7B%0A%0A
+  public static final String DEFAULT_LANGUAGE = %22en_GB%22;%0A%0A
   privat
@@ -474,16 +474,59 @@
 age%3E();%0A
+  private static Language defaultLanguage;%0A
   privat
@@ -1661,25 +1661,16 @@
         
-Language 
 language
@@ -1756,16 +1756,122 @@
 guage);%0A
+        Log.debug(%22Loaded localisation %5C%22%22 + language.getCode() + %22%5C%22 from file %5C%22%22 + asset.path + %22%5C%22%22);%0A
       %7D 
@@ -1945,39 +1945,43 @@
 Failed to read l
-anguage
+ocalisation
  file: %22 + asset
@@ -2006,37 +2006,28 @@
 %7D%0A    %7D%0A
+%0A
   
-%7D%0A%0A  public static 
+  default
 Language
@@ -2031,24 +2031,42 @@
 age 
-getL
+= l
 ang
-a
 uage
-() %7B
+s.get(DEFAULT_LANGUAGE);
 %0A   
@@ -2070,17 +2070,24 @@
     if (
-l
+defaultL
 anguage 
@@ -2098,48 +2098,134 @@
 ull)
- %7B
 %0A      
-Language local = languages.get(
+throw new ModularWorldException(%22Failed to load the default language (%22 + DEFAULT_LANGUAGE + %22)%22);%0A%0A    String local = 
 Loca
@@ -2298,66 +2298,123 @@
 ry()
-)
 ;%0A    
-  if (local != null) %7B%0A        language =
+language = languages.get(local);%0A    if (language == null) %7B%0A      Log.info(%22No localisation for %22 +
  local
+)
 ;%0A
-  
     
@@ -2432,85 +2432,66 @@
     
-  language = languages.get(%22en_GB%22);%0A      %7D%0A    %7D%0A    return language;
+Log.info(%22Localisation loaded for %22 + local);%0A    %7D
 %0A  %7D%0A%0A
+%0A
   pu
@@ -2535,33 +2535,65 @@
     
-String s = getL
+if (language != null) %7B%0A      String localised = l
 ang
-a
 uage
-()
 .get
@@ -2607,30 +2607,182 @@
     
-return s != null ? s :
+  if (localised != null) return localised;%0A    %7D%0A    String defaultLocalised = defaultLanguage.get(str);%0A    if (defaultLocalised != null) return defaultLocalised;%0A    return
  str

Add a default language