public class ResourceInternationalString extends AbstractInternationalString implements Serializable
ResourceBundle
.
Resource bundles can be Java classes or properties files, one for each
language. The fully qualified class name of the base resource bundle (without locale suffix)
is specified at construction time.
The appropriate resource bundle is loaded at runtime for the client's language by looking for
a class or a properties file with the right suffix, for example "_en"
for English or
"_fr"
for French.
See the ResourceBundle.getBundle(…)
Javadoc for more information.
MyResources.properties
" exists in org.mypackage
and contains the following line:
MyKey = some valueThen an international string for
"some value"
can be created using the following code:
InternationalString value = new ResourceInternationalString("org.mypackage.MyResources", "MyKey");The
"some value"
string will be localized if the required properties files exist, for
example "MyResources_fr.properties
" for French or "MyResources_it.properties
"
for Italian, etc.
If needed, users can gain more control by overriding the getBundle(Locale)
method.
ClassLoader
to use be overriding the
getBundle(Locale)
method. This is recommended if the running environment
loads modules in isolated class loaders, as OSGi does for instance.
ClassLoader
argument in the constructor of this class because class loaders
can often be hard-coded (thus avoiding the cost of an extra field) and are usually not serializable.ResourceBundle
with the addition of type safety and optional arguments to be formatted in the localized string.
Those resource bundles provide formatInternational(int, …)
static methods for creating
international strings with the same functionality than this ResourceInternationalString
.
See org.apache.sis.util.resources
for more information.
getBundle(Locale)
is also immutable. Subclasses may or may not be immutable, at implementation choice. But implementors are
encouraged to make sure that subclasses remain immutable for more predictable behavior.ResourceBundle.getBundle(String, Locale)
,
Serialized FormDefined in the sis-utility
module
Modifier and Type | Field and Description |
---|---|
protected String |
key
The key for the resource to fetch.
|
protected String |
resources
The name of the resource bundle, as a fully qualified class name.
|
Constructor and Description |
---|
ResourceInternationalString(String resources,
String key)
Creates a new international string from the specified resource bundle and key.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object object)
Compares this international string with the specified object for equality.
|
protected ResourceBundle |
getBundle(Locale locale)
Returns the resource bundle for the given locale.
|
int |
hashCode()
Returns a hash code value for this international text.
|
String |
toString(Locale locale)
Returns a string in the specified locale.
|
charAt, compareTo, formatTo, length, subSequence, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
chars, codePoints
protected final String resources
null
.protected final String key
null
.public ResourceInternationalString(String resources, String key)
toString(Locale)
caller,
unless the getBundle(Locale)
method is overridden.resources
- the name of the resource bundle, as a fully qualified class name.key
- the key for the resource to fetch.protected ResourceBundle getBundle(Locale locale) throws MissingResourceException
MyResource
is a class defined
in the same module than the one that contain the resources to load:
@Override protected ResourceBundle getBundle(final Locale locale) { return ResourceBundle.getBundle(resources, locale, MyResource.class.getClassLoader()); }
locale
- the locale for which to get the resource bundle.MissingResourceException
- if no resource bundle can be found for the base name specified
at construction time.ResourceBundle.getBundle(String, Locale, ClassLoader)
public String toString(Locale locale) throws MissingResourceException
locale
, then this method searches for a string in an other locale as
specified in the ResourceBundle
class description.
null
argument valuenull
locale is handled as a synonymous of
Locale.ROOT
. However subclasses are free to use a different fallback. Client
code are encouraged to specify only non-null values for more determinist behavior.toString
in interface InternationalString
toString
in class AbstractInternationalString
locale
- the desired locale for the string to be returned.MissingResourceException
- if no resource can be found for the base name or for the key
specified at construction time.Locale.getDefault()
,
Locale.ROOT
public boolean equals(Object object)
Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.