01: /*
02: * Copyright 2005 jWic Group (http://www.jwic.de)
03: * de.jwic.util.Messages
04: * $Id: Messages.java,v 1.1 2006/01/16 08:31:13 lordsam Exp $
05: */
06: package de.jwic.util;
07:
08: import java.util.ArrayList;
09: import java.util.Locale;
10: import java.util.MissingResourceException;
11: import java.util.ResourceBundle;
12:
13: /**
14: * Handles localized messages.
15: * @version $Revision: 1.1 $
16: * @author Florian Lippisch
17: */
18: public class Messages {
19:
20: private ResourceBundle RESOURCE_BUNDLE = null;
21: private ArrayList bundles = null;
22:
23: /**
24: * @param locale
25: */
26: public Messages(Locale locale, String bundleName) {
27: RESOURCE_BUNDLE = ResourceBundle.getBundle(bundleName, locale);
28: }
29:
30: /**
31: * Returns a string from the resource bundle defined by the key.
32: * @param key
33: * @return
34: */
35: public String getString(String key) {
36: if (RESOURCE_BUNDLE == null) {
37: throw new IllegalStateException(
38: "RESOURCE BUNDLE not yet initialized.");
39: }
40: try {
41: return RESOURCE_BUNDLE.getString(key);
42: } catch (MissingResourceException e) {
43: if (bundles != null) {
44: // look in added bundles
45: for (int i = 0; i < bundles.size(); i++) {
46: ResourceBundle bundle = (ResourceBundle) bundles
47: .get(i);
48: try {
49: return bundle.getString(key);
50: } catch (MissingResourceException mre) {
51: // resource not found
52: }
53: }
54: }
55: // key unkown
56: return '!' + key + '!';
57: }
58: }
59:
60: /**
61: * Add a bundle.
62: * @param bundleName
63: */
64: public void addBundle(String bundleName) {
65: if (bundles == null) {
66: bundles = new ArrayList();
67: }
68: bundles.add(ResourceBundle.getBundle(bundleName,
69: RESOURCE_BUNDLE.getLocale()));
70: }
71:
72: }
|