StringTranslatorpublic class StringTranslator extends Object This is the implementation of the String Translator, which provides services
for internationalization of messages to all services running inside the JBI
environment. |
Fields Summary |
---|
private static final String | LOGGER_NAMELogger name | public static final String | RESOURCE_BUNDLE_NAMEUnqualified name for resource bundles. | private static final String | LOG_NEW_INSTANCELog message for creation of new instance. | private static final String | LOG_CURRENT_LOCALELog message for locale. | private static final String | LOG_UNABLE_TO_LOAD_BUNDLELog message for failure loading resource bundle. | private static final String | LOG_USING_BUNDLELog message for using alternate resource bundle. | private static final String | LOG_TRANSLATION_USING_FALLBACKLog message for using fallback resource bundle to look up a message. | private static final String | LOG_NO_TRANSLATION_FOR_KEYLog message for no translation available for a message key in any
resource bundle. | private static final String | LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLELog message for no translation available for a message key in a
particular resource bundle. | private static final String | MSG_NO_TRANSLATIONMessage text used when no translation is available for a message key. | private Locale | mDefaultLocaleThe default locale at the time this StringTranslator was created. | private Logger | mLogLogger for this instance | private ResourceBundle | mFallbackBundleFallback ResourceBundle for a single package name. This is used only
when the default locale is something other than Locale.US. In that
case, this is the bundle for Locale.US for looking up messages that are
not found in the default locale. | private ResourceBundle | mResourceBundleResourceBundle for a single package name. | private static StringTranslator | defaultInstance |
Constructors Summary |
---|
public StringTranslator()
this("com.sun.enterprise.jbi.serviceengine.core", null);
| public StringTranslator(String packageName, ClassLoader classLoader)Constructor. This loads the Resource Bundle for the current locale, and
if the current locale is not Locale.US, it loads the Resource Bundle
for Locale.US and stores it as the backup for string lookup.
mLog = Logger.getLogger(packageName);
String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
mDefaultLocale = Locale.getDefault();
// Always load the bundle for english
mFallbackBundle = null;
ResourceBundle englishBundle = null;
try
{
if (null == classLoader)
{
englishBundle = ResourceBundle.getBundle(bundleName, Locale.US);
}
else
{
englishBundle =
ResourceBundle.getBundle(bundleName, Locale.US, classLoader);
}
}
catch (java.util.MissingResourceException mrEx)
{
mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE,
new Object [] {bundleName, Locale.US, mrEx}));
}
// If the default locale is English, set it as the primary bundle.
// If the default locale is not English, attempt to load the bundle.
// If it is found, save it as the primary and set the fallback to
// English. If it is not found, set the primary to English.
if (mDefaultLocale.equals(Locale.US))
{
mResourceBundle = englishBundle;
}
else
{
try
{
if (null == classLoader)
{
mResourceBundle = ResourceBundle.getBundle(bundleName);
mFallbackBundle = englishBundle;
}
else
{
mResourceBundle =
ResourceBundle.getBundle(bundleName, mDefaultLocale,
classLoader);
mFallbackBundle = englishBundle;
}
}
catch (java.util.MissingResourceException mrEx)
{
mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE,
new Object [] {bundleName, mDefaultLocale, mrEx}));
mLog.warning(MessageFormat.format(LOG_USING_BUNDLE,
new Object [] {Locale.US}));
mResourceBundle = englishBundle;
}
}
|
Methods Summary |
---|
private java.lang.String | formatInserts(java.lang.Object[] inserts)Format an array of message inserts into a string. The ouptut string is
in the format "insert1,insert2,....,insertn".
StringBuffer formatted = new StringBuffer("");
for (int i = 0; i < inserts.length; i++)
{
if (i > 0)
{
formatted.append(",");
}
formatted.append(inserts[i].toString());
}
return formatted.toString();
| public static com.sun.enterprise.jbi.serviceengine.util.soap.StringTranslator | getDefaultInstance()
if (defaultInstance == null) {
defaultInstance = new StringTranslator();
}
return defaultInstance;
| public java.lang.String | getString(java.lang.String key)Get a localized string using the specified resource key.
Object [] inserts = new Object[0];
return getString(key, inserts);
| public java.lang.String | getString(java.lang.String key, java.lang.Object insert1)Get a localized string using the specified resource key. Handle one
message insert.
Object [] inserts = {insert1};
return getString(key, inserts);
| public java.lang.String | getString(java.lang.String key, java.lang.Object insert1, java.lang.Object insert2)Get a localized string using the specified resource key. Handle two
message inserts.
Object [] inserts = {insert1, insert2};
return getString(key, inserts);
| public java.lang.String | getString(java.lang.String key, java.lang.Object insert1, java.lang.Object insert2, java.lang.Object insert3)Get a localized string using the specified resource key. Handle three
message inserts.
Object [] inserts = {insert1, insert2, insert3};
return getString(key, inserts);
| public java.lang.String | getString(java.lang.String key, java.lang.Object insert1, java.lang.Object insert2, java.lang.Object insert3, java.lang.Object insert4)Get a localized string using the specified resource key. Handle four
message inserts.
Object [] inserts = {insert1, insert2, insert3, insert4};
return getString(key, inserts);
| public java.lang.String | getString(java.lang.String key, java.lang.Object[] inserts)Get a localized string using the specified resource key. Handle any
number of message inserts. This method is called by all the other
getString() methods in the class. The procedure for string lookup is to
first look in the primary resource bundle, then in the fallback
resource bundle. If the string is found in the primary, return the
translated string quietly. If the string is not found in the primary
but in the fallback, log a warning and return the translated string. If
the string is not found in either bundle, log an error and return a
message formatted with the key and insert values provided by the
caller. If there is no resource bundle available, just return a message
formatted with the key and insert values provided by the caller.
String translated = null;
if (null != mResourceBundle)
{
try
{
translated = mResourceBundle.getString(key);
translated = MessageFormat.format(translated, inserts);
}
catch (java.util.MissingResourceException mrEx)
{
if (null != mFallbackBundle)
{
try
{
translated = mFallbackBundle.getString(key);
translated = MessageFormat.format(translated, inserts);
mLog.fine(MessageFormat.format(
LOG_TRANSLATION_USING_FALLBACK,
new Object [] {key, mDefaultLocale, Locale.US}));
}
catch (java.util.MissingResourceException mreEx)
{
String fi = formatInserts(inserts);
translated =
MessageFormat.format(MSG_NO_TRANSLATION,
new Object [] {key, fi});
mLog.warning(MessageFormat.format(
LOG_NO_TRANSLATION_FOR_KEY,
new Object [] {key, fi}));
}
}
else
{
String fi = formatInserts(inserts);
translated =
MessageFormat.format(MSG_NO_TRANSLATION,
new Object [] {key, fi});
mLog.warning(MessageFormat.format(
LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
new Object [] {key, mDefaultLocale, fi}));
}
}
}
else
{
translated =
MessageFormat.format(MSG_NO_TRANSLATION,
new Object [] {key, formatInserts(inserts)});
}
return translated;
|
|