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