public class Messages extends ResourceBundle
ResourceBundle
.
Wraps a ResourceBundle to provide some useful additional functionality.
MissingResourceException
, the ResourceBundle.getString(String)
method
will return The following example shows the basic usage of this class for retrieving localized messages:
The ability to resolve keys prefixed by class name allows you to place all your messages in a single file such
as a common
The following shows how to retrieve messages from a common bundle:
Resource bundles are searched using the following base name patterns:
These patterns can be customized using the MessagesBuilder.baseNames(String...)
method.
Localized messages can be retrieved in the following way:
ResourceBundle.Control
Constructor and Description |
---|
Messages(Class<?> forClass,
ResourceBundle rb,
Locale locale,
Messages parent)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(String key) |
static MessagesBuilder |
create(Class<?> forClass)
Creator.
|
String |
findFirstString(String... keys)
Looks for all the specified keys in the resource bundle and returns the first value that exists.
|
Messages |
forLocale(Locale locale)
Returns this message bundle for the specified locale.
|
Enumeration<String> |
getKeys() |
String |
getString(String key,
Object... args)
Similar to
ResourceBundle.getString(String) except allows you to pass in MessageFormat objects. |
protected Object |
handleGetObject(String key) |
Set<String> |
keySet() |
Set<String> |
keySet(String prefix)
Returns all keys in this resource bundle with the specified prefix.
|
static Messages |
of(Class<?> forClass)
Constructor.
|
static Messages |
of(Class<?> forClass,
String name)
Constructor.
|
String |
toString() |
clearCache, clearCache, getBaseBundleName, getBundle, getBundle, getBundle, getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, handleKeySet, setParent
public Messages(Class<?> forClass, ResourceBundle rb, Locale locale, Messages parent)
forClass
- The class we're creating this object for.rb
- The resource bundle we're encapsulating. Can be locale
- The locale of these messages.parent
- The parent resource. Can be public static final MessagesBuilder create(Class<?> forClass)
forClass
- The class we're creating this object for.public static final Messages of(Class<?> forClass)
forClass
- The class we're creating this object for.public static final Messages of(Class<?> forClass, String name)
forClass
- The class we're creating this object for.name
- The bundle name (e.g. public Messages forLocale(Locale locale)
locale
- The locale to get the messages for.Messages
object. Never public Set<String> keySet(String prefix)
Keys are returned in alphabetical order.
prefix
- The prefix.public String getString(String key, Object... args)
ResourceBundle.getString(String)
except allows you to pass in MessageFormat
objects.key
- The resource bundle key.args
- Optional MessageFormat
-style arguments.public String findFirstString(String... keys)
keys
- The list of possible keys.protected Object handleGetObject(String key)
handleGetObject
in class ResourceBundle
public boolean containsKey(String key)
containsKey
in class ResourceBundle
public Set<String> keySet()
keySet
in class ResourceBundle
public Enumeration<String> getKeys()
getKeys
in class ResourceBundle
Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.