001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: ResManager.java,v 1.3 2007-10-19 10:05:39 sinisa Exp $
022: */
023:
024: package org.enhydra.i18n;
025:
026: import java.text.MessageFormat;
027: import java.util.Hashtable;
028: import java.util.ResourceBundle;
029:
030: import com.lutris.logging.LogChannel;
031: import com.lutris.logging.Logger;
032:
033: //import org.apache.log4j.Logger;
034:
035: /**
036: *
037: * Some helper functions for handeling i18n issues. One instance of this class
038: * should be created for each resource bundle.<P>
039: *
040: * The ResManager is created by a call to <CODE>getResourceManager()</CODE>
041: * the parameter is the name of the package that contails the Res class.
042: * e.g. ResManager rez = getResourceBundle("org.enhydra.mypackagename");<P>
043: *
044: * To use the ResManager make a call to any of the <CODE>format()</CODE>
045: * methods. In the default resource bundle the key is the same as the value.
046: * So to display "I am 2 years old" call rez.format("I am {0} years old",2);
047: * If the string "I am {0} years old" is in the bundle the value is returned. If
048: * string is not found in the bundle the key is returned and an error is logged
049: * to I18N_DEBUG. To see these errors add DEBUG_I18N to Server.LogToFile[] and
050: * Server.LogToStderr[] in multiserver.conf.
051: *
052: *
053: * @author Peter Johnson
054: */
055:
056: public class ResManager {
057:
058: /**
059: * The ResourceBundle for this locale.
060: */
061:
062: // private ResourceBundle bundle;
063: // private String bundleName;
064: // v. strahinja, 22 sep 2002
065: // private LogChannel logChannel;
066: // v. strahinja, 22 sep 2002
067: // private int logLevelDebug;
068: // v. strahinja, 22 sep 2002
069: // private int logLevelInfo;
070: static private Hashtable bundles = new Hashtable();
071:
072: // static Logger logger = Logger.getLogger(ResManager.class.getName());
073:
074: private ResManager(String packageName) {
075: // bundleName = packageName + ".Res";
076: // not ready yet.
077: // bundle = ResourceBundle.getBundle(bundleName);
078:
079: // Add DEBUG_I18N to the multiserver.conf file to log ResManager issues
080: // v. strahinja, 22 sep 2002
081: // logChannel = Logger.getCentralLogger().getChannel("Multiserver");
082: // v. strahinja, 22 sep 2002
083: // logLevelDebug = logChannel.getLevel("I18N_DEBUG");
084: // v. strahinja, 22 sep 2002
085: // logLevelInfo = logChannel.getLevel("I18N_INFO");
086: }
087:
088: /**
089: * Returns a resource manager assocated with the package name.
090: * An instance of the Res class is created the first time the method is
091: * called.
092: *
093: * @param clazz A class from the package that Rez is in.
094: *
095: */
096: public static ResManager getResManager(Class clazz) {
097:
098: // build a package name from the
099: String packageName = clazz.getName();
100: int lastDot = packageName.lastIndexOf('.');
101: if (lastDot != -1)
102: packageName = packageName.substring(0, lastDot);
103:
104: return getResManager(packageName);
105: }
106:
107: /**
108: * Returns a resource manager assocated with the package name.
109: * An instance of the Res class is created the first time the method is
110: * called.
111: *
112: * @param packageName The package name that holds the Res class
113: *
114: */
115: public static ResManager getResManager(String packageName) {
116: ResManager rez = (ResManager) bundles.get(packageName);
117: if (rez == null) {
118: rez = new ResManager(packageName);
119: bundles.put(packageName, rez);
120: }
121: return rez;
122: }
123:
124: private String getString(String key) {
125: // just return the key for now.
126: return key;
127: /*
128: String msg;
129: try {
130: msg = bundle.getString(key);
131: }
132: catch (MissingResourceException ex) {
133: msg = key;
134:
135: // Couldn't find the key log to I18N_INFO
136: //logChannel.write(logLevelInfo,key + " (String is missing from resource bundle \"" + bundleName+"\" )");
137: }
138: logChannel.write(logLevelDebug,key + " => " + msg);
139: return msg;
140: */
141: }
142:
143: /**
144: * Returns a string that has been obtained from the resource manager
145: *
146: * @param key The string that is the key to the translated message
147: *
148: */
149: public String format(String key) {
150: return getString(key);
151: }
152:
153: /**
154: * Returns a string that has been obtained from the resource manager then
155: * formatted using the passed parameters.
156: *
157: * @param key The string that is the key to the translated message
158: * @param o0 The param passed to format replaces {0}
159: *
160: */
161: public String format(String pattern, Object o0) {
162: return MessageFormat.format(getString(pattern),
163: new Object[] { o0 });
164: }
165:
166: /**
167: * Returns a string that has been obtained from the resource manager then
168: * formatted using the passed parameters.
169: *
170: * @param key The string that is the key to the translated message
171: * @param o0 The param passed to format replaces {0}
172: * @param o1 The param passed to format replaces {1}
173: *
174: */
175: public String format(String pattern, Object o0, Object o1) {
176: return MessageFormat.format(getString(pattern), new Object[] {
177: o0, o1 });
178: }
179:
180: /**
181: * Returns a string that has been obtained from the resource manager then
182: * formatted using the passed parameters.
183: *
184: * @param key The string that is the key to the translated message
185: * @param o0 The param passed to format replaces {0}
186: * @param o1 The param passed to format replaces {1}
187: * @param o2 The param passed to format replaces {2}
188: *
189: */
190: public String format(String pattern, Object o0, Object o1, Object o2) {
191: return MessageFormat.format(getString(pattern), new Object[] {
192: o0, o1, o2 });
193: }
194:
195: /**
196: * Returns a string that has been obtained from the resource manager then
197: * formatted using the passed parameters.
198: *
199: * @param key The string that is the key to the translated message
200: * @param o0 The param passed to format replaces {0}
201: * @param o1 The param passed to format replaces {1}
202: * @param o2 The param passed to format replaces {2}
203: * @param o3 The param passed to format replaces {3}
204: *
205: */
206: public String format(String pattern, Object o0, Object o1,
207: Object o2, Object o3) {
208: return MessageFormat.format(getString(pattern), new Object[] {
209: o0, o1, o2, o3 });
210: }
211:
212: // add more if you need them...
213: }
|