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