Allow multiple MessageSources that are comma separated.
Fixes gh-532, Fixes gh-506
This commit is contained in:
parent
632af6b1ab
commit
7be2d97d49
|
@ -16,6 +16,9 @@
|
|||
|
||||
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.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
|
@ -26,10 +29,11 @@ import org.springframework.context.support.ResourceBundleMessageSource;
|
|||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}.
|
||||
*
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Configuration
|
||||
|
@ -48,7 +52,9 @@ public class MessageSourceAutoConfiguration implements EnvironmentAware {
|
|||
public MessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
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");
|
||||
messageSource.setDefaultEncoding(encoding);
|
||||
return messageSource;
|
||||
|
|
|
@ -16,17 +16,17 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link MessageSourceAutoConfiguration}.
|
||||
*
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class MessageSourceAutoConfigurationTests {
|
||||
|
@ -55,6 +55,20 @@ public class MessageSourceAutoConfigurationTests {
|
|||
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
|
||||
public void testBadEncoding() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
foo-foo=bar-bar
|
Loading…
Reference in New Issue