KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > i18n > ResManager


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: ResManager.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25 package org.enhydra.i18n;
26
27
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 import com.lutris.logging.LogChannel;
33 import com.lutris.logging.Logger;
34 //import org.apache.log4j.Logger;
35

36
37 /**
38  *
39  * Some helper functions for handeling i18n issues. One instance of this class
40  * should be created for each resource bundle.<P>
41  *
42  * The ResManager is created by a call to <CODE>getResourceManager()</CODE>
43  * the parameter is the name of the package that contails the Res class.
44  * e.g. ResManager rez = getResourceBundle("org.enhydra.mypackagename");<P>
45  *
46  * To use the ResManager make a call to any of the <CODE>format()</CODE>
47  * methods. In the default resource bundle the key is the same as the value.
48  * So to display "I am 2 years old" call rez.format("I am {0} years old",2);
49  * If the string "I am {0} years old" is in the bundle the value is returned. If
50  * string is not found in the bundle the key is returned and an error is logged
51  * to I18N_DEBUG. To see these errors add DEBUG_I18N to Server.LogToFile[] and
52  * Server.LogToStderr[] in multiserver.conf.
53  *
54  *
55  * @author Peter Johnson
56  */

57
58 public class ResManager {
59
60     /**
61      * The ResourceBundle for this locale.
62      */

63
64     private ResourceBundle JavaDoc bundle;
65     private String JavaDoc bundleName;
66 // v. strahinja, 22 sep 2002
67
private LogChannel logChannel;
68 // v. strahinja, 22 sep 2002
69
private int logLevelDebug;
70 // v. strahinja, 22 sep 2002
71
private int logLevelInfo;
72
73     static private Hashtable JavaDoc bundles = new Hashtable JavaDoc();
74
75 // static Logger logger = Logger.getLogger(ResManager.class.getName());
76

77     private ResManager(String JavaDoc packageName) {
78     bundleName = packageName + ".Res";
79         // not ready yet.
80
// bundle = ResourceBundle.getBundle(bundleName);
81

82         // Add DEBUG_I18N to the multiserver.conf file to log ResManager issues
83
// v. strahinja, 22 sep 2002
84
logChannel = Logger.getCentralLogger().getChannel("Multiserver");
85 // v. strahinja, 22 sep 2002
86
logLevelDebug = logChannel.getLevel("I18N_DEBUG");
87 // v. strahinja, 22 sep 2002
88
logLevelInfo = logChannel.getLevel("I18N_INFO");
89     }
90
91
92    /**
93      * Returns a resource manager assocated with the package name.
94      * An instance of the Res class is created the first time the method is
95      * called.
96      *
97      * @param clazz A class from the package that Rez is in.
98      *
99      */

100     public static ResManager getResManager(Class JavaDoc clazz) {
101
102         // build a package name from the
103
String JavaDoc packageName = clazz.getName();
104         int lastDot = packageName.lastIndexOf('.');
105         if (lastDot != -1)
106             packageName = packageName.substring(0,lastDot);
107
108         return getResManager(packageName);
109     }
110
111    /**
112      * Returns a resource manager assocated with the package name.
113      * An instance of the Res class is created the first time the method is
114      * called.
115      *
116      * @param packageName The package name that holds the Res class
117      *
118      */

119     public static ResManager getResManager(String JavaDoc packageName) {
120         ResManager rez = (ResManager) bundles.get(packageName);
121         if (rez==null) {
122             rez = new ResManager(packageName);
123             bundles.put(packageName,rez);
124         }
125         return rez;
126     }
127
128
129     private String JavaDoc getString(String JavaDoc key) {
130         // just return the key for now.
131
return key;
132         /*
133         String msg;
134         try {
135             msg = bundle.getString(key);
136         }
137         catch (MissingResourceException ex) {
138             msg = key;
139
140             // Couldn't find the key log to I18N_INFO
141             //logChannel.write(logLevelInfo,key + " (String is missing from resource bundle \"" + bundleName+"\" )");
142         }
143         logChannel.write(logLevelDebug,key + " => " + msg);
144         return msg;
145         */

146     }
147
148
149    /**
150      * Returns a string that has been obtained from the resource manager
151      *
152      * @param key The string that is the key to the translated message
153      *
154      */

155     public String JavaDoc format(String JavaDoc key) {
156         return getString(key);
157     }
158
159    /**
160      * Returns a string that has been obtained from the resource manager then
161      * formatted using the passed parameters.
162      *
163      * @param key The string that is the key to the translated message
164      * @param o0 The param passed to format replaces {0}
165      *
166      */

167     public String JavaDoc format(String JavaDoc pattern, Object JavaDoc o0) {
168         return MessageFormat.format(getString(pattern), new Object JavaDoc[] {o0});
169     }
170
171    /**
172      * Returns a string that has been obtained from the resource manager then
173      * formatted using the passed parameters.
174      *
175      * @param key The string that is the key to the translated message
176      * @param o0 The param passed to format replaces {0}
177      * @param o1 The param passed to format replaces {1}
178      *
179      */

180     public String JavaDoc format(String JavaDoc pattern, Object JavaDoc o0, Object JavaDoc o1) {
181         return MessageFormat.format(getString(pattern), new Object JavaDoc[] {o0,o1});
182     }
183
184
185    /**
186      * Returns a string that has been obtained from the resource manager then
187      * formatted using the passed parameters.
188      *
189      * @param key The string that is the key to the translated message
190      * @param o0 The param passed to format replaces {0}
191      * @param o1 The param passed to format replaces {1}
192      * @param o2 The param passed to format replaces {2}
193      *
194      */

195     public String JavaDoc format(String JavaDoc pattern, Object JavaDoc o0, Object JavaDoc o1, Object JavaDoc o2) {
196         return MessageFormat.format(getString(pattern), new Object JavaDoc[] {o0,o1,o2});
197     }
198
199    /**
200      * Returns a string that has been obtained from the resource manager then
201      * formatted using the passed parameters.
202      *
203      * @param key The string that is the key to the translated message
204      * @param o0 The param passed to format replaces {0}
205      * @param o1 The param passed to format replaces {1}
206      * @param o2 The param passed to format replaces {2}
207      * @param o3 The param passed to format replaces {3}
208      *
209      */

210     public String JavaDoc format(String JavaDoc pattern, Object JavaDoc o0, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
211         return MessageFormat.format(getString(pattern), new Object JavaDoc[] {o0,o1,o2,o3});
212     }
213
214     // add more if you need them...
215
}
216
217
Popular Tags