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