Class MessageSourceSupport
- Direct Known Subclasses:
AbstractMessageSource,DelegatingMessageSource
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.
- Since:
- 2.5.5
- Author:
- Juergen Hoeller
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected MessageFormatcreateMessageFormat(String msg, Locale locale) Create aMessageFormatfor the given message and Locale.protected StringformatMessage(String msg, Object[] args, Locale locale) Format the given message String, using cached MessageFormats.protected booleanReturn whether to always apply theMessageFormatrules, parsing even messages without arguments.protected StringrenderDefaultMessage(String defaultMessage, Object[] args, Locale locale) Render the given default message String.protected Object[]resolveArguments(Object[] args, Locale locale) Template method for resolving argument objects.voidsetAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) Set whether to always apply theMessageFormatrules, parsing even messages without arguments.
-
Field Details
-
logger
Logger available to subclasses.
-
-
Constructor Details
-
MessageSourceSupport
public MessageSourceSupport()
-
-
Method Details
-
setAlwaysUseMessageFormat
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) Set whether to always apply theMessageFormatrules, parsing even messages without arguments.Default is
false: Messages without arguments are by default returned as-is, without parsing them throughMessageFormat. Set this totrueto enforceMessageFormatfor all messages, expecting all message texts to be written withMessageFormatescaping.For example,
MessageFormatexpects a single quote to be escaped as two adjacent single quotes ("''"). If your message texts are all written with such escaping, even when not defining argument placeholders, you need to set this flag totrue. Otherwise, only message texts with actual arguments are supposed to be written withMessageFormatescaping.- See Also:
-
isAlwaysUseMessageFormat
protected boolean isAlwaysUseMessageFormat()Return whether to always apply theMessageFormatrules, parsing even messages without arguments. -
renderDefaultMessage
protected String renderDefaultMessage(String defaultMessage, @Nullable Object[] args, @Nullable Locale locale) Render the given default message String. The default message is passed in as specified by the caller and can be rendered into a fully formatted default message shown to the user.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.- Parameters:
defaultMessage- the passed-in default message Stringargs- array of arguments that will be filled in for params within the message, ornullif none.locale- the Locale used for formatting- Returns:
- the rendered default message (with resolved arguments)
- See Also:
-
formatMessage
Format the given message String, using cached MessageFormats. By default invoked for passed-in default messages, to resolve any argument placeholders found in them.- Parameters:
msg- the message to formatargs- array of arguments that will be filled in for params within the message, ornullif nonelocale- the Locale used for formatting- Returns:
- the formatted message (with resolved arguments)
-
createMessageFormat
Create aMessageFormatfor the given message and Locale.- Parameters:
msg- the message to create aMessageFormatforlocale- the Locale to create aMessageFormatfor- Returns:
- the
MessageFormatinstance
-
resolveArguments
Template method for resolving argument objects.The default implementation simply returns the given argument array as-is. Can be overridden in subclasses in order to resolve special argument types.
- Parameters:
args- the original argument arraylocale- the Locale to resolve against- Returns:
- the resolved argument array
-