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.binding.shared.util;
030:
031: import java.text.MessageFormat;
032:
033: import java.util.Locale;
034: import java.util.MissingResourceException;
035: import java.util.ResourceBundle;
036:
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 StringTranslator {
046: /**
047: * Logger name
048: */
049: private static final String LOGGER_NAME = "com.sun.jbi.binding.jms";
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: * 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 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: public StringTranslator(String packageName, ClassLoader classLoader) {
125: mLog = Logger.getLogger(LOGGER_NAME);
126:
127: String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
128: mDefaultLocale = Locale.getDefault();
129:
130: try {
131: if (null == classLoader) {
132: mResourceBundle = ResourceBundle.getBundle(bundleName);
133: } else {
134: mResourceBundle = ResourceBundle.getBundle(bundleName,
135: mDefaultLocale, classLoader);
136: }
137: } catch (java.util.MissingResourceException mrEx) {
138: mLog.warning(MessageFormat.format(
139: LOG_UNABLE_TO_LOAD_BUNDLE, new Object[] {
140: bundleName, mDefaultLocale, mrEx }));
141: }
142: }
143:
144: /**
145: * Get a localized string using the specified resource key.
146: *
147: * @param key - the key to the localized string in the resource bundle.
148: *
149: * @return the localized string.
150: */
151: public String getString(String key) {
152: Object[] inserts = new Object[0];
153:
154: return getString(key, inserts);
155: }
156:
157: /**
158: * Get a localized string using the specified resource key. Handle one message
159: * insert.
160: *
161: * @param key - the key to the localized string in the resource bundle.
162: * @param insert1 - the message insert.
163: *
164: * @return the localized string formatted with the message insert.
165: */
166: public String getString(String key, Object insert1) {
167: Object[] inserts = { insert1 };
168:
169: return getString(key, inserts);
170: }
171:
172: /**
173: * Get a localized string using the specified resource key. Handle two message
174: * inserts.
175: *
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: *
180: * @return the localized string formatted with the message inserts.
181: */
182: public String getString(String key, Object insert1, Object insert2) {
183: Object[] inserts = { insert1, insert2 };
184:
185: return getString(key, inserts);
186: }
187:
188: /**
189: * Get a localized string using the specified resource key. Handle three message
190: * inserts.
191: *
192: * @param key - the key to the localized string in the resource bundle.
193: * @param insert1 - the first message insert.
194: * @param insert2 - the second message insert.
195: * @param insert3 - the third message insert.
196: *
197: * @return the localized string formatted with the message inserts.
198: */
199: public String getString(String key, Object insert1, Object insert2,
200: Object insert3) {
201: Object[] inserts = { insert1, insert2, insert3 };
202:
203: return getString(key, inserts);
204: }
205:
206: /**
207: * Get a localized string using the specified resource key. Handle four message
208: * inserts.
209: *
210: * @param key - the key to the localized string in the resource bundle.
211: * @param insert1 - the first message insert.
212: * @param insert2 - the second message insert.
213: * @param insert3 - the third message insert.
214: * @param insert4 - the fourth message insert.
215: *
216: * @return the localized string formatted with the message inserts.
217: */
218: public String getString(String key, Object insert1, Object insert2,
219: Object insert3, Object insert4) {
220: Object[] inserts = { insert1, insert2, insert3, insert4 };
221:
222: return getString(key, inserts);
223: }
224:
225: /**
226: * Get a localized string using the specified resource key. Handle any number of
227: * message inserts. This method is called by all the other getString() methods in
228: * the class. The procedure for string lookup is to first look in the primary
229: * resource bundle, then in the fallback resource bundle. If the string is found in
230: * the primary, return the translated string quietly. If the string is not found in
231: * the primary but in the fallback, log a warning and return the translated string.
232: * If the string is not found in either bundle, log an error and return a message
233: * formatted with the key and insert values provided by the caller. If there is no
234: * resource bundle available, just return a message formatted with the key and
235: * insert values provided by the caller.
236: *
237: * @param key - the key to the localized string in the resource bundle.
238: * @param inserts - the array of message inserts.
239: *
240: * @return the localized string formatted with the message inserts.
241: */
242: public String getString(String key, Object[] inserts) {
243: String translated = null;
244:
245: if (null != mResourceBundle) {
246: try {
247: translated = mResourceBundle.getString(key);
248: translated = MessageFormat.format(translated, inserts);
249: } catch (java.util.MissingResourceException mrEx) {
250: String fi = formatInserts(inserts);
251: translated = MessageFormat.format(MSG_NO_TRANSLATION,
252: new Object[] { key, fi });
253: mLog.warning(MessageFormat.format(
254: LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
255: new Object[] { key, mDefaultLocale, fi }));
256: }
257: } else {
258: translated = MessageFormat.format(MSG_NO_TRANSLATION,
259: new Object[] { key, formatInserts(inserts) });
260: }
261:
262: return translated;
263: }
264:
265: /**
266: * Format an array of message inserts into a string. The ouptut string is in the
267: * format "insert1,insert2,....,insertn".
268: *
269: * @param inserts - the array of message inserts.
270: *
271: * @return the string formatted with the message inserts.
272: */
273: private String formatInserts(Object[] inserts) {
274: StringBuffer formatted = new StringBuffer("");
275:
276: for (int i = 0; i < inserts.length; i++) {
277: if (i > 0) {
278: formatted.append(",");
279: }
280:
281: formatted.append(inserts[i].toString());
282: }
283:
284: return formatted.toString();
285: }
286: }
|