Commit 7be2d97d authored by Chris Savory's avatar Chris Savory Committed by Dave Syer

Allow multiple MessageSources that are comma separated.

Fixes gh-532, Fixes gh-506
parent 632af6b1
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure; package org.springframework.boot.autoconfigure;
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
import static org.springframework.util.StringUtils.trimAllWhitespace;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
...@@ -26,10 +29,11 @@ import org.springframework.context.support.ResourceBundleMessageSource; ...@@ -26,10 +29,11 @@ import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}. * {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}.
* *
* @author Dave Syer * @author Dave Syer
*/ */
@Configuration @Configuration
...@@ -48,7 +52,9 @@ public class MessageSourceAutoConfiguration implements EnvironmentAware { ...@@ -48,7 +52,9 @@ public class MessageSourceAutoConfiguration implements EnvironmentAware {
public MessageSource messageSource() { public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
String basename = this.environment.getProperty("basename", "messages"); String basename = this.environment.getProperty("basename", "messages");
messageSource.setBasename(basename); if (StringUtils.hasText(basename)) {
messageSource.setBasenames(commaDelimitedListToStringArray(trimAllWhitespace(basename)));
}
String encoding = this.environment.getProperty("encoding", "utf-8"); String encoding = this.environment.getProperty("encoding", "utf-8");
messageSource.setDefaultEncoding(encoding); messageSource.setDefaultEncoding(encoding);
return messageSource; return messageSource;
......
...@@ -16,17 +16,17 @@ ...@@ -16,17 +16,17 @@
package org.springframework.boot.autoconfigure; package org.springframework.boot.autoconfigure;
import static org.junit.Assert.assertEquals;
import java.util.Locale; import java.util.Locale;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertEquals;
/** /**
* Tests for {@link MessageSourceAutoConfiguration}. * Tests for {@link MessageSourceAutoConfiguration}.
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class MessageSourceAutoConfigurationTests { public class MessageSourceAutoConfigurationTests {
...@@ -55,6 +55,20 @@ public class MessageSourceAutoConfigurationTests { ...@@ -55,6 +55,20 @@ public class MessageSourceAutoConfigurationTests {
this.context.getMessage("foo", null, "Foo message", Locale.UK)); this.context.getMessage("foo", null, "Foo message", Locale.UK));
} }
@Test
public void testMultipleMessageSourceCreated() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/messages,test/messages2");
this.context.refresh();
assertEquals("bar",
this.context.getMessage("foo", null, "Foo message", Locale.UK));
assertEquals("bar-bar",
this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK));
}
@Test @Test
public void testBadEncoding() throws Exception { public void testBadEncoding() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment