001package net.minecraft.util; 002 003import cpw.mods.fml.common.registry.LanguageRegistry; 004import cpw.mods.fml.relauncher.Side; 005import cpw.mods.fml.relauncher.SideOnly; 006import java.io.BufferedReader; 007import java.io.File; 008import java.io.FileReader; 009import java.io.IOException; 010import java.io.InputStreamReader; 011import java.util.Enumeration; 012import java.util.IllegalFormatException; 013import java.util.Properties; 014import java.util.TreeMap; 015 016public class StringTranslate 017{ 018 /** Is the private singleton instance of StringTranslate. */ 019 private static StringTranslate instance = new StringTranslate("en_US"); 020 021 /** 022 * Contains all key/value pairs to be translated - is loaded from '/lang/en_US.lang' when the StringTranslate is 023 * created. 024 */ 025 public Properties translateTable = new Properties(); 026 private TreeMap languageList; 027 private TreeMap field_94521_d = new TreeMap(); 028 public String currentLanguage; 029 private boolean isUnicode; 030 031 public StringTranslate(String par1Str) 032 { 033 this.loadLanguageList(); 034 this.setLanguage(par1Str, false); 035 } 036 037 /** 038 * Return the StringTranslate singleton instance 039 */ 040 public static StringTranslate getInstance() 041 { 042 return instance; 043 } 044 045 private void loadLanguageList() 046 { 047 TreeMap treemap = new TreeMap(); 048 049 try 050 { 051 BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(StringTranslate.class.getResourceAsStream("/lang/languages.txt"), "UTF-8")); 052 053 for (String s = bufferedreader.readLine(); s != null; s = bufferedreader.readLine()) 054 { 055 String[] astring = s.trim().split("="); 056 057 if (astring != null && astring.length == 2) 058 { 059 treemap.put(astring[0], astring[1]); 060 } 061 } 062 } 063 catch (IOException ioexception) 064 { 065 ioexception.printStackTrace(); 066 return; 067 } 068 069 this.languageList = treemap; 070 this.languageList.put("en_US", "English (US)"); 071 } 072 073 public TreeMap getLanguageList() 074 { 075 return this.languageList; 076 } 077 078 private void loadLanguage(Properties par1Properties, String par2Str) throws IOException 079 { 080 BufferedReader bufferedreader = null; 081 082 if (this.field_94521_d.containsKey(par2Str)) 083 { 084 bufferedreader = new BufferedReader(new FileReader((File)this.field_94521_d.get(par2Str))); 085 } 086 else 087 { 088 bufferedreader = new BufferedReader(new InputStreamReader(StringTranslate.class.getResourceAsStream("/lang/" + par2Str + ".lang"), "UTF-8")); 089 } 090 091 for (String s1 = bufferedreader.readLine(); s1 != null; s1 = bufferedreader.readLine()) 092 { 093 s1 = s1.trim(); 094 095 if (!s1.startsWith("#")) 096 { 097 String[] astring = s1.split("="); 098 099 if (astring != null && astring.length == 2) 100 { 101 par1Properties.setProperty(astring[0], astring[1]); 102 } 103 } 104 } 105 LanguageRegistry.instance().loadLanguageTable(par1Properties, par2Str); 106 } 107 108 public synchronized void setLanguage(String par1Str, boolean par2) 109 { 110 if (par2 || !par1Str.equals(this.currentLanguage)) 111 { 112 Properties properties = new Properties(); 113 114 try 115 { 116 this.loadLanguage(properties, "en_US"); 117 } 118 catch (IOException ioexception) 119 { 120 ; 121 } 122 123 this.isUnicode = false; 124 125 if (!"en_US".equals(par1Str)) 126 { 127 try 128 { 129 this.loadLanguage(properties, par1Str); 130 Enumeration enumeration = properties.propertyNames(); 131 132 while (enumeration.hasMoreElements() && !this.isUnicode) 133 { 134 Object object = enumeration.nextElement(); 135 Object object1 = properties.get(object); 136 137 if (object1 != null) 138 { 139 String s1 = object1.toString(); 140 141 for (int i = 0; i < s1.length(); ++i) 142 { 143 if (s1.charAt(i) >= 256) 144 { 145 this.isUnicode = true; 146 break; 147 } 148 } 149 } 150 } 151 } 152 catch (IOException ioexception1) 153 { 154 ioexception1.printStackTrace(); 155 return; 156 } 157 } 158 159 this.currentLanguage = par1Str; 160 this.translateTable = properties; 161 } 162 } 163 164 /** 165 * Translate a key to current language. 166 */ 167 public synchronized String translateKey(String par1Str) 168 { 169 return this.translateTable.getProperty(par1Str, par1Str); 170 } 171 172 /** 173 * Translate a key to current language applying String.format() 174 */ 175 public synchronized String translateKeyFormat(String par1Str, Object ... par2ArrayOfObj) 176 { 177 String s1 = this.translateTable.getProperty(par1Str, par1Str); 178 179 try 180 { 181 return String.format(s1, par2ArrayOfObj); 182 } 183 catch (IllegalFormatException illegalformatexception) 184 { 185 return "Format error: " + s1; 186 } 187 } 188 189 public String getCurrentLanguage() 190 { 191 return this.currentLanguage; 192 } 193 194 @SideOnly(Side.CLIENT) 195 public boolean isUnicode() 196 { 197 return this.isUnicode; 198 } 199 200 public synchronized boolean func_94520_b(String par1Str) 201 { 202 return this.translateTable.containsKey(par1Str); 203 } 204 205 /** 206 * Translate a key with a extra '.name' at end added, is used by blocks and items. 207 */ 208 public synchronized String translateNamedKey(String par1Str) 209 { 210 return this.translateTable.getProperty(par1Str + ".name", ""); 211 } 212 213 @SideOnly(Side.CLIENT) 214 public static boolean isBidirectional(String par0Str) 215 { 216 return "ar_SA".equals(par0Str) || "he_IL".equals(par0Str); 217 } 218 219 @SideOnly(Side.CLIENT) 220 221 public synchronized void func_94519_a(String par1Str, File par2File) 222 { 223 int i = par1Str.indexOf(46); 224 225 if (i > 0) 226 { 227 par1Str = par1Str.substring(0, i); 228 } 229 230 this.field_94521_d.put(par1Str, par2File); 231 232 if (par1Str.contains(this.currentLanguage)) 233 { 234 this.setLanguage(this.currentLanguage, true); 235 } 236 } 237}