public class IlvResourceUtil extends Object
Use of this class, instead of ResourceBundle.getBundle
,
allows applets loaded over http
to perform only a minimal
number of http
requests (assuming the applet's main code
uses the setAvailableResourceSuffixes(java.lang.String...)
method to specify which
locale is supported by the applet's resources).
The second idea here is not to hard code resource bundle paths. Instead, we pass the corresponding class. The advantage: if classes are moved, the resource bundle path is also up to date.
For example:
String s = IlvResourceUtil.getString(key, Myclass.class);It is not recommended to use this utility with an implicit class reference such as
String s = IlvResourceUtil.getString(key, getClass());because this variant works only if all subclasses of the current class are in the same package.
Modifier and Type | Method and Description |
---|---|
static ResourceBundle |
getBundle(String baseName)
Deprecated.
Since JViews 8.6, use
getBundle(String, ClassLoader) . This method is
unsafe since it used to lead to different results in
different JDKs, if multiple class loaders are used in your
application. Hence we strongly recommend not to use it
anymore. |
static ResourceBundle |
getBundle(String name,
Class clazz)
Deprecated.
Since JViews 8.7, please use the method
getBundle(String, Class, Locale) instead,
passing a locale explicitly - usually either
or
. |
static ResourceBundle |
getBundle(String baseName,
ClassLoader loader)
Deprecated.
Since JViews 8.7, please use the method
getBundle(String, Locale, ClassLoader) instead,
passing a locale explicitly - usually either
or
. |
static ResourceBundle |
getBundle(String name,
Class clazz,
Locale locale)
Returns the resource bundle in the package of the input class.
|
static ResourceBundle |
getBundle(String name,
Class clazz,
ULocale locale)
Returns the resource bundle in the package of the input class.
|
static ResourceBundle |
getBundle(String baseName,
Locale locale)
Deprecated.
Since JViews 8.6, use
getBundle(String, Locale, ClassLoader) . This method
is unsafe since it used to lead to different results in
different JDKs, if multiple class loaders are used in your
application. Hence we strongly recommend not to use it
anymore. |
static ResourceBundle |
getBundle(String baseName,
Locale locale,
ClassLoader loader)
Returns a resource bundle, examining the available resource suffixes.
|
static ResourceBundle |
getBundle(String baseName,
ULocale locale,
ClassLoader loader)
Returns a resource bundle, examining the available resource suffixes.
|
static String |
getCurrentLocaleString(Class clazz,
String subkey)
Returns a string for the given subkey from the resource bundle named
"messages" in the package of the input object.
|
static String |
getSafeString(String key,
Class clazz)
Deprecated.
Since JViews 8.7, please use the method
getSafeString(String, Class, Locale) instead,
passing a locale explicitly - usually either
or
. |
static String |
getSafeString(String key,
Class clazz,
Locale locale)
Returns a string for the given key from the resource bundle named
"messages" in the package of the input class.
|
static String |
getSafeString(String key,
Class clazz,
ULocale locale)
Returns a string for the given key from the resource bundle named
"messages" in the package of the input class.
|
static String |
getSafeString(String key,
String bundle,
Class clazz)
Deprecated.
Since JViews 8.7, please use the method
getSafeString(String, String, Class, Locale) instead,
passing a locale explicitly - usually either
or
. |
static String |
getSafeString(String key,
String bundle,
Class clazz,
Locale locale)
Returns a string for the given key from the resource bundle in the
package of the input class.
|
static String |
getSafeString(String key,
String bundle,
Class clazz,
ULocale locale)
Returns a string for the given key from the resource bundle in the
package of the input class.
|
static String |
getServerLocaleString(Class clazz,
String subkey)
Returns a string for the given subkey from the resource bundle named
"messages" in the package of the input object.
|
static String |
getString(String key,
Class clazz)
Deprecated.
Since JViews 8.7, please use the method
getString(String, Class, Locale) instead,
passing a locale explicitly - usually either
or
. |
static String |
getString(String key,
Class clazz,
Locale locale)
Returns a string for the given key from the resource bundle named
"messages" in the package of the input class.
|
static String |
getString(String key,
Class clazz,
Locale locale,
String defString)
Returns a string for the given key from the resource bundle named
"messages" in the package of the input class.
|
static String |
getString(String key,
Class clazz,
String defString)
Deprecated.
Since JViews 8.7, please use the method
getString(String, Class, Locale, String) instead,
passing a locale explicitly - usually either
or
. |
static String |
getString(String key,
Class clazz,
ULocale locale)
Returns a string for the given key from the resource bundle named
"messages" in the package of the input class.
|
static String |
getString(String key,
Class clazz,
ULocale locale,
String defString)
Returns a string for the given key from the resource bundle named
"messages" in the package of the input class.
|
static String |
getString(String key,
String bundle,
Class clazz)
Deprecated.
Since JViews 8.7, please use the method
getString(String, String, Class, Locale) instead,
passing a locale explicitly - usually either
or
. |
static String |
getString(String key,
String bundle,
Class clazz,
Locale locale)
Returns a string for the given key from the resource bundle in the
package of the input class.
|
static String |
getString(String key,
String bundle,
Class clazz,
Locale locale,
String defString)
Returns a string for the given key from the resource bundle in the
package of the input class.
|
static String |
getString(String key,
String bundle,
Class clazz,
String defString)
Deprecated.
Since JViews 8.7, please use the method
getString(String, String, Class, Locale, String) instead,
passing a locale explicitly - usually either
or
. |
static String |
getString(String key,
String bundle,
Class clazz,
ULocale locale)
Returns a string for the given key from the resource bundle in the
package of the input class.
|
static String |
getString(String key,
String bundle,
Class clazz,
ULocale locale,
String defString)
Returns a string for the given key from the resource bundle in the
package of the input class.
|
static boolean |
isInApplet()
Tests whether we are running inside an applet.
|
static void |
setAvailableResourceLocales(Locale... locales)
Sets the available resource locales.
|
static void |
setAvailableResourceLocales(ULocale... locales)
Sets the available resource locales.
|
static void |
setAvailableResourceSuffixes(String... suffixes)
Sets the available resource suffixes.
|
static void |
setDebug(int debug)
Sets the debugging mode.
|
public static String getString(String key, Class clazz)
getString(String, Class, Locale)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.MissingResourceException
- if no string for the given key can
be foundpublic static String getString(String key, Class clazz, Locale locale)
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.MissingResourceException
- if no string for the given key can
be foundpublic static String getString(String key, Class clazz, ULocale locale)
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.MissingResourceException
- if no string for the given key can
be foundpublic static String getString(String key, String bundle, Class clazz)
getString(String, String, Class, Locale)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.MissingResourceException
- if no string for the given key can
be foundpublic static String getString(String key, String bundle, Class clazz, Locale locale)
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.MissingResourceException
- if no string for the given key can
be foundpublic static String getString(String key, String bundle, Class clazz, ULocale locale)
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.MissingResourceException
- if no string for the given key can
be foundpublic static String getString(String key, Class clazz, String defString)
getString(String, Class, Locale, String)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.defString
- If the resource bundle is not found or no string for
the given key can be found, this default string is returned.public static String getString(String key, Class clazz, Locale locale, String defString)
This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.defString
- If the resource bundle is not found or no string for
the given key can be found, this default string is returned.public static String getString(String key, Class clazz, ULocale locale, String defString)
This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.defString
- If the resource bundle is not found or no string for
the given key can be found, this default string is returned.public static String getString(String key, String bundle, Class clazz, String defString)
getString(String, String, Class, Locale, String)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.defString
- If the resource bundle is not found or no string for
the given key can be found, this default string is returned.public static String getString(String key, String bundle, Class clazz, Locale locale, String defString)
This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.defString
- If the resource bundle is not found or no string for
the given key can be found, this default string is returned.public static String getString(String key, String bundle, Class clazz, ULocale locale, String defString)
This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.defString
- If the resource bundle is not found or no string for
the given key can be found, this default string is returned.public static String getSafeString(String key, Class clazz)
getSafeString(String, Class, Locale)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.public static String getSafeString(String key, Class clazz, Locale locale)
This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.public static String getSafeString(String key, Class clazz, ULocale locale)
This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.
key
- The key of the resource string.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.public static String getSafeString(String key, String bundle, Class clazz)
getSafeString(String, String, Class, Locale)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.public static String getSafeString(String key, String bundle, Class clazz, Locale locale)
This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.public static String getSafeString(String key, String bundle, Class clazz, ULocale locale)
This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.
key
- The key of the resource string.bundle
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.public static ResourceBundle getBundle(String name, Class clazz)
getBundle(String, Class, Locale)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.name
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.public static ResourceBundle getBundle(String name, Class clazz, Locale locale)
name
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.public static ResourceBundle getBundle(String name, Class clazz, ULocale locale)
name
- The base name of the resource bundle excluding the
package name.clazz
- The input class, to determine the package name and the
class loader.locale
- The locale.public static void setAvailableResourceSuffixes(String... suffixes)
The following sample shows how to use the API:
IlvResourceUtil.setAvailableResourceSuffixes("", "_fr", "_de" );This statement specifies that the default bundle (""), the generic French bundle ("_fr") and the generic German bundle ("_de") are available. If the locale is currently Canadian French, it will not try to load the bundle "_fr_CA" but immediately use the bundle "fr" instead.
In order to set back to the default behavior, call:
IlvResourceUtil.setAvailableResourceSuffixes((String[])null);
Suffixes have the following formats:
public static void setAvailableResourceLocales(Locale... locales)
setAvailableResourceSuffixes(java.lang.String...)
to set the
available resource suffixes.
This is useful for applets that don't provide resources for all
possible locales. Normally, if a resource is requested for a locale
that is not available, several unsuccessful requests are made to the
server to load the resource. This slows down the applet.
When setting the available resource locales, the requests are
avoided for those locales that are not in the list passed as argument,
and instead the resource bundle for the best matching default locale is
loaded. This speeds up the applet.
In order to set back to the default behavior, call:
IlvResourceUtil.setAvailableResourceLocales((String[])null);
public static void setAvailableResourceLocales(ULocale... locales)
setAvailableResourceSuffixes(java.lang.String...)
to set the
available resource suffixes.
This is useful for applets that don't provide resources for all
possible locales. Normally, if a resource is requested for a locale
that is not available, several unsuccessful requests are made to the
server to load the resource. This slows down the applet.
When setting the available resource locales, the requests are
avoided for those locales that are not in the list passed as argument,
and instead the resource bundle for the best matching default locale is
loaded. This speeds up the applet.
In order to set back to the default behavior, call:
IlvResourceUtil.setAvailableResourceLocales((String[])null);
public static boolean isInApplet()
true
when called inside an applet.
Returns false
when called inside an application that is
not an applet, or when it is impossible to determine the applet/application
mode from the class loader.
Since it is only useful for applets, the typical code to specify the available resources is the following:
if (IlvResourceUtil.isInApplet()) IlvResourceUtil.setAvailableResourceSuffixes("", "_fr", "_de" );
public static void setDebug(int debug)
ilog.views.util.IlvResourceUtil
",
stderr
.
public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) throws MissingResourceException
baseName
- The base name of the resource bundle including the
package name.locale
- The locale.loader
- The class loader to load the bundle.MissingResourceException
public static ResourceBundle getBundle(String baseName, ULocale locale, ClassLoader loader) throws MissingResourceException
baseName
- The base name of the resource bundle including the
package name.locale
- The locale.loader
- The class loader to load the bundle.MissingResourceException
public static ResourceBundle getBundle(String baseName, ClassLoader loader) throws MissingResourceException
getBundle(String, Locale, ClassLoader)
instead,
passing a locale explicitly - usually either
IlvLocaleUtil.getCurrentLocale()
or
IlvLocaleUtil.getServerLocale()
.baseName
- The base name of the resource bundle including the
package name.loader
- The class loader to load the bundle.MissingResourceException
public static ResourceBundle getBundle(String baseName, Locale locale) throws MissingResourceException
getBundle(String, Locale, ClassLoader)
. This method
is unsafe since it used to lead to different results in
different JDKs, if multiple class loaders are used in your
application. Hence we strongly recommend not to use it
anymore.baseName
- The base name of the resource bundle including the
package name.locale
- The locale.MissingResourceException
public static ResourceBundle getBundle(String baseName) throws MissingResourceException
getBundle(String, ClassLoader)
. This method is
unsafe since it used to lead to different results in
different JDKs, if multiple class loaders are used in your
application. Hence we strongly recommend not to use it
anymore.baseName
- The base name of the resource bundle including the
package name.MissingResourceException
public static String getCurrentLocaleString(Class clazz, String subkey)
IlvLocaleUtil.getCurrentLocale()
.
clazz
- The class, to determine the package name and the
class loader.subkey
- The subkey of the resource string. The key used will
be the concatenation of the simple name of the clazz,
a dot, and this subkey.MissingResourceException
- if no string for the given key can
be foundpublic static String getServerLocaleString(Class clazz, String subkey)
IlvLocaleUtil.getServerLocale()
.
clazz
- The class, to determine the package name and the
class loader.subkey
- The subkey of the resource string. The key used will
be the concatenation of the simple name of the clazz,
a dot, and this subkey.MissingResourceException
- if no string for the given key can
be found© Copyright Rogue Wave Software, Inc. 1997, 2015. All Rights Reserved.