Skip to content

Adds auto-configuration of property useCodeAsDefaultMessage of MessageSource #10466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public MessageSource messageSource() {
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
messageSource.setCacheSeconds(properties.getCacheSeconds());
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Configuration properties for Message Source.
*
* @author Stephane Nicoll
* @author Kedar Joshi
* @since 2.0.0
*/
public class MessageSourceProperties {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -97,4 +103,11 @@ public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
}

public boolean isUseCodeAsDefaultMessage() {
return this.useCodeAsDefaultMessage;
}

public void setUseCodeAsDefaultMessage(final boolean useCodeAsDefaultMessage) {
this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Dave Syer
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Kedar Joshi
*/
public class MessageSourceAutoConfigurationTests {

Expand Down Expand Up @@ -142,6 +143,26 @@ private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
.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();
Expand Down