Adds support for useCodeAsDefaultMessage

See gh-10466
pull/10457/merge
Kedar Joshi 7 years ago committed by Stephane Nicoll
parent 9102eb32d1
commit a68ec76bb6

@ -75,6 +75,7 @@ public class MessageSourceAutoConfiguration {
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
messageSource.setCacheSeconds(properties.getCacheSeconds());
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource;
}

@ -22,6 +22,7 @@ import java.nio.charset.Charset;
* Configuration properties for Message Source.
*
* @author Stephane Nicoll
* @author Kedar Joshi
* @since 2.0.0
*/
public class MessageSourceProperties {
@ -57,6 +58,11 @@ public class MessageSourceProperties {
*/
private boolean alwaysUseMessageFormat = false;
/**
* Set whether to use the message code as default message instead of throwing a NoSuchMessageException.
*/
private boolean useCodeAsDefaultMessage = false;
public String getBasename() {
return this.basename;
}
@ -97,4 +103,11 @@ public class MessageSourceProperties {
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
}
public boolean isUseCodeAsDefaultMessage() {
return this.useCodeAsDefaultMessage;
}
public void setUseCodeAsDefaultMessage(final boolean useCodeAsDefaultMessage) {
this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
}
}

@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Kedar Joshi
*/
public class MessageSourceAutoConfigurationTests {
@ -142,6 +143,26 @@ public class MessageSourceAutoConfigurationTests {
.getPropertyValue("alwaysUseMessageFormat");
}
@Test
public void testUseCodeAsDefaultMessageDefault() throws Exception {
load("spring.messages.basename:test/messages");
assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class)))
.isFalse();
}
@Test
public void testUseCodeAsDefaultMessageOn() throws Exception {
load("spring.messages.basename:test/messages",
"spring.messages.use-code-as-default-message:true");
assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class)))
.isTrue();
}
private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
return (boolean) new DirectFieldAccessor(messageSource)
.getPropertyValue("useCodeAsDefaultMessage");
}
@Test
public void existingMessageSourceIsPreferred() {
this.context = new AnnotationConfigApplicationContext();

Loading…
Cancel
Save