1 64 package com.jcorporate.expresso.core.i18n; 65 66 import com.jcorporate.expresso.core.dbobj.Schema; 67 import com.jcorporate.expresso.core.dbobj.SchemaFactory; 68 import com.jcorporate.expresso.core.misc.StringUtil; 69 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 70 import org.apache.log4j.Logger; 71 72 import java.text.MessageFormat ; 73 import java.util.HashMap ; 74 import java.util.Locale ; 75 import java.util.Map ; 76 import java.util.ResourceBundle ; 77 import java.util.WeakHashMap ; 78 79 80 84 public class MessageBundle extends Object { 85 88 public static final String DEFAULT_BUNDLE_NAME = "MessagesBundle"; 89 public static final String DEFAULT_BUNDLE_PATH = "com.jcorporate.expresso.core"; 90 91 94 private static Logger log = Logger.getLogger(MessageBundle.class); 95 96 99 private static final Locale BLANK_LOCALE = new Locale ("en", "us", "null"); 100 private static Map schemas = new HashMap (); 101 102 105 private String bundleName = DEFAULT_BUNDLE_NAME; 106 private String bundlePath = null; 107 private String myCountry = null; 108 private String myLanguage = null; 109 private String mySchema = null; 110 111 114 private WeakHashMap bundleCache = new WeakHashMap (); 115 116 119 public MessageBundle() { 120 } 121 122 123 124 129 public synchronized void setBundleName(String bundleName) { 130 this.bundleName = bundleName; 131 } 132 133 139 public synchronized String getBundleName() { 140 return bundleName; 141 } 142 143 148 public synchronized void setBundlePath(String newBundlePath) { 149 bundlePath = newBundlePath; 150 } 151 152 153 154 160 public synchronized String getBundlePath() { 161 return bundlePath; 162 } 163 164 165 166 171 public synchronized void setCountry(String newCountry) { 172 myCountry = newCountry; 173 } 174 175 176 177 182 public String getCountry() { 183 return myCountry; 184 } 185 186 187 188 193 public synchronized void setLanguage(String newLanguage) { 194 myLanguage = newLanguage; 195 } 196 197 198 199 202 public String getLanguage() { 203 return myLanguage; 204 } 205 206 207 208 214 public synchronized void setSchema(String schemaName) { 215 mySchema = schemaName; 216 217 Schema theSchema = getSchema(mySchema); 218 219 bundlePath = theSchema.getMessageBundlePath(); 220 } 221 222 223 224 232 public String getString(String stringCode, Object [] args) { 233 try { 234 Locale l = null; 235 236 if (myLanguage != null) { 237 if (myCountry.equals("default")) { 238 l = new Locale (myLanguage, ""); 239 } else { 240 l = new Locale (myLanguage, myCountry); 241 } 242 } else { 243 l = Locale.getDefault(); 244 } 245 246 String messageString = getStringFromBundle(l, stringCode); 247 MessageFormat formatter = new MessageFormat (""); 248 formatter.setLocale(l); 249 formatter.applyPattern(messageString); 250 251 return StringUtil.notNull(formatter.format(args)); 252 } catch (Exception se) { 253 254 String msg = "No such key '" + 255 stringCode + "' in bundle at '" + bundlePath + "'"; 256 if (log.isDebugEnabled()) { 257 msg += " with language '" + getLanguage() + "', " + "Country '" + 258 getCountry() + "'"; 259 } 260 log.warn(msg); 261 return stringCode; 262 } 263 } 264 265 266 267 278 public String getStringRequired(String stringCode, Object [] args) throws IllegalArgumentException { 279 try { 280 Locale locale = null; 281 String result = ""; 282 283 if (myLanguage != null) { 284 if (myCountry.equals("default")) { 285 locale = new Locale (myLanguage, ""); 286 } else { 287 locale = new Locale (myLanguage, myCountry); 288 } 289 } else { 290 locale = Locale.getDefault(); 291 } 292 293 String messageString = getStringFromBundle(locale, stringCode); 294 if (args != null && args.length > 0) { 295 MessageFormat formatter = new MessageFormat (messageString, locale); 296 result = StringUtil.notNull(formatter.format(args)); 297 } else { 298 result = messageString; 299 } 300 301 return result; 302 } catch (Exception se) { 303 String msg = "No such key '" + 304 stringCode + "' in bundle at '" + bundlePath + "'"; 305 if (log.isDebugEnabled()) { 306 msg += " with language '" + getLanguage() + "', " + "Country '" + 307 getCountry() + "'"; 308 } 309 log.warn(msg); 310 throw new IllegalArgumentException (msg); 311 } 312 } 313 314 315 323 protected Schema getSchema(String schemaClass) { 324 Schema oneSchema; 325 326 synchronized (schemas) { 327 oneSchema = (Schema) schemas.get(schemaClass); 328 } 329 330 if (oneSchema != null) { 331 return oneSchema; 332 } else { 333 oneSchema = SchemaFactory.getInstance().getSchema(schemaClass); 334 335 if (oneSchema == null) { 336 throw new IllegalArgumentException ("Can't instantiate " + 337 "Schema class " + schemaClass); 338 } 339 } 340 341 synchronized (schemas) { 342 schemas.put(schemaClass, oneSchema); 343 } 344 345 return oneSchema; 346 } 347 348 349 350 357 protected String getStringFromBundle(Locale ourLocale, String stringCode) { 358 String result = null; 359 try { 360 ResourceBundle rb = getBundle(ourLocale); 361 result = rb.getString(stringCode); 362 } catch (Exception e) { 363 if (!DEFAULT_BUNDLE_PATH.equals(bundlePath)) { 365 result = getStringFromDefaultSchemaBundle(ourLocale, stringCode); 366 } 367 if (result == null) { 368 throw new RuntimeException ("cannot find translated string"); 369 } 370 } 371 return result; 372 } 373 374 381 protected String getStringFromDefaultSchemaBundle(Locale ourLocale, String stringCode) { 382 ResourceBundle rb = null; 383 synchronized (bundleCache) { 384 if (ourLocale != null) { 385 rb = getBundleFromPath(ourLocale, DEFAULT_BUNDLE_PATH); 386 } else { 387 rb = getBundleFromPath(BLANK_LOCALE, DEFAULT_BUNDLE_PATH); 388 } 389 } 390 391 return rb.getString(stringCode); 392 } 393 394 395 396 403 private synchronized ResourceBundle getBundle(Locale ourLocale) { 404 405 ResourceBundle rb = null; 406 synchronized (bundleCache) { 407 if (ourLocale != null) { 408 rb = getBundleFromPath(ourLocale, bundlePath); 409 } else { 410 rb = getBundleFromPath(BLANK_LOCALE, bundlePath); 411 } 412 } 413 return rb; 414 } 415 416 419 private ResourceBundle getBundleFromPath(Locale ourLocale, String path) { 420 String key = ourLocale + path; 421 ResourceBundle rb = (ResourceBundle ) bundleCache.get(key); 422 423 if (rb == null) { 424 FastStringBuffer fsb = new FastStringBuffer(path); 425 fsb.append("/" + bundleName); 426 rb = ResourceBundle.getBundle(fsb.toString(), ourLocale); 427 428 bundleCache.put(key, rb); 429 } 430 431 return rb; 432 } 433 434 442 public String getStringOrNull(String stringCode, Object [] args) { 443 try { 444 Locale l = null; 445 446 if (myLanguage != null) { 447 if (myCountry.equals("default")) { 448 l = new Locale (myLanguage, ""); 449 } else { 450 l = new Locale (myLanguage, myCountry); 451 } 452 } else { 453 l = Locale.getDefault(); 454 } 455 456 String messageString = getStringFromBundle(l, stringCode); 457 MessageFormat formatter = new MessageFormat (""); 458 formatter.setLocale(l); 459 formatter.applyPattern(messageString); 460 461 return StringUtil.notNull(formatter.format(args)); 462 } catch (Exception se) { 463 return null; 464 } 465 } 466 } 467 468 469 | Popular Tags |