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.xslt.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: import com.sun.jbi.engine.xslt.TransformationEngineContext;
038:
039: /**
040: * This is the implementation of the String Translator, which provides services
041: * for internationalization of messages to all services running inside the JBI
042: * environment.
043: *
044: * @author Sun Microsystems, Inc.
045: */
046: public class StringTranslator {
047: /**
048: * Logger name
049: */
050: private static final String LOGGER_NAME = "com.sun.jbi.engine.xslt";
051:
052: /**
053: * Unqualified name for resource bundles.
054: */
055: public static final String RESOURCE_BUNDLE_NAME = "TEResources";
056:
057: /**
058: * Log message for creation of new instance.
059: */
060: private static final String LOG_NEW_INSTANCE = "New StringTranslator for package {0}, classLoader is {1}";
061:
062: /**
063: * Log message for locale.
064: */
065: private static final String LOG_CURRENT_LOCALE = "Current locale is {0}";
066:
067: /**
068: * Log message for failure loading resource bundle.
069: */
070: private static final String LOG_UNABLE_TO_LOAD_BUNDLE = "Unable to load resource bundle {0} for locale {1}: {2}";
071:
072: /**
073: * Log message for using alternate resource bundle.
074: */
075: private static final String LOG_USING_BUNDLE = "Using resource bundle for locale {0} instead.";
076:
077: /**
078: * Log message for using fallback resource bundle to look up a message.
079: */
080: private static final String LOG_TRANSLATION_USING_FALLBACK = "No translation for key={0} found in resource bundle for locale {1}, "
081: + "using locale {2} instead.";
082:
083: /**
084: * Log message for no translation available for a message key in any
085: * resource bundle.
086: */
087: private static final String LOG_NO_TRANSLATION_FOR_KEY = "No translation for key={0} found in any resource bundle. "
088: + "Insert data is [{1}].";
089:
090: /**
091: * Log message for no translation available for a message key in a
092: * particular resource bundle.
093: */
094: private static final String LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE = "No translation for key={0} found in resource bundle for locale {1}. "
095: + "Insert data is [{2}].";
096:
097: /**
098: * Message text used when no translation is available for a message key.
099: */
100: private static final String MSG_NO_TRANSLATION = "No translation available for message with key={0} and inserts=[{1}].";
101:
102: /**
103: * The default locale at the time this StringTranslator was created.
104: */
105: private Locale mDefaultLocale;
106:
107: /**
108: * Logger for this instance
109: */
110: private Logger mLog;
111:
112: /**
113: * ResourceBundle for a single package name.
114: */
115: private ResourceBundle mResourceBundle;
116:
117: /**
118: * Constructor. This loads the Resource Bundle for the current locale, and
119: * if the current locale is not Locale.US, it loads the Resource Bundle
120: * for Locale.US and stores it as the backup for string lookup.
121: *
122: * @param packageName - the name of the package that contains the resource
123: * bundle.
124: * @param classLoader - the class loader to be used for loading the
125: * resource bundle. If this parameter is null, the current class
126: * loader is used.
127: */
128: public StringTranslator(String packageName, ClassLoader classLoader) {
129: mLog = TransformationEngineContext.getInstance().getLogger("");
130:
131: String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
132: mDefaultLocale = Locale.getDefault();
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
163: * message 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
178: * message 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
194: * message 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
212: * message 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
231: * number of message inserts. This method is called by all the other
232: * getString() methods in the class. The procedure for string lookup is to
233: * first look in the primary resource bundle, then in the fallback
234: * resource bundle. If the string is found in the primary, return the
235: * translated string quietly. If the string is not found in the primary
236: * but in the fallback, log a warning and return the translated string. If
237: * the string is not found in either bundle, log an error and return a
238: * message formatted with the key and insert values provided by the
239: * caller. If there is no resource bundle available, just return a message
240: * formatted with the key and insert values provided by the caller.
241: *
242: * @param key - the key to the localized string in the resource bundle.
243: * @param inserts - the array of message inserts.
244: *
245: * @return the localized string formatted with the message inserts.
246: */
247: public String getString(String key, Object[] inserts) {
248: String translated = null;
249:
250: if (null != mResourceBundle) {
251: try {
252: translated = mResourceBundle.getString(key);
253: translated = MessageFormat.format(translated, inserts);
254: } catch (java.util.MissingResourceException mrEx) {
255:
256: String fi = formatInserts(inserts);
257: translated = MessageFormat.format(MSG_NO_TRANSLATION,
258: new Object[] { key, fi });
259: mLog.warning(MessageFormat.format(
260: LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
261: new Object[] { key, mDefaultLocale, fi }));
262:
263: }
264: } else {
265: translated = MessageFormat.format(MSG_NO_TRANSLATION,
266: new Object[] { key, formatInserts(inserts) });
267: }
268:
269: return translated;
270: }
271:
272: /**
273: * Format an array of message inserts into a string. The ouptut string is
274: * in the format "insert1,insert2,....,insertn".
275: *
276: * @param inserts - the array of message inserts.
277: *
278: * @return the string formatted with the message inserts.
279: */
280: private String formatInserts(Object[] inserts) {
281: StringBuffer formatted = new StringBuffer("");
282:
283: for (int i = 0; i < inserts.length; i++) {
284: if (i > 0) {
285: formatted.append(",");
286: }
287:
288: formatted.append(inserts[i].toString());
289: }
290:
291: return formatted.toString();
292: }
293: }
|