KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > i18n > I18n


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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 any 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  * --------------------------------------------------------------------------
22  * $Id: I18n.java 572 2006-06-04 21:23:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

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

41 public final class I18n {
42
43     /**
44      * Name of the resource bundle to use.
45      */

46     private static final String JavaDoc RESOURCE_BUNDLE_NAME = "I18n";
47
48     /**
49      * Name of the default package (when a class don't have a package). This is
50      * the case for the generated classes.
51      */

52     private static final String JavaDoc NO_PACKAGE = "NO_PACKAGE";
53
54     /**
55      * List of our I18n objects.
56      */

57     private static Map JavaDoc<String JavaDoc, I18n> bundles = null;
58
59     /**
60      * Internal Resource Bundle.
61      */

62     private ResourceBundle JavaDoc resourceBundle = null;
63
64     /**
65      * Constructor (private access, use getInstance instead).
66      * @param packageName name of the package for the bundle.
67      * @param cl the classloader used to load bundle.
68      */

69     private I18n(final String JavaDoc packageName, final ClassLoader JavaDoc cl) {
70
71         String JavaDoc bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
72
73         // Resource bundle is on the form packagename.I18n.properties
74
try {
75             Locale JavaDoc locale = Locale.getDefault();
76             resourceBundle = ResourceBundle.getBundle(bundleName, locale, cl);
77         } catch (MissingResourceException JavaDoc mre) {
78             resourceBundle = null;
79         }
80     }
81
82     /**
83      * Gets the instance for a given class.
84      * @param c the class for which we want a bundle.
85      * @return I18n object.
86      */

87     public static I18n getInstance(final Class JavaDoc c) {
88         return getInstance(c, Thread.currentThread().getContextClassLoader());
89     }
90
91     /**
92      * Gets the instance for a given class.
93      * @param c the class for which we want a bundle.
94      * @param cl the classloader used to load bundle.
95      * @return I18n object.
96      */

97     public static I18n getInstance(final Class JavaDoc c, final ClassLoader JavaDoc cl) {
98         Package JavaDoc p = c.getPackage();
99         if (p != null) {
100             return getInstance(p.getName(), cl);
101         }
102         return getInstance(NO_PACKAGE, cl);
103     }
104
105     /**
106      * Gets the instance for a given package.
107      * @param packageName the package for which we want a bundle.
108      * @return I18n object.
109      */

110     public static I18n getInstance(final String JavaDoc packageName) {
111         return getInstance(packageName, Thread.currentThread().getContextClassLoader());
112     }
113
114     /**
115      * Gets the instance for a given package.
116      * @param packageName the package for which we want a bundle.
117      * @param cl the classloader used to load bundle.
118      * @return I18n object.
119      */

120     public static I18n getInstance(final String JavaDoc packageName, final ClassLoader JavaDoc cl) {
121         if (bundles == null) {
122             bundles = new HashMap JavaDoc<String JavaDoc, I18n>();
123         }
124
125         I18n i18n = bundles.get(packageName);
126         if (i18n != null) {
127             return i18n;
128         }
129
130         i18n = new I18n(packageName, cl);
131         bundles.put(packageName, i18n);
132         return i18n;
133     }
134
135     /**
136      * Gets the formatted string with the given arguments.
137      * @param key the keystring on which to apply arguments.
138      * @param objs list of argument for the formatter.
139      * @return the formatted string.
140      */

141     public String JavaDoc getMessage(final String JavaDoc key, final Object JavaDoc... objs) {
142         String JavaDoc pattern = key;
143         // No bundle, return key
144
if (resourceBundle != null) {
145             try {
146                 pattern = resourceBundle.getString(key);
147             } catch (MissingResourceException JavaDoc mre) {
148                 // key not found, use this pattern.
149
pattern = key;
150             }
151         }
152         try {
153             return MessageFormat.format(pattern, objs);
154         } catch (IllegalArgumentException JavaDoc iae) {
155             return key;
156         }
157     }
158
159 }
160
161
Popular Tags