From 74e1a1c94081d30756cebc2a43c144fc1e3f50ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 13 Aug 2015 10:56:02 -0500 Subject: [PATCH 1/2] Add fallbackToSystemLocale configuration key Allow to configure the `fallbackToSystemLocale` attribute of `MessageSource` via configuration. Closes gh-3749 --- .../MessageSourceAutoConfiguration.java | 15 +++++++ .../MessageSourceAutoConfigurationTests.java | 40 ++++++++++++++++--- .../appendix-application-properties.adoc | 1 + 3 files changed, 51 insertions(+), 5 deletions(-) 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 8303d58ba80..d3a223b9c99 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 c1f2a38a4a4..ff54cd6fc00 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 d62bac23d29..296bd570921 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]) From 4e0f99ed8be1e6c98e1736f1587b5f1a3ae7db0d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 14 Aug 2015 09:24:24 +0200 Subject: [PATCH 2/2] Polish Closes gh-3750 --- .../MessageSourceAutoConfiguration.java | 4 +- .../MessageSourceAutoConfigurationTests.java | 66 +++++++------------ .../appendix-application-properties.adoc | 2 +- 3 files changed, 28 insertions(+), 44 deletions(-) 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 d3a223b9c99..ca8237d0ea1 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 @@ -79,7 +79,9 @@ public class MessageSourceAutoConfiguration { private int cacheSeconds = -1; /** - * Fall back to the system locale. + * Set whether to fall back to the system Locale if no files for a specific Locale + * have been found. if this is turned off, the only fallback will be the default + * file (e.g. "messages.properties" for basename "messages"). */ private boolean fallbackToSystemLocale = true; 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 ff54cd6fc00..10621de0813 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 @@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure; import java.util.Locale; +import org.junit.After; import org.junit.Ignore; import org.junit.Test; 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; @@ -34,53 +34,44 @@ import static org.junit.Assert.assertTrue; * Tests for {@link MessageSourceAutoConfiguration}. * * @author Dave Syer + * @author Eddú Meléndez + * @author Stephane Nicoll */ public class MessageSourceAutoConfigurationTests { private AnnotationConfigApplicationContext context; + @After + public void closeContext() { + if (context != null) { + context.close(); + } + } + @Test public void testDefaultMessageSource() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(MessageSourceAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); assertEquals("Foo message", this.context.getMessage("foo", null, "Foo message", Locale.UK)); } @Test public void testMessageSourceCreated() 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(); + load("spring.messages.basename:test/messages"); assertEquals("bar", this.context.getMessage("foo", null, "Foo message", Locale.UK)); } @Test public void testEncodingWorks() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.messages.basename:test/swedish"); - this.context.register(MessageSourceAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.messages.basename:test/swedish"); assertEquals("Some text with some swedish öäå!", this.context.getMessage("foo", null, "Foo message", Locale.UK)); } @Test public void testMultipleMessageSourceCreated() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.messages.basename:test/messages,test/messages2"); - this.context.register(MessageSourceAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.messages.basename:test/messages,test/messages2"); 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)); @@ -88,11 +79,7 @@ public class MessageSourceAutoConfigurationTests { @Test public void testBadEncoding() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.messages.encoding:rubbish"); - this.context.register(MessageSourceAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.messages.encoding:rubbish"); // Bad encoding just means the messages are ignored assertEquals("blah", this.context.getMessage("foo", null, "blah", Locale.UK)); } @@ -110,31 +97,26 @@ public class MessageSourceAutoConfigurationTests { @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(); + load("spring.messages.basename:test/messages"); 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", + load("spring.messages.basename:test/messages", "spring.messages.fallback-to-system-locale:false"); + assertFalse(this.context.getBean(MessageSourceAutoConfiguration.class) + .isFallbackToSystemLocale()); + } + + private void load(String... environment) { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, environment); 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 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 296bd570921..cd526a3f77e 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -279,7 +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 + spring.messages.fallback-to-system-locale=true # set whether to fall back to the system Locale [[common-application-properties-security]] # SECURITY ({sc-spring-boot-autoconfigure}/security/SecurityProperties.{sc-ext}[SecurityProperties])