1 31 32 package org.opencms.i18n; 33 34 import org.opencms.util.CmsDateUtil; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.text.DateFormat ; 38 import java.text.MessageFormat ; 39 import java.util.Date ; 40 import java.util.Locale ; 41 import java.util.MissingResourceException ; 42 import java.util.ResourceBundle ; 43 44 59 public class CmsMessages { 60 61 62 public static final String KEY_SHORT_SUFFIX = ".short"; 63 64 65 public static final String UNKNOWN_KEY_EXTENSION = "???"; 66 67 68 private String m_bundleName; 69 70 71 private Locale m_locale; 72 73 74 private ResourceBundle m_resourceBundle; 75 76 82 public CmsMessages(String bundleName, Locale locale) { 83 84 try { 85 m_locale = locale; 86 m_bundleName = bundleName; 87 m_resourceBundle = CmsResourceBundleLoader.getBundle(bundleName, m_locale); 88 } catch (MissingResourceException e) { 89 m_resourceBundle = null; 90 } 91 } 92 93 104 public CmsMessages(String bundleName, String language) { 105 106 this(bundleName, language, "", ""); 107 } 108 109 122 public CmsMessages(String bundleName, String language, String country) { 123 124 this(bundleName, language, country, ""); 125 } 126 127 142 public CmsMessages(String bundleName, String language, String country, String variant) { 143 144 this(bundleName, new Locale (language, country, variant)); 145 } 146 147 150 protected CmsMessages() { 151 152 } 154 155 163 public static String formatUnknownKey(String keyName) { 164 165 StringBuffer buf = new StringBuffer (64); 166 buf.append(UNKNOWN_KEY_EXTENSION); 167 buf.append(" "); 168 buf.append(keyName); 169 buf.append(" "); 170 buf.append(UNKNOWN_KEY_EXTENSION); 171 return buf.toString(); 172 } 173 174 185 public static boolean isUnknownKey(String value) { 186 187 return (value == null) || (value.startsWith(UNKNOWN_KEY_EXTENSION)); 188 } 189 190 193 public boolean equals(Object obj) { 194 195 if (obj == this) { 196 return true; 197 } 198 if (obj instanceof CmsMultiMessages) { 199 return false; 200 } 201 if (obj instanceof CmsMessages) { 202 CmsMessages other = (CmsMessages)obj; 203 return other.getBundleName().equals(m_bundleName) && other.getLocale().equals(m_locale); 204 } 205 return false; 206 } 207 208 213 public ResourceBundle getResourceBundle() { 214 215 return m_resourceBundle; 216 } 217 218 226 public String getDate(Date date) { 227 228 return CmsDateUtil.getDate(date, DateFormat.SHORT, m_locale); 229 } 230 231 240 public String getDate(Date date, int format) { 241 242 return CmsDateUtil.getDate(date, format, m_locale); 243 } 244 245 253 public String getDate(long time) { 254 255 return CmsDateUtil.getDate(new Date (time), DateFormat.SHORT, m_locale); 256 } 257 258 266 public String getDateTime(Date date) { 267 268 return CmsDateUtil.getDateTime(date, DateFormat.SHORT, m_locale); 269 } 270 271 280 public String getDateTime(Date date, int format) { 281 282 return CmsDateUtil.getDateTime(date, format, m_locale); 283 } 284 285 293 public String getDateTime(long time) { 294 295 return CmsDateUtil.getDateTime(new Date (time), DateFormat.SHORT, m_locale); 296 } 297 298 303 public Locale getLocale() { 304 305 return m_locale; 306 } 307 308 320 public String getString(String keyName) throws CmsMessageException { 321 322 if (m_resourceBundle != null) { 323 try { 324 return m_resourceBundle.getString(keyName); 325 } catch (MissingResourceException e) { 326 throw new CmsMessageException(Messages.get().container( 327 Messages.ERR_CANT_FIND_RESOURCE_FOR_BUNDLE_2, 328 keyName, 329 m_bundleName)); 330 } 331 } else { 332 throw new CmsMessageException(Messages.get().container( 333 Messages.ERR_MESSAGE_BUNDLE_NOT_INITIALIZED_1, 334 m_bundleName)); 335 } 336 } 337 338 341 public int hashCode() { 342 343 return m_locale.hashCode() + (m_bundleName == null ? 0 : m_bundleName.hashCode()); 344 } 345 346 351 public boolean isInitialized() { 352 353 return (m_resourceBundle != null); 354 } 355 356 366 public String key(String keyName) { 367 368 return key(keyName, false); 369 } 370 371 386 public String key(String keyName, boolean allowNull) { 387 388 try { 389 if (m_resourceBundle != null) { 390 return m_resourceBundle.getString(keyName); 391 } 392 } catch (MissingResourceException e) { 393 if (allowNull) { 395 return null; 396 } 397 } 398 return formatUnknownKey(keyName); 399 } 400 401 411 public String key(String key, Object arg0) { 412 413 return key(key, new Object [] {arg0}); 414 } 415 416 427 public String key(String key, Object arg0, Object arg1) { 428 429 return key(key, new Object [] {arg0, arg1}); 430 } 431 432 444 public String key(String key, Object arg0, Object arg1, Object arg2) { 445 446 return key(key, new Object [] {arg0, arg1, arg2}); 447 } 448 449 464 public String key(String key, Object [] args) { 465 466 if ((args == null) || (args.length == 0)) { 467 return key(key); 469 } 470 471 String result = key(key, true); 472 if (result == null) { 473 result = formatUnknownKey(key); 475 } else { 476 MessageFormat formatter = new MessageFormat (result, m_locale); 478 result = formatter.format(args); 479 } 480 return result; 482 } 483 484 494 public String keyDefault(String keyName, String defaultValue) { 495 496 String result = key(keyName, true); 497 return (result == null) ? defaultValue : result; 498 } 499 500 525 public String keyWithParams(String keyName) { 526 527 if (keyName.indexOf('|') == -1) { 528 return key(keyName, false); 530 } else { 531 String [] values = CmsStringUtil.splitAsArray(keyName, '|'); 533 String cutKeyName = values[0]; 534 String [] params = new String [values.length - 1]; 535 System.arraycopy(values, 1, params, 0, params.length); 536 return key(cutKeyName, params); 537 } 538 } 539 540 543 public String toString() { 544 545 StringBuffer result = new StringBuffer (); 546 547 result.append('['); 548 result.append(this.getClass().getName()); 549 result.append(", baseName: "); 550 result.append(m_bundleName); 551 result.append(", locale: "); 552 result.append(getLocale()); 553 result.append(']'); 554 555 return result.toString(); 556 } 557 558 563 protected String getBundleName() { 564 565 return m_bundleName; 566 } 567 568 573 protected void setBundleName(String bundleName) { 574 575 m_bundleName = bundleName; 576 } 577 578 583 protected void setLocale(Locale locale) { 584 585 m_locale = locale; 586 } 587 588 593 protected void setResourceBundle(ResourceBundle resourceBundle) { 594 595 m_resourceBundle = resourceBundle; 596 } 597 } | Popular Tags |