KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > I18n


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: I18n.java,v 1.4 2004/05/28 14:02:57 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib;
28
29 import java.text.MessageFormat JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.MissingResourceException JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35
36
37 /**
38  * This class defines the way for getting message with
39  * ResourceBundle for different language
40  * Inspired from http://java.sun.com/docs/books/tutorial/i18n/
41  * @author Florent Benoit
42  */

43 public class I18n {
44
45     /**
46      * Name of the resource bundle to use
47      */

48     private static final String JavaDoc RESOURCE_BUNDLE_NAME = "I18n";
49
50     /**
51      * List of our I18n objects
52      */

53     private static Map JavaDoc bundles = null;
54
55     /**
56      * Internal Resource Bundle
57      */

58     private ResourceBundle JavaDoc resourceBundle = null;
59
60     /**
61      * Constructor (private access, use getInstance instead)
62      * @param packageName name of the package for the bundle
63      * @param cl the classloader used to load bundle
64      */

65     private I18n(String JavaDoc packageName, ClassLoader JavaDoc cl) {
66
67         String JavaDoc bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
68
69         // Resource bundle is on the form packagename.I18n.properties
70
try {
71             Locale JavaDoc locale = Locale.getDefault();
72             resourceBundle = ResourceBundle.getBundle(bundleName, locale, cl);
73         } catch (MissingResourceException JavaDoc mre) {
74                 String JavaDoc err = "Error when trying to get a ResourceBundle for package '" + packageName + "' : " + mre.getMessage();
75                 throw new IllegalStateException JavaDoc(err);
76         }
77     }
78
79     /**
80      * Gets the instance for a given class
81      * @param c the class for which we want a bundle
82      * @return I18n object
83      */

84     public static I18n getInstance(Class JavaDoc c) {
85         return getInstance(c.getPackage().getName());
86     }
87
88     /**
89      * Gets the instance for a given class
90      * @param c the class for which we want a bundle
91      * @param cl the classloader used to load bundle
92      * @return I18n object
93      */

94     public static I18n getInstance(Class JavaDoc c, ClassLoader JavaDoc cl) {
95         return getInstance(c.getPackage().getName(), cl);
96     }
97
98     /**
99      * Gets the instance for a given package
100      * @param packageName the package for which we want a bundle
101      * @return I18n object
102      */

103     public static I18n getInstance(String JavaDoc packageName) {
104         return getInstance(packageName, Thread.currentThread().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 JavaDoc packageName, ClassLoader JavaDoc cl) {
114         if (bundles == null) {
115             bundles = new HashMap JavaDoc();
116         }
117
118         I18n i18n = (I18n) bundles.get(packageName);
119         if (i18n != null) {
120             return i18n;
121         }
122
123
124         i18n = new I18n(packageName, cl);
125         bundles.put(packageName, i18n);
126         return i18n;
127     }
128
129     /**
130      * Gets the formatted string with the given arguments
131      * @param key the keystring on which to apply arguments
132      * @param args the object arguments for the formatter
133      * @return the formatted string
134      */

135     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc[] args) {
136         String JavaDoc value = getMessage(key);
137         return MessageFormat.format(value, args);
138     }
139
140
141     /**
142      * Gets the value of the given key
143      * @param key the keystring to retrieve
144      * @return the value for the given key or the key if the value can not be found
145      */

146     public String JavaDoc getMessage(String JavaDoc key) {
147         String JavaDoc ret = null;
148
149         // No bundle, return key
150
if (resourceBundle == null) {
151             return key;
152         }
153
154         try {
155             ret = resourceBundle.getString(key);
156         } catch (MissingResourceException JavaDoc mre) {
157             // key not found, return the key
158
ret = key;
159         }
160
161         return ret;
162     }
163
164     /**
165      * Gets the formatted string with the given arguments
166      * @param key the keystring on which to apply arguments
167      * @param obj the object argument for the formatter
168      * @return the formatted string
169      */

170     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc obj) {
171         return getMessage(key, new Object JavaDoc[] {obj});
172     }
173
174
175     /**
176      * Gets the formatted string with the given arguments
177      * @param key the keystring on which to apply arguments
178      * @param obj1 the first object argument for the formatter
179      * @param obj2 the second object argument for the formatter
180      * @return the formatted string
181      */

182     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc obj1, Object JavaDoc obj2) {
183         return getMessage(key, new Object JavaDoc[] {obj1, obj2 });
184     }
185
186     /**
187      * Gets the formatted string with the given arguments
188      * @param key the keystring on which to apply arguments
189      * @param obj1 the first object argument for the formatter
190      * @param obj2 the second object argument for the formatter
191      * @param obj3 the third object argument for the formatter
192      * @return the formatted string
193      */

194     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3) {
195         return getMessage(key, new Object JavaDoc[] {obj1, obj2, obj3 });
196     }
197
198
199     /**
200      * Gets the formatted string with the given arguments
201      * @param key the keystring on which to apply arguments
202      * @param obj1 argument for the formatter
203      * @param obj2 argument for the formatter
204      * @param obj3 argument for the formatter
205      * @param obj4 argument for the formatter
206      * @return the formatted string
207      */

208     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3, Object JavaDoc obj4) {
209         return getMessage(key, new Object JavaDoc[] {obj1, obj2, obj3, obj4 });
210     }
211
212
213     /**
214      * Gets the formatted string with the given arguments
215      * @param key the keystring on which to apply arguments
216      * @param obj1 argument for the formatter
217      * @param obj2 argument for the formatter
218      * @param obj3 argument for the formatter
219      * @param obj4 argument for the formatter
220      * @param obj5 argument for the formatter
221      * @return the formatted string
222      */

223     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3, Object JavaDoc obj4, Object JavaDoc obj5) {
224         return getMessage(key, new Object JavaDoc[] {obj1, obj2, obj3, obj4, obj5 });
225     }
226
227
228     /**
229      * Gets the formatted string with the given arguments
230      * @param key the keystring on which to apply arguments
231      * @param obj1 argument for the formatter
232      * @param obj2 argument for the formatter
233      * @param obj3 argument for the formatter
234      * @param obj4 argument for the formatter
235      * @param obj5 argument for the formatter
236      * @param obj6 argument for the formatter
237      * @return the formatted string
238      */

239     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3, Object JavaDoc obj4, Object JavaDoc obj5, Object JavaDoc obj6) {
240         return getMessage(key, new Object JavaDoc[] {obj1, obj2, obj3, obj4, obj5, obj6 });
241     }
242
243 }
244
Popular Tags