diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java index 8303d58ba8..d3a223b9c9 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java @@ -48,6 +48,7 @@ import static org.springframework.util.StringUtils.trimAllWhitespace; * * @author Dave Syer * @author Phillip Webb + * @author EddĂș MelĂ©ndez */ @Configuration @ConditionalOnMissingBean(MessageSource.class) @@ -77,6 +78,11 @@ public class MessageSourceAutoConfiguration { */ private int cacheSeconds = -1; + /** + * Fall back to the system locale. + */ + private boolean fallbackToSystemLocale = true; + @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); @@ -87,6 +93,7 @@ public class MessageSourceAutoConfiguration { if (this.encoding != null) { messageSource.setDefaultEncoding(this.encoding.name()); } + messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale); messageSource.setCacheSeconds(this.cacheSeconds); return messageSource; } @@ -115,6 +122,14 @@ public class MessageSourceAutoConfiguration { this.cacheSeconds = cacheSeconds; } + public boolean isFallbackToSystemLocale() { + return this.fallbackToSystemLocale; + } + + public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) { + this.fallbackToSystemLocale = fallbackToSystemLocale; + } + protected static class ResourceBundleCondition extends SpringBootCondition { private static ConcurrentReferenceHashMap cache = new ConcurrentReferenceHashMap(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java index c1f2a38a4a..ff54cd6fc0 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,8 +24,11 @@ import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Tests for {@link MessageSourceAutoConfiguration}. @@ -78,8 +81,7 @@ public class MessageSourceAutoConfigurationTests { this.context.register(MessageSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); - assertEquals("bar", - this.context.getMessage("foo", null, "Foo message", Locale.UK)); + 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)); } @@ -87,8 +89,7 @@ public class MessageSourceAutoConfigurationTests { @Test public void testBadEncoding() throws Exception { this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.messages.encoding:rubbish"); + EnvironmentTestUtils.addEnvironment(this.context, "spring.messages.encoding:rubbish"); this.context.register(MessageSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); @@ -107,6 +108,35 @@ public class MessageSourceAutoConfigurationTests { this.context.getMessage("foo", null, "Foo message", Locale.UK)); } + @Test + public void testFallbackDefault() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.messages.basename:test/messages"); + this.context.register(MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + + assertTrue(this.context.getBean(MessageSourceAutoConfiguration.class) + .isFallbackToSystemLocale()); + assertEquals("bar", this.context.getMessage("foo", null, "bar", Locale.UK)); + } + + @Test + public void testFallbackTurnOff() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.messages.basename:test/messages", + "spring.messages.fallback-to-system-locale:false"); + this.context.register(MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + + assertFalse(this.context.getBean(MessageSourceAutoConfiguration.class) + .isFallbackToSystemLocale()); + assertEquals("bar", this.context.getMessage("foo", null, "bar", Locale.UK)); + } + @Configuration @PropertySource("classpath:/switch-messages.properties") protected static class Config { diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index d62bac23d2..296bd57092 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -279,6 +279,7 @@ content into your application; rather pick only the properties that you need. spring.messages.basename=messages spring.messages.cache-seconds=-1 spring.messages.encoding=UTF-8 + spring.messages.fallback-to-system-locale=true [[common-application-properties-security]] # SECURITY ({sc-spring-boot-autoconfigure}/security/SecurityProperties.{sc-ext}[SecurityProperties])