KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jbi > serviceengine > util > soap > StringTranslator


1 // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
2
/*
3  * StringTranslator.java
4  *
5  * SUN PROPRIETARY/CONFIDENTIAL.
6  * This software is the proprietary information of Sun Microsystems, Inc.
7  * Use is subject to license terms.
8  *
9  */

10 package com.sun.enterprise.jbi.serviceengine.util.soap;
11
12 import java.text.MessageFormat JavaDoc;
13
14 import java.util.Locale JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16 import java.util.logging.Logger JavaDoc;
17
18
19 /**
20  * This is the implementation of the String Translator, which provides services
21  * for internationalization of messages to all services running inside the JBI
22  * environment.
23  *
24  * @author Sun Microsystems Inc.
25  */

26 public class StringTranslator
27 {
28     /**
29      * Logger name
30      */

31     private static final String JavaDoc LOGGER_NAME = "com.sun.jbi.common.soap";
32
33     /**
34      * Unqualified name for resource bundles.
35      */

36     public static final String JavaDoc RESOURCE_BUNDLE_NAME = "LocalStrings";
37
38     /**
39      * Log message for creation of new instance.
40      */

41     private static final String JavaDoc LOG_NEW_INSTANCE =
42         "New StringTranslator for package {0}, classLoader is {1}";
43
44     /**
45      * Log message for locale.
46      */

47     private static final String JavaDoc LOG_CURRENT_LOCALE = "Current locale is {0}";
48
49     /**
50      * Log message for failure loading resource bundle.
51      */

52     private static final String JavaDoc LOG_UNABLE_TO_LOAD_BUNDLE =
53         "Unable to load resource bundle {0} for locale {1}: {2}";
54
55     /**
56      * Log message for using alternate resource bundle.
57      */

58     private static final String JavaDoc LOG_USING_BUNDLE =
59         "Using resource bundle for locale {0} instead.";
60
61     /**
62      * Log message for using fallback resource bundle to look up a message.
63      */

64     private static final String JavaDoc LOG_TRANSLATION_USING_FALLBACK =
65         "No translation for key={0} found in resource bundle for locale {1}, "
66         + "using locale {2} instead.";
67
68     /**
69      * Log message for no translation available for a message key in any
70      * resource bundle.
71      */

72     private static final String JavaDoc LOG_NO_TRANSLATION_FOR_KEY =
73         "No translation for key={0} found in any resource bundle. "
74         + "Insert data is [{1}].";
75
76     /**
77      * Log message for no translation available for a message key in a
78      * particular resource bundle.
79      */

80     private static final String JavaDoc LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE =
81         "No translation for key={0} found in resource bundle for locale {1}. "
82         + "Insert data is [{2}].";
83
84     /**
85      * Message text used when no translation is available for a message key.
86      */

87     private static final String JavaDoc MSG_NO_TRANSLATION =
88         "No translation available for message with key={0} and inserts=[{1}].";
89
90     /**
91      * The default locale at the time this StringTranslator was created.
92      */

93     private Locale JavaDoc mDefaultLocale;
94
95     /**
96      * Logger for this instance
97      */

98     private Logger JavaDoc mLog;
99
100     /**
101      * Fallback ResourceBundle for a single package name. This is used only
102      * when the default locale is something other than Locale.US. In that
103      * case, this is the bundle for Locale.US for looking up messages that are
104      * not found in the default locale.
105      */

106     private ResourceBundle JavaDoc mFallbackBundle;
107
108     /**
109      * ResourceBundle for a single package name.
110      */

111     private ResourceBundle JavaDoc mResourceBundle;
112
113     /**
114      * Constructor. This loads the Resource Bundle for the current locale, and
115      * if the current locale is not Locale.US, it loads the Resource Bundle
116      * for Locale.US and stores it as the backup for string lookup.
117      *
118      * @param packageName - the name of the package that contains the resource
119      * bundle.
120      * @param classLoader - the class loader to be used for loading the
121      * resource bundle. If this parameter is null, the current class
122      * loader is used.
123      */

124     public StringTranslator(
125         String JavaDoc packageName,
126         ClassLoader JavaDoc classLoader)
127     {
128         mLog = Logger.getLogger(packageName);
129
130         String JavaDoc bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
131         mDefaultLocale = Locale.getDefault();
132
133         // Always load the bundle for english
134
mFallbackBundle = null;
135
136         ResourceBundle JavaDoc englishBundle = null;
137
138         try
139         {
140             if (null == classLoader)
141             {
142                 englishBundle = ResourceBundle.getBundle(bundleName, Locale.US);
143             }
144             else
145             {
146                 englishBundle =
147                     ResourceBundle.getBundle(bundleName, Locale.US, classLoader);
148             }
149         }
150         catch (java.util.MissingResourceException JavaDoc mrEx)
151         {
152             mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE,
153                     new Object JavaDoc [] {bundleName, Locale.US, mrEx}));
154         }
155
156         // If the default locale is English, set it as the primary bundle.
157
// If the default locale is not English, attempt to load the bundle.
158
// If it is found, save it as the primary and set the fallback to
159
// English. If it is not found, set the primary to English.
160
if (mDefaultLocale.equals(Locale.US))
161         {
162             mResourceBundle = englishBundle;
163         }
164         else
165         {
166             try
167             {
168                 if (null == classLoader)
169                 {
170                     mResourceBundle = ResourceBundle.getBundle(bundleName);
171                     mFallbackBundle = englishBundle;
172                 }
173                 else
174                 {
175                     mResourceBundle =
176                         ResourceBundle.getBundle(bundleName, mDefaultLocale,
177                             classLoader);
178                     mFallbackBundle = englishBundle;
179                 }
180             }
181             catch (java.util.MissingResourceException JavaDoc mrEx)
182             {
183                 mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE,
184                         new Object JavaDoc [] {bundleName, mDefaultLocale, mrEx}));
185                 mLog.warning(MessageFormat.format(LOG_USING_BUNDLE,
186                         new Object JavaDoc [] {Locale.US}));
187                 mResourceBundle = englishBundle;
188             }
189         }
190     }
191
192     /**
193      * Get a localized string using the specified resource key.
194      *
195      * @param key - the key to the localized string in the resource bundle.
196      *
197      * @return the localized string.
198      */

199     public String JavaDoc getString(String JavaDoc key)
200     {
201         Object JavaDoc [] inserts = new Object JavaDoc[0];
202
203         return getString(key, inserts);
204     }
205
206     /**
207      * Get a localized string using the specified resource key. Handle one
208      * message insert.
209      *
210      * @param key - the key to the localized string in the resource bundle.
211      * @param insert1 - the message insert.
212      *
213      * @return the localized string formatted with the message insert.
214      */

215     public String JavaDoc getString(
216         String JavaDoc key,
217         Object JavaDoc insert1)
218     {
219         Object JavaDoc [] inserts = {insert1};
220
221         return getString(key, inserts);
222     }
223
224     /**
225      * Get a localized string using the specified resource key. Handle two
226      * message inserts.
227      *
228      * @param key - the key to the localized string in the resource bundle.
229      * @param insert1 - the first message insert.
230      * @param insert2 - the second message insert.
231      *
232      * @return the localized string formatted with the message inserts.
233      */

234     public String JavaDoc getString(
235         String JavaDoc key,
236         Object JavaDoc insert1,
237         Object JavaDoc insert2)
238     {
239         Object JavaDoc [] inserts = {insert1, insert2};
240
241         return getString(key, inserts);
242     }
243
244     /**
245      * Get a localized string using the specified resource key. Handle three
246      * message inserts.
247      *
248      * @param key - the key to the localized string in the resource bundle.
249      * @param insert1 - the first message insert.
250      * @param insert2 - the second message insert.
251      * @param insert3 - the third message insert.
252      *
253      * @return the localized string formatted with the message inserts.
254      */

255     public String JavaDoc getString(
256         String JavaDoc key,
257         Object JavaDoc insert1,
258         Object JavaDoc insert2,
259         Object JavaDoc insert3)
260     {
261         Object JavaDoc [] inserts = {insert1, insert2, insert3};
262
263         return getString(key, inserts);
264     }
265
266     /**
267      * Get a localized string using the specified resource key. Handle four
268      * message inserts.
269      *
270      * @param key - the key to the localized string in the resource bundle.
271      * @param insert1 - the first message insert.
272      * @param insert2 - the second message insert.
273      * @param insert3 - the third message insert.
274      * @param insert4 - the fourth message insert.
275      *
276      * @return the localized string formatted with the message inserts.
277      */

278     public String JavaDoc getString(
279         String JavaDoc key,
280         Object JavaDoc insert1,
281         Object JavaDoc insert2,
282         Object JavaDoc insert3,
283         Object JavaDoc insert4)
284     {
285         Object JavaDoc [] inserts = {insert1, insert2, insert3, insert4};
286
287         return getString(key, inserts);
288     }
289
290     /**
291      * Get a localized string using the specified resource key. Handle any
292      * number of message inserts. This method is called by all the other
293      * getString() methods in the class. The procedure for string lookup is to
294      * first look in the primary resource bundle, then in the fallback
295      * resource bundle. If the string is found in the primary, return the
296      * translated string quietly. If the string is not found in the primary
297      * but in the fallback, log a warning and return the translated string. If
298      * the string is not found in either bundle, log an error and return a
299      * message formatted with the key and insert values provided by the
300      * caller. If there is no resource bundle available, just return a message
301      * formatted with the key and insert values provided by the caller.
302      *
303      * @param key - the key to the localized string in the resource bundle.
304      * @param inserts - the array of message inserts.
305      *
306      * @return the localized string formatted with the message inserts.
307      */

308     public String JavaDoc getString(
309         String JavaDoc key,
310         Object JavaDoc [] inserts)
311     {
312         String JavaDoc translated = null;
313
314         if (null != mResourceBundle)
315         {
316             try
317             {
318                 translated = mResourceBundle.getString(key);
319                 translated = MessageFormat.format(translated, inserts);
320             }
321             catch (java.util.MissingResourceException JavaDoc mrEx)
322             {
323                 if (null != mFallbackBundle)
324                 {
325                     try
326                     {
327                         translated = mFallbackBundle.getString(key);
328                         translated = MessageFormat.format(translated, inserts);
329                         mLog.fine(MessageFormat.format(
330                                 LOG_TRANSLATION_USING_FALLBACK,
331                                 new Object JavaDoc [] {key, mDefaultLocale, Locale.US}));
332                     }
333                     catch (java.util.MissingResourceException JavaDoc mreEx)
334                     {
335                         String JavaDoc fi = formatInserts(inserts);
336                         translated =
337                             MessageFormat.format(MSG_NO_TRANSLATION,
338                                 new Object JavaDoc [] {key, fi});
339                         mLog.warning(MessageFormat.format(
340                                 LOG_NO_TRANSLATION_FOR_KEY,
341                                 new Object JavaDoc [] {key, fi}));
342                     }
343                 }
344                 else
345                 {
346                     String JavaDoc fi = formatInserts(inserts);
347                     translated =
348                         MessageFormat.format(MSG_NO_TRANSLATION,
349                             new Object JavaDoc [] {key, fi});
350                     mLog.warning(MessageFormat.format(
351                             LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
352                             new Object JavaDoc [] {key, mDefaultLocale, fi}));
353                 }
354             }
355         }
356         else
357         {
358             translated =
359                 MessageFormat.format(MSG_NO_TRANSLATION,
360                     new Object JavaDoc [] {key, formatInserts(inserts)});
361         }
362
363         return translated;
364     }
365
366     /**
367      * Format an array of message inserts into a string. The ouptut string is
368      * in the format "insert1,insert2,....,insertn".
369      *
370      * @param inserts - the array of message inserts.
371      *
372      * @return the string formatted with the message inserts.
373      */

374     private String JavaDoc formatInserts(Object JavaDoc [] inserts)
375     {
376         StringBuffer JavaDoc formatted = new StringBuffer JavaDoc("");
377
378         for (int i = 0; i < inserts.length; i++)
379         {
380             if (i > 0)
381             {
382                 formatted.append(",");
383             }
384
385             formatted.append(inserts[i].toString());
386         }
387
388         return formatted.toString();
389     }
390 }
391
Popular Tags