From 4912b989a59b36e3decad0d0cf6ef5769a0ddcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sat, 26 Mar 2016 19:48:12 +1000 Subject: [PATCH] Add useAlwaysMessageFormat configuration key Allow to configure the `useAlwaysMessageFormat` attribute of `MessageSource` via configuration. Closes gh-5483 --- .../MessageSourceAutoConfiguration.java | 15 +++++++++++++++ .../MessageSourceAutoConfigurationTests.java | 15 +++++++++++++++ .../asciidoc/appendix-application-properties.adoc | 1 + 3 files changed, 31 insertions(+) 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 8cbd87fc88f..98d1d246af4 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 @@ -80,6 +80,12 @@ public class MessageSourceAutoConfiguration { */ private boolean fallbackToSystemLocale = true; + /** + * Set whether to always apply the MessageFormat rules, parsing even + * messages without arguments. + */ + private boolean alwaysUseMessageFormat = false; + @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); @@ -92,6 +98,7 @@ public class MessageSourceAutoConfiguration { } messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale); messageSource.setCacheSeconds(this.cacheSeconds); + messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat); return messageSource; } @@ -127,6 +134,14 @@ public class MessageSourceAutoConfiguration { this.fallbackToSystemLocale = fallbackToSystemLocale; } + public boolean isAlwaysUseMessageFormat() { + return this.alwaysUseMessageFormat; + } + + public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) { + this.alwaysUseMessageFormat = alwaysUseMessageFormat; + } + 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 ddb6deb6981..68d2cf80040 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 @@ -116,6 +116,21 @@ public class MessageSourceAutoConfigurationTests { .isFallbackToSystemLocale()).isFalse(); } + @Test + public void testFormatMessageDefault() throws Exception { + load("spring.messages.basename:test/messages"); + assertThat(this.context.getBean(MessageSourceAutoConfiguration.class) + .isAlwaysUseMessageFormat()).isFalse(); + } + + @Test + public void testFormatMessageOn() throws Exception { + load("spring.messages.basename:test/messages", + "spring.messages.always-use-message-format:true"); + assertThat(this.context.getBean(MessageSourceAutoConfiguration.class) + .isAlwaysUseMessageFormat()).isTrue(); + } + @Test public void existingMessageSourceIsPreferred() { this.context = new AnnotationConfigApplicationContext(); 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 fa31ea2ee9b..3fcf02978d2 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -113,6 +113,7 @@ content into your application; rather pick only the properties that you need. spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever. spring.messages.encoding=UTF-8 # Message bundles encoding. spring.messages.fallback-to-system-locale=true # Set whether to fall back to the system Locale if no files for a specific Locale have been found. + spring.messages.always-use-message-format=false # Set whether to always apply the MessageFormat rules, parsing even messages without arguments. # OUTPUT spring.output.ansi.enabled=detect # Configure the ANSI output (can be "detect", "always", "never").