001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)StringTranslator.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.util;
030:
031: import java.text.MessageFormat;
032: import java.util.Locale;
033: import java.util.logging.Logger;
034: import java.util.ResourceBundle;
035:
036: /**
037: * This is the implementation of the String Translator, which provides services
038: * for internationalization of messages to all services running inside the
039: * JBI environment.
040: *
041: * @author Sun Microsystems, Inc.
042: */
043: public class StringTranslator {
044: /**
045: * Logger name
046: */
047: private static final String LOGGER_NAME = "com.sun.jbi.util";
048:
049: /**
050: * Unqualified name for resource bundles.
051: */
052: public static final String RESOURCE_BUNDLE_NAME = "LocalStrings";
053:
054: /**
055: * Log message for creation of new instance.
056: */
057: private static final String LOG_NEW_INSTANCE = "New StringTranslator for package {0}, classLoader is {1}";
058:
059: /**
060: * Log message for locale.
061: */
062: private static final String LOG_CURRENT_LOCALE = "Current locale is {0}";
063:
064: /**
065: * Log message for failure loading resource bundle.
066: */
067: private static final String LOG_UNABLE_TO_LOAD_BUNDLE = "Unable to load resource bundle {0} for locale {1}: {2}";
068:
069: /**
070: * Log message for using alternate resource bundle.
071: */
072: private static final String LOG_USING_BUNDLE = "Using resource bundle for locale {0} instead.";
073:
074: /**
075: * Log message for using fallback resource bundle to look up a message.
076: */
077: private static final String LOG_TRANSLATION_USING_FALLBACK = "No translation for key={0} found in resource bundle for locale {1}, "
078: + "using locale {2} instead.";
079: /**
080: * Log message for no translation available for a message key in any
081: * resource bundle.
082: */
083: private static final String LOG_NO_TRANSLATION_FOR_KEY = "No translation for key={0} found in any resource bundle. "
084: + "Insert data is [{1}].";
085:
086: /**
087: * Log message for no translation available for a message key in a
088: * particular resource bundle.
089: */
090: private static final String LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE = "No translation for key={0} found in resource bundle for locale {1}. "
091: + "Insert data is [{2}].";
092:
093: /**
094: * Message text used when no translation is available for a message key.
095: */
096: private static final String MSG_NO_TRANSLATION = "No translation available for message with key={0} and inserts=[{1}].";
097:
098: /**
099: * Logger for this class.
100: */
101: private static Logger sLog;
102:
103: /**
104: * The default locale at the time the first StringTranslator was created.
105: */
106: private static Locale sDefaultLocale;
107:
108: /**
109: * ResourceBundle for a single package name.
110: */
111: private ResourceBundle mResourceBundle;
112:
113: /**
114: * Static initializer. This sets up the logger and the default locale.
115: */
116: static {
117: sLog = Logger.getLogger(LOGGER_NAME);
118: sDefaultLocale = Locale.getDefault();
119: sLog.finest(MessageFormat.format(LOG_CURRENT_LOCALE,
120: new Object[] { sDefaultLocale }));
121: }
122:
123: /**
124: * Constructor. This loads the Resource Bundle for the current locale,
125: * and if the current locale is not Locale.US, it loads the Resource
126: * Bundle for Locale.US and stores it as the backup for string lookup.
127: * @param packageName - the name of the package that contains the resource
128: * bundle.
129: * @param classLoader - the class loader to be used for loading the resource
130: * bundle. If this parameter is null, the current class loader is used.
131: */
132: public StringTranslator(String packageName, ClassLoader classLoader) {
133: sLog.finest(MessageFormat.format(LOG_NEW_INSTANCE,
134: new Object[] { packageName, classLoader }));
135: String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
136:
137: try {
138: if (null == classLoader) {
139: mResourceBundle = ResourceBundle.getBundle(bundleName);
140: } else {
141: mResourceBundle = ResourceBundle.getBundle(bundleName,
142: sDefaultLocale, classLoader);
143: }
144: } catch (java.util.MissingResourceException mrEx) {
145: sLog.warning(MessageFormat.format(
146: LOG_UNABLE_TO_LOAD_BUNDLE, new Object[] {
147: bundleName, sDefaultLocale, mrEx }));
148: }
149: }
150:
151: /**
152: * Get a localized string using the specified resource key.
153: * @param key - the key to the localized string in the resource bundle.
154: * @return the localized string.
155: */
156: public String getString(String key) {
157: Object[] inserts = new Object[0];
158: return getString(key, inserts);
159: }
160:
161: /**
162: * Get a localized string using the specified resource key. Handle one
163: * message insert.
164: * @param key - the key to the localized string in the resource bundle.
165: * @param insert1 - the message insert.
166: * @return the localized string formatted with the message insert.
167: */
168: public String getString(String key, Object insert1) {
169: Object[] inserts = { insert1 };
170: return getString(key, inserts);
171: }
172:
173: /**
174: * Get a localized string using the specified resource key. Handle two
175: * message inserts.
176: * @param key - the key to the localized string in the resource bundle.
177: * @param insert1 - the first message insert.
178: * @param insert2 - the second message insert.
179: * @return the localized string formatted with the message inserts.
180: */
181: public String getString(String key, Object insert1, Object insert2) {
182: Object[] inserts = { insert1, insert2 };
183: return getString(key, inserts);
184: }
185:
186: /**
187: * Get a localized string using the specified resource key. Handle three
188: * message inserts.
189: * @param key - the key to the localized string in the resource bundle.
190: * @param insert1 - the first message insert.
191: * @param insert2 - the second message insert.
192: * @param insert3 - the third message insert.
193: * @return the localized string formatted with the message inserts.
194: */
195: public String getString(String key, Object insert1, Object insert2,
196: Object insert3) {
197: Object[] inserts = { insert1, insert2, insert3 };
198: return getString(key, inserts);
199: }
200:
201: /**
202: * Get a localized string using the specified resource key. Handle four
203: * message inserts.
204: * @param key - the key to the localized string in the resource bundle.
205: * @param insert1 - the first message insert.
206: * @param insert2 - the second message insert.
207: * @param insert3 - the third message insert.
208: * @param insert4 - the fourth message insert.
209: * @return the localized string formatted with the message inserts.
210: */
211: public String getString(String key, Object insert1, Object insert2,
212: Object insert3, Object insert4) {
213: Object[] inserts = { insert1, insert2, insert3, insert4 };
214: return getString(key, inserts);
215: }
216:
217: /**
218: * Get a localized string using the specified resource key. Handle five
219: * message inserts.
220: * @param key - the key to the localized string in the resource bundle.
221: * @param insert1 - the first message insert.
222: * @param insert2 - the second message insert.
223: * @param insert3 - the third message insert.
224: * @param insert4 - the fourth message insert.
225: * @param insert5 - the fifth message insert.
226: * @return the localized string formatted with the message inserts.
227: */
228: public String getString(String key, Object insert1, Object insert2,
229: Object insert3, Object insert4, Object insert5) {
230: Object[] inserts = { insert1, insert2, insert3, insert4,
231: insert5 };
232: return getString(key, inserts);
233: }
234:
235: /**
236: * Get a localized string using the specified resource key. Handle any
237: * number of message inserts. This method is called by all the other
238: * getString() methods in the class. The procedure for string lookup is
239: * to first look in the primary resource bundle, then in the fallback
240: * resource bundle. If the string is found in the primary, return the
241: * translated string quietly. If the string is not found in the primary
242: * but in the fallback, log a warning and return the translated string.
243: * If the string is not found in either bundle, log an error and return
244: * a message formatted with the key and insert values provided by the
245: * caller. If there is no resource bundle available, just return a message
246: * formatted with the key and insert values provided by the caller.
247: * @param key - the key to the localized string in the resource bundle.
248: * @param inserts - the array of message inserts.
249: * @return the localized string formatted with the message inserts.
250: */
251: public String getString(String key, Object[] inserts) {
252: String translated = null;
253:
254: if (null != mResourceBundle) {
255: try {
256: translated = mResourceBundle.getString(key);
257: translated = MessageFormat.format(translated, inserts);
258: } catch (java.util.MissingResourceException mrEx) {
259: String fi = formatInserts(inserts);
260: translated = MessageFormat.format(MSG_NO_TRANSLATION,
261: new Object[] { key, fi });
262: sLog.warning(MessageFormat.format(
263: LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
264: new Object[] { key, sDefaultLocale, fi }));
265: }
266: } else {
267: translated = MessageFormat.format(MSG_NO_TRANSLATION,
268: new Object[] { key, formatInserts(inserts) });
269: }
270:
271: return translated;
272: }
273:
274: /**
275: * Format an array of message inserts into a string. The ouptut string is
276: * in the format "insert1,insert2,....,insertn".
277: * @param inserts - the array of message inserts.
278: * @return the string formatted with the message inserts.
279: */
280: private String formatInserts(Object[] inserts) {
281: StringBuffer formatted = new StringBuffer("");
282: for (int i = 0; i < inserts.length; i++) {
283: if (i > 0) {
284: formatted.append(",");
285: }
286: formatted.append(inserts[i].toString());
287: }
288: return formatted.toString();
289: }
290: }
|