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