1 package com.ca.commons.cbutil; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.text.MessageFormat ; 6 import java.util.*; 7 import java.util.logging.Logger ; 8 import java.util.logging.Level ; 9 10 11 22 public class CBLocalization 23 { 24 private final static Logger log = Logger.getLogger(CBLocalization.class.getName()); 25 26 Locale locale = null; 27 28 MessageFormat messageFormatter = null; 29 30 Hashtable translations = null; 31 32 private boolean errorGiven = false; 34 38 private boolean english = true; 39 40 46 public CBLocalization(Locale locale, String path) 47 { 48 this.locale = locale; 49 50 translations = new Hashtable(500); 51 52 loadLanguageFile(path, locale); 53 54 if (!locale.getLanguage().equals("en")) 55 english = false; 56 57 messageFormatter = new MessageFormat (""); 58 messageFormatter.setLocale(locale); 59 } 60 61 67 public String get(String key) 68 { 69 if (key == null) { 71 return "null key"; 72 } 73 74 if (translations == null || translations.size() == 0) { 76 if (errorGiven == false) { if (!english) log.warning("Unable to translate (" + key + ") - can't find language file."); 79 errorGiven = true; 80 } 81 return key; } 83 84 try 85 { 86 String val = (String ) translations.get(key); if (val == null) { 89 if (!english) log.warning("Can't find translation for '" + key + "' - returning '" + key + "' unchanged."); 90 return key; 91 } 92 return val; 93 } 94 catch (MissingResourceException e) 95 { 96 return key; } 98 } 99 100 111 public String get(String key, ArrayList args) 112 { 113 return get(key, args.toArray()); 114 } 115 116 127 public String get(String key, Object [] args) 128 { 129 if (key == null) { 131 return "null key"; 132 } 133 String val = key; 134 135 if (translations == null || translations.size() == 0) { 137 if (errorGiven == false) { if (!english) log.warning("Unable to translate (" + key + ") - can't find language file."); 140 errorGiven = true; 141 } 142 } 143 else 144 { 145 try 146 { 147 val = (String ) translations.get(key); if (val == null) { 150 if (!english) log.warning("Can't find translation for (" + key + ") - returning unchanged."); 151 val = key; } 153 } 154 catch (MissingResourceException e) 155 { 156 val = key; } 158 } 159 160 return MessageFormat.format(val, args); 161 } 162 163 172 private void loadLanguageFile(String path, Locale locale) 173 { 174 Vector names = getLanguageFileNames(path, locale); 175 176 if (names == null) 177 { 178 log.warning("Names are null"); 179 return; 180 } 181 182 for (int i = names.size() - 1; i >= 0; i--) 184 { 185 if (loadData(new File (names.get(i).toString())) == true) 187 return; 188 } 189 190 log.warning("Unable to load language file '" + path + "'"); 192 return; 193 } 194 195 209 private static Vector getLanguageFileNames(String path, Locale locale) 210 { 211 if (locale == null) 212 { 213 log.warning("Locale is null"); 214 return null; 215 } 216 217 final Vector result = new Vector(8); 218 final String language = locale.getLanguage(); 219 final int languageLength = language.length(); 220 final String country = locale.getCountry(); 221 final int countryLength = country.length(); 222 final String variant = locale.getVariant(); 223 final int variantLength = variant.length(); 224 final StringBuffer temp = new StringBuffer (path); 225 226 if (languageLength + countryLength + variantLength == 0) 227 { 228 return result; } 230 231 temp.append(language); 232 233 result.addElement(temp.toString() + ".properties"); result.addElement(temp.toString()); 236 if (countryLength + variantLength == 0) 237 { 238 return result; } 240 241 temp.append('_'); 242 temp.append(country); 243 244 result.addElement(temp.toString() + ".properties"); result.addElement(temp.toString()); 247 if (variantLength == 0) 248 { 249 return result; } 251 252 temp.append('_'); 253 temp.append(variant); 254 255 result.addElement(temp.toString() + ".properties"); result.addElement(temp.toString()); 258 return result; } 260 261 270 private boolean loadData(File file) 271 { 272 try 273 { 274 if (file == null || !file.exists()) 275 return false; 276 277 log.info("Reading data from " + file.getAbsolutePath()); 278 279 byte[] data = CBUtility.readStream(new FileInputStream (file)); 281 282 StringBuffer buffy = new StringBuffer (CBUtility.readI18NByteArray(data)); 284 285 buffy.insert(buffy.length(), '\n'); 287 return parseData(buffy.toString()); 289 } 290 catch (Exception e) 291 { 292 log.log(Level.WARNING, "Unable to read data from file '" + file.getAbsolutePath(), e); 293 return false; 294 } 295 } 296 297 308 protected boolean parseData(String text) 309 { 310 int start = 0, end = 0; 311 while ((end = text.indexOf('\n', start)) != -1) 312 { 313 String line = text.substring(start, end); 314 315 line = line.trim(); 316 317 if (line.startsWith("=")) 318 { 319 log.warning("Invalid entry in language file: '" + line + "'"); 320 } 321 else if (line.length() != 0 && line.charAt(0) != '#') { 323 try 324 { 325 int equalPos = 0; 326 327 do { 329 equalPos = line.indexOf('=', equalPos + 1); 330 } 331 while (line.charAt(equalPos - 1) == '\\'); 332 333 String key = unescape(line.substring(0, equalPos)).trim(); 334 String trans = line.substring(equalPos + 1).trim(); 335 translations.put(key, trans); 336 337 } 338 catch (Exception e) 339 { 340 log.warning("Exception parsing data line '" + line + "' -> " + e); 341 } } 343 344 start = end + 1; 345 } 346 347 boolean success = (translations.size() > 0); 350 if (success == false) 351 log.warning("ParseData unsuccessfull - no new data found"); 352 return success; 353 } 354 355 359 private String unescape(String escapeMe) 360 { 361 int pos = 0; 362 while ((pos = escapeMe.indexOf('\\', pos)) >= 0) 363 escapeMe = escapeMe.substring(0, pos) + escapeMe.substring(pos + 1); 364 365 return escapeMe; 366 } 367 368 374 public Enumeration keys() 375 { 376 return translations.keys(); 377 } 378 379 385 public Enumeration getKeys() 386 { 387 return translations.keys(); 388 } 389 390 396 public Object get(Object key) 397 { 398 return translations.get(key); 399 } 400 401 407 public Object getObject(Object key) 408 { 409 return translations.get(key); 410 } 411 412 422 public String getString(String key) 423 { 424 if (key == null) return ""; 425 Object o = translations.get(key); 426 if (o == null) return ""; 427 428 return (o instanceof String ) ? (String ) o : o.toString(); 429 } 430 431 439 public static Locale createLocale(String language) 440 { 441 if (language == null) 442 return Locale.getDefault(); 443 444 String [] langs = Locale.getISOLanguages(); 445 for (int i = 0; i < langs.length; i++) 446 if (language.equalsIgnoreCase(langs[i])) 447 return new Locale(langs[i]); 448 449 return Locale.getDefault(); 450 } 451 } 452 | Popular Tags |