spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.82k stars 38.18k forks source link

Formating "defaultMessage" in MessageSource.getMessage(...) [SPR-1008] #5718

Closed spring-projects-issues closed 19 years ago

spring-projects-issues commented 19 years ago

Xavier Leveque opened SPR-1008 and commented

When using method MessageSource.getMessage(String code, Object[] args, String defaultMessage, Locale locale), if the "code" can not be resolved in current message source or its parent(s), the "defaultMessage" is returned (or the "code" if "useCodeAsDefaultMessage" is set to true).

What if that default message (or code) contains parameters to be replaced by arguments.

It would be great if the "defaultMessage" (or "code") could be optionally formatted with the given arguments (if any) as the resolved message is.

This is something that should be done in the message source only if the code can't be resolved since obviously we don't want to pre-format the defaultMessage given as argument.

A new method could be added to the AbstractMessageSource class to activate/deactivate this behavior.

public void setFormatDefaultMessage(boolean formatDefaultMessage);

Another way would be to add a new argument to the method "getMessage()" that contains default arguments:

public String getMessage(String code, Object[] args, String defaultMessage, Object[] defaultArgs, Locale locale);


Affects: 1.2.1

spring-projects-issues commented 19 years ago

Juergen Hoeller commented

Good idea! Default messages should be formatted as well, to allow for argument placeholders there - even if such messages will be plain text Strings most of the time.

Consequently, I've refined AbstractMessageSource to format passed-in default messages as well, resolving arguments when necessary. The used MessageFormats will be cached for efficiency.

Juergen