001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: I18n.java 4840 2004-05-28 14:02:57Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib;
026:
027: import java.text.MessageFormat;
028: import java.util.HashMap;
029: import java.util.Locale;
030: import java.util.Map;
031: import java.util.MissingResourceException;
032: import java.util.ResourceBundle;
033:
034: /**
035: * This class defines the way for getting message with
036: * ResourceBundle for different language
037: * Inspired from http://java.sun.com/docs/books/tutorial/i18n/
038: * @author Florent Benoit
039: */
040: public class I18n {
041:
042: /**
043: * Name of the resource bundle to use
044: */
045: private static final String RESOURCE_BUNDLE_NAME = "I18n";
046:
047: /**
048: * List of our I18n objects
049: */
050: private static Map bundles = null;
051:
052: /**
053: * Internal Resource Bundle
054: */
055: private ResourceBundle resourceBundle = null;
056:
057: /**
058: * Constructor (private access, use getInstance instead)
059: * @param packageName name of the package for the bundle
060: * @param cl the classloader used to load bundle
061: */
062: private I18n(String packageName, ClassLoader cl) {
063:
064: String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
065:
066: // Resource bundle is on the form packagename.I18n.properties
067: try {
068: Locale locale = Locale.getDefault();
069: resourceBundle = ResourceBundle.getBundle(bundleName,
070: locale, cl);
071: } catch (MissingResourceException mre) {
072: String err = "Error when trying to get a ResourceBundle for package '"
073: + packageName + "' : " + mre.getMessage();
074: throw new IllegalStateException(err);
075: }
076: }
077:
078: /**
079: * Gets the instance for a given class
080: * @param c the class for which we want a bundle
081: * @return I18n object
082: */
083: public static I18n getInstance(Class c) {
084: return getInstance(c.getPackage().getName());
085: }
086:
087: /**
088: * Gets the instance for a given class
089: * @param c the class for which we want a bundle
090: * @param cl the classloader used to load bundle
091: * @return I18n object
092: */
093: public static I18n getInstance(Class c, ClassLoader cl) {
094: return getInstance(c.getPackage().getName(), cl);
095: }
096:
097: /**
098: * Gets the instance for a given package
099: * @param packageName the package for which we want a bundle
100: * @return I18n object
101: */
102: public static I18n getInstance(String packageName) {
103: return getInstance(packageName, Thread.currentThread()
104: .getContextClassLoader());
105: }
106:
107: /**
108: * Gets the instance for a given package
109: * @param packageName the package for which we want a bundle
110: * @param cl the classloader used to load bundle
111: * @return I18n object
112: */
113: public static I18n getInstance(String packageName, ClassLoader cl) {
114: if (bundles == null) {
115: bundles = new HashMap();
116: }
117:
118: I18n i18n = (I18n) bundles.get(packageName);
119: if (i18n != null) {
120: return i18n;
121: }
122:
123: i18n = new I18n(packageName, cl);
124: bundles.put(packageName, i18n);
125: return i18n;
126: }
127:
128: /**
129: * Gets the formatted string with the given arguments
130: * @param key the keystring on which to apply arguments
131: * @param args the object arguments for the formatter
132: * @return the formatted string
133: */
134: public String getMessage(String key, Object[] args) {
135: String value = getMessage(key);
136: return MessageFormat.format(value, args);
137: }
138:
139: /**
140: * Gets the value of the given key
141: * @param key the keystring to retrieve
142: * @return the value for the given key or the key if the value can not be found
143: */
144: public String getMessage(String key) {
145: String ret = null;
146:
147: // No bundle, return key
148: if (resourceBundle == null) {
149: return key;
150: }
151:
152: try {
153: ret = resourceBundle.getString(key);
154: } catch (MissingResourceException mre) {
155: // key not found, return the key
156: ret = key;
157: }
158:
159: return ret;
160: }
161:
162: /**
163: * Gets the formatted string with the given arguments
164: * @param key the keystring on which to apply arguments
165: * @param obj the object argument for the formatter
166: * @return the formatted string
167: */
168: public String getMessage(String key, Object obj) {
169: return getMessage(key, new Object[] { obj });
170: }
171:
172: /**
173: * Gets the formatted string with the given arguments
174: * @param key the keystring on which to apply arguments
175: * @param obj1 the first object argument for the formatter
176: * @param obj2 the second object argument for the formatter
177: * @return the formatted string
178: */
179: public String getMessage(String key, Object obj1, Object obj2) {
180: return getMessage(key, new Object[] { obj1, obj2 });
181: }
182:
183: /**
184: * Gets the formatted string with the given arguments
185: * @param key the keystring on which to apply arguments
186: * @param obj1 the first object argument for the formatter
187: * @param obj2 the second object argument for the formatter
188: * @param obj3 the third object argument for the formatter
189: * @return the formatted string
190: */
191: public String getMessage(String key, Object obj1, Object obj2,
192: Object obj3) {
193: return getMessage(key, new Object[] { obj1, obj2, obj3 });
194: }
195:
196: /**
197: * Gets the formatted string with the given arguments
198: * @param key the keystring on which to apply arguments
199: * @param obj1 argument for the formatter
200: * @param obj2 argument for the formatter
201: * @param obj3 argument for the formatter
202: * @param obj4 argument for the formatter
203: * @return the formatted string
204: */
205: public String getMessage(String key, Object obj1, Object obj2,
206: Object obj3, Object obj4) {
207: return getMessage(key, new Object[] { obj1, obj2, obj3, obj4 });
208: }
209:
210: /**
211: * Gets the formatted string with the given arguments
212: * @param key the keystring on which to apply arguments
213: * @param obj1 argument for the formatter
214: * @param obj2 argument for the formatter
215: * @param obj3 argument for the formatter
216: * @param obj4 argument for the formatter
217: * @param obj5 argument for the formatter
218: * @return the formatted string
219: */
220: public String getMessage(String key, Object obj1, Object obj2,
221: Object obj3, Object obj4, Object obj5) {
222: return getMessage(key, new Object[] { obj1, obj2, obj3, obj4,
223: obj5 });
224: }
225:
226: /**
227: * Gets the formatted string with the given arguments
228: * @param key the keystring on which to apply arguments
229: * @param obj1 argument for the formatter
230: * @param obj2 argument for the formatter
231: * @param obj3 argument for the formatter
232: * @param obj4 argument for the formatter
233: * @param obj5 argument for the formatter
234: * @param obj6 argument for the formatter
235: * @return the formatted string
236: */
237: public String getMessage(String key, Object obj1, Object obj2,
238: Object obj3, Object obj4, Object obj5, Object obj6) {
239: return getMessage(key, new Object[] { obj1, obj2, obj3, obj4,
240: obj5, obj6 });
241: }
242:
243: }
|