public abstract static class Resources.ShadowResourceBundle extends ResourceBundle
ShadowResourceBundle is an abstract base class for
ResourceBundle classes which are backed by a properties file. When
the class is created, it loads a properties file with the same name as the
class.
In the standard scheme (see ResourceBundle), if
you call ,
it first looks for a class called ResourceBundle.getBundle(java.lang.String)("foo.MyResource")foo.MyResource, then
looks for a file called foo/MyResource.properties. If it finds
the file, it creates a PropertyResourceBundle and loads the class.
The problem is if you want to load the .properties file
into a dedicated class; ShadowResourceBundle helps with this
case.
You should create a class as follows:
package foo;
class MyResource extends ShadowResourceBundle {
public MyResource() throws java.io.IOException {
}
}
Then when you call
ResourceBundle.getBundle("foo.MyResource"),
it will find the class before the properties file, but still automatically
load the properties file based upon the name of the class.ResourceBundle.Controlparent| Modifier | Constructor and Description |
|---|---|
protected |
Resources.ShadowResourceBundle()
Creates a
ShadowResourceBundle, and reads resources from
a .properties file with the same name as the current class. |
| Modifier and Type | Method and Description |
|---|---|
Enumeration<String> |
getKeys() |
protected Object |
handleGetObject(String key) |
protected static Resources.ShadowResourceBundle |
instance(String baseName,
Locale locale,
ResourceBundle bundle)
Returns the instance of the
baseName resource bundle
for the given locale. |
clearCache, clearCache, containsKey, getBundle, getBundle, getBundle, getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, handleKeySet, keySet, setParentprotected Resources.ShadowResourceBundle()
throws IOException
ShadowResourceBundle, and reads resources from
a .properties file with the same name as the current class.
For example, if the class is called foo.MyResource_en_US,
reads from foo/MyResource_en_US.properties, then
foo/MyResource_en.properties, then
foo/MyResource.properties.IOException - on errorpublic Enumeration<String> getKeys()
getKeys in class ResourceBundleprotected Object handleGetObject(String key)
handleGetObject in class ResourceBundleprotected static Resources.ShadowResourceBundle instance(String baseName, Locale locale, ResourceBundle bundle)
baseName resource bundle
for the given locale.
This method should be called from a derived class, with the proper casting:
class MyResource extends ShadowResourceBundle {
...
/**
* Retrieves the instance of {@link MyResource} appropriate
* to the given locale.
**/
public static MyResource instance(Locale locale) {
return (MyResource) instance(
MyResource.class.getName(), locale,
ResourceBundle.getBundle(MyResource.class.getName(), locale));
}
...
}baseName - Base namelocale - Localebundle - Resource bundleCopyright © 2014–2015 Julian Hyde. All rights reserved.