1 package SnowMailClient.Language; 2 3 import snow.utils.storage.FileUtils; 4 5 import java.io.*; 6 import java.util.*; 7 import javax.swing.JOptionPane ; 8 9 28 public final class Language 29 { 30 public static final String ENGLISH = "English"; 31 public static final String FRANCAIS = "Francais"; 32 public static final String DEUTSCH = "Deutsch"; 33 public static final String POLISH = "Polish"; 34 35 private static final String PropertyFile = "Languages.properties"; 37 38 private SentenceDictionary dictionary = null; 40 private String actualLanguage = ENGLISH; private boolean readFromJar = true; 43 44 public static String baseDirectory = "."; 46 47 private Language() { } 49 private static Language languageInstance = null; 50 51 public Locale getLocale() 52 { 53 if(actualLanguage==null) return Locale.ENGLISH; 54 if(actualLanguage.equals(ENGLISH)) return Locale.ENGLISH; 55 if(actualLanguage.equals(FRANCAIS)) return Locale.FRENCH; 56 if(actualLanguage.equals(DEUTSCH)) return Locale.GERMAN; 57 return Locale.ENGLISH; 59 } 60 61 private static String defaultLanguage = ENGLISH; 62 public static void setDefaultLanguage(String language) 63 { 64 defaultLanguage = language; 65 } 66 67 69 public static Language getInstance() 70 { 71 if(languageInstance==null) 72 { 73 languageInstance = new Language(); 75 76 Properties prop = new Properties(); 78 File propFile = new File(baseDirectory, PropertyFile); 79 System.out.println("Read language props in "+propFile+" ( base="+baseDirectory+")"); 80 if(propFile.exists()) 81 { 82 FileInputStream fis = null; 83 try 84 { 85 prop.load(fis = new FileInputStream(propFile)); 86 } 87 catch(Exception e) 88 { 89 e.printStackTrace(); } 91 finally 92 { 93 if(fis!=null) try{ fis.close();} catch(Exception ee){} 94 } 95 } 96 97 String lang = prop.getProperty("ActiveTranslationLanguage", defaultLanguage); 98 String readFromJarProp = prop.getProperty("ReadFromJar", "True"); 101 102 languageInstance.readFromJar = readFromJarProp.equals("True"); 103 languageInstance.setActualTranslation(lang, languageInstance.readFromJar); 104 105 System.out.println("Read ini lang = "+lang); 106 107 } 108 return languageInstance; 109 } 110 111 114 public String [] getAvailableInternalLanguages() 115 { 116 return SentenceDictionary.getInternalAvailableTranslations(); 117 } 118 119 public String [] getAvailableLanguages() 120 { 121 Vector<String > found = new Vector<String >(); 122 found.addElement(ENGLISH); 124 File base = new File("SnowMailClient/Language"); 125 126 if(base.isDirectory() && base.exists()) 127 { 128 File[] files = base.listFiles(); 129 for(int i=0; i<files.length; i++) 130 { 131 int pos = files[i].getName().toLowerCase().indexOf(".translation"); 132 if(pos!=-1) 133 { 134 found.addElement(files[i].getName().substring(0,pos)); 135 } 136 } 137 } 138 return found.toArray(new String [found.size()]); 139 } 140 141 public boolean getActualTranslationWasReadFromJarFile() { return readFromJar; } 142 143 public String getActualTranslation() {return actualLanguage;} 144 145 146 151 public void setActualTranslation(String language, boolean fromJarFile) 152 { 153 154 155 actualLanguage = language; 156 this.readFromJar = fromJarFile; 157 158 saveProperties(); 160 161 162 if(actualLanguage.compareToIgnoreCase("english")==0) 163 { 164 dictionary = null; 165 } 166 else 167 { 168 try 169 { 170 if(fromJarFile) 171 { 172 dictionary = SentenceDictionary.readFromJarFile(language); 173 if(dictionary==null) 174 { 175 dictionary = SentenceDictionary.readFromFile(language, true); } 177 } 179 else 180 { 181 dictionary = SentenceDictionary.readFromFile(language, true); System.out.println("Language "+language+" read from file"); 183 } 184 } 186 catch(Exception e) 187 { 188 JOptionPane.showMessageDialog(null, ""+e.getMessage() 190 +"\nDelete the language file in Language/ or use a language file\ncompatible with the current Schmortopf version.", "Language pack error", 191 JOptionPane.ERROR_MESSAGE); 192 } 193 } 194 } 195 196 197 200 public SentenceDictionary getDictionaryFromFile(String language, boolean useEmbeddedInNotFound ) 201 { 202 if(actualLanguage.equals(language) 203 && readFromJar == false) return dictionary; 204 205 try 206 { 207 SentenceDictionary dic = SentenceDictionary.readFromFile(language, useEmbeddedInNotFound); 208 return dic; 209 } 210 catch(Exception e) 211 { 212 e.printStackTrace(); 214 return null; 215 } 216 } 217 218 219 222 public void save() 223 { 224 } 225 226 227 230 private void saveProperties() 231 { 232 String a = ""; 235 236 237 238 239 Properties prop = new Properties(); 240 prop.setProperty("ActiveTranslationLanguage", actualLanguage); 241 prop.setProperty("ReadFromJar", (readFromJar?"True":"False")); 242 243 File propFile = new File(baseDirectory, PropertyFile); 244 System.out.println("Save language props in "+propFile); 245 FileOutputStream fos = null; 246 try 247 { 248 if(!propFile.getParentFile().exists()) 249 { 250 propFile.getParentFile().mkdirs(); 251 } 252 fos = new FileOutputStream(propFile); 253 prop.store(fos, "Schmortopf Language Settings"); 254 } 255 catch(Exception e) 256 { 257 e.printStackTrace(); 258 } 259 finally 260 { 261 FileUtils.closeIgnoringExceptions(fos); 262 } 263 264 265 294 295 296 } 297 298 299 307 public static String translate(String sentence) 308 { 309 if(getInstance().actualLanguage.compareToIgnoreCase("english")==0) return sentence; 310 if(getInstance().dictionary!=null) 311 { 312 return getInstance().dictionary.getTranslatedSentence(sentence); 313 } 314 else 315 { 316 return sentence; 317 } 318 } 319 320 321 324 public static String translate(String _sentenceWithArgument, String arg) 325 { 326 String sentenceWithArgument = translate(_sentenceWithArgument); 327 328 StringBuffer sb = new StringBuffer (sentenceWithArgument); 329 int pos = sentenceWithArgument.indexOf("%"); 330 if(pos==-1) 331 { 332 System.out.println("***************************************************"); 334 System.out.println("Invalid sentence for translation (should have a % for the replacement)"); 335 System.out.println(" sentence = "+_sentenceWithArgument); 336 System.out.println(" arg = "+ arg); 337 System.out.println("***************************************************"); 338 339 return sentenceWithArgument+" "+arg; 340 } 341 342 return sb.replace(pos,pos+1, arg).toString(); 343 } 344 345 348 public static String translate(String _sentenceWithArgument, String arg1, String arg2) 349 { 350 String sentenceWithArgument = translate(_sentenceWithArgument); 351 StringBuffer sb = new StringBuffer (sentenceWithArgument); 352 353 int pos1 = sentenceWithArgument.indexOf("%1"); 355 if(pos1==-1) 356 { 357 System.out.println("***************************************************"); 358 System.out.println("Invalid sentence for translation (should have a %1 for the first replacement)"); 359 System.out.println(" sentence = "+_sentenceWithArgument); 360 System.out.println(" arg1 = "+ arg1); 361 System.out.println(" arg2 = "+ arg2); 362 System.out.println("***************************************************"); 363 364 return sentenceWithArgument+" "+arg1+" "+arg2; 365 } 366 367 sb = sb.replace(pos1,pos1+2, arg1); 368 369 sentenceWithArgument = sb.toString(); 371 int pos2 = sentenceWithArgument.indexOf("%2"); 372 if(pos2==-1) 373 { 374 System.out.println("***************************************************"); 375 System.out.println("Invalid sentence for translation (should have a %2 for the first replacement)"); 376 System.out.println(" sentence = "+_sentenceWithArgument); 377 System.out.println(" arg1 = "+ arg1); 378 System.out.println(" arg2 = "+ arg2); 379 System.out.println("***************************************************"); 380 381 return sb.toString()+" "+arg2; 382 } 383 384 sb = sb.replace(pos2,pos2+2, arg2); 385 386 return sb.toString(); 387 } 388 389 392 public static String translate(String _sentenceWithArgument, String arg1, String arg2, String arg3) 393 { 394 String sentenceWithArgument = translate(_sentenceWithArgument); 395 StringBuffer sb = new StringBuffer (sentenceWithArgument); 396 397 int pos1 = sentenceWithArgument.indexOf("%1"); 399 if(pos1==-1) 400 { 401 System.out.println("***************************************************"); 402 System.out.println("Invalid sentence for translation (should have a %1 for the first replacement)"); 403 System.out.println(" sentence = "+_sentenceWithArgument); 404 System.out.println(" arg1 = "+ arg1); 405 System.out.println(" arg2 = "+ arg2); 406 System.out.println("***************************************************"); 407 408 return sentenceWithArgument+" "+arg1+" "+arg2; 409 } 410 411 sb = sb.replace(pos1,pos1+2, arg1); 412 413 sentenceWithArgument = sb.toString(); 415 int pos2 = sentenceWithArgument.indexOf("%2"); 416 if(pos2==-1) 417 { 418 System.out.println("***************************************************"); 419 System.out.println("Invalid sentence for translation (should have a %2 for the second replacement)"); 420 System.out.println(" sentence = "+_sentenceWithArgument); 421 System.out.println(" arg1 = "+ arg1); 422 System.out.println(" arg2 = "+ arg2); 423 System.out.println(" arg3 = "+ arg3); 424 System.out.println("***************************************************"); 425 426 return sb.toString()+" "+arg2; 427 } 428 429 sb = sb.replace(pos2,pos2+2, arg2); 430 431 sentenceWithArgument = sb.toString(); 433 int pos3 = sentenceWithArgument.indexOf("%3"); 434 if(pos3==-1) 435 { 436 System.out.println("***************************************************"); 437 System.out.println("Invalid sentence for translation (should have a %3 for the third replacement)"); 438 System.out.println(" sentence = "+_sentenceWithArgument); 439 System.out.println(" arg1 = "+ arg1); 440 System.out.println(" arg2 = "+ arg2); 441 System.out.println(" arg3 = "+ arg3); 442 System.out.println("***************************************************"); 443 444 return sb.toString()+" "+arg3; 445 } 446 447 sb = sb.replace(pos3, pos3+2, arg3); 448 449 return sb.toString(); 450 } 451 452 } | Popular Tags |