public abstract class MessageSourceSupport
extends java.lang.Object
MessageFormat
handling but not implementing concrete
methods defined in the MessageSource
.
AbstractMessageSource
derives from this class, providing concrete
getMessage
implementations that delegate to a central template
method for message code resolution.
Modifier and Type | Field and Description |
---|---|
private boolean |
alwaysUseMessageFormat |
private static java.text.MessageFormat |
INVALID_MESSAGE_FORMAT |
protected Log |
logger
Logger available to subclasses
|
private java.util.Map<java.lang.String,java.util.Map<java.util.Locale,java.text.MessageFormat>> |
messageFormatsPerMessage
Cache to hold already generated MessageFormats per message.
|
Constructor and Description |
---|
MessageSourceSupport() |
Modifier and Type | Method and Description |
---|---|
protected java.text.MessageFormat |
createMessageFormat(java.lang.String msg,
java.util.Locale locale)
Create a MessageFormat for the given message and Locale.
|
protected java.lang.String |
formatMessage(java.lang.String msg,
java.lang.Object[] args,
java.util.Locale locale)
Format the given message String, using cached MessageFormats.
|
protected boolean |
isAlwaysUseMessageFormat()
Return whether to always apply the MessageFormat rules, parsing even
messages without arguments.
|
protected java.lang.String |
renderDefaultMessage(java.lang.String defaultMessage,
java.lang.Object[] args,
java.util.Locale locale)
Render the given default message String.
|
protected java.lang.Object[] |
resolveArguments(java.lang.Object[] args,
java.util.Locale locale)
Template method for resolving argument objects.
|
void |
setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat)
Set whether to always apply the
MessageFormat rules,
parsing even messages without arguments. |
private static final java.text.MessageFormat INVALID_MESSAGE_FORMAT
protected final Log logger
private boolean alwaysUseMessageFormat
private final java.util.Map<java.lang.String,java.util.Map<java.util.Locale,java.text.MessageFormat>> messageFormatsPerMessage
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat)
MessageFormat
rules,
parsing even messages without arguments.
Default is "false": Messages without arguments are by default returned as-is, without parsing them through MessageFormat. Set this to "true" to enforce MessageFormat for all messages, expecting all message texts to be written with MessageFormat escaping.
For example, MessageFormat expects a single quote to be escaped as "''". If your message texts are all written with such escaping, even when not defining argument placeholders, you need to set this flag to "true". Else, only message texts with actual arguments are supposed to be written with MessageFormat escaping.
MessageFormat
protected boolean isAlwaysUseMessageFormat()
protected java.lang.String renderDefaultMessage(java.lang.String defaultMessage, java.lang.Object[] args, java.util.Locale locale)
The default implementation passes the String to formatMessage
,
resolving any argument placeholders found in them. Subclasses may override
this method to plug in custom processing of default messages.
defaultMessage
- the passed-in default message Stringargs
- array of arguments that will be filled in for params within
the message, or null
if none.locale
- the Locale used for formattingformatMessage(String, Object[], java.util.Locale)
protected java.lang.String formatMessage(java.lang.String msg, java.lang.Object[] args, java.util.Locale locale)
msg
- the message to formatargs
- array of arguments that will be filled in for params within
the message, or null
if nonelocale
- the Locale used for formattingprotected java.text.MessageFormat createMessageFormat(java.lang.String msg, java.util.Locale locale)
msg
- the message to create a MessageFormat forlocale
- the Locale to create a MessageFormat forprotected java.lang.Object[] resolveArguments(java.lang.Object[] args, java.util.Locale locale)
The default implementation simply returns the given argument array as-is. Can be overridden in subclasses in order to resolve special argument types.
args
- the original argument arraylocale
- the Locale to resolve against