Merge pull request #3750 from eddumelendez/gh-3749
* pr/3750: Polish Add fallbackToSystemLocale configuration key
This commit is contained in:
commit
78769444d2
|
|
@ -48,6 +48,7 @@ import static org.springframework.util.StringUtils.trimAllWhitespace;
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnMissingBean(MessageSource.class)
|
@ConditionalOnMissingBean(MessageSource.class)
|
||||||
|
|
@ -77,6 +78,13 @@ public class MessageSourceAutoConfiguration {
|
||||||
*/
|
*/
|
||||||
private int cacheSeconds = -1;
|
private int cacheSeconds = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MessageSource messageSource() {
|
public MessageSource messageSource() {
|
||||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||||
|
|
@ -87,6 +95,7 @@ public class MessageSourceAutoConfiguration {
|
||||||
if (this.encoding != null) {
|
if (this.encoding != null) {
|
||||||
messageSource.setDefaultEncoding(this.encoding.name());
|
messageSource.setDefaultEncoding(this.encoding.name());
|
||||||
}
|
}
|
||||||
|
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
|
||||||
messageSource.setCacheSeconds(this.cacheSeconds);
|
messageSource.setCacheSeconds(this.cacheSeconds);
|
||||||
return messageSource;
|
return messageSource;
|
||||||
}
|
}
|
||||||
|
|
@ -115,6 +124,14 @@ public class MessageSourceAutoConfiguration {
|
||||||
this.cacheSeconds = cacheSeconds;
|
this.cacheSeconds = cacheSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isFallbackToSystemLocale() {
|
||||||
|
return this.fallbackToSystemLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
|
||||||
|
this.fallbackToSystemLocale = fallbackToSystemLocale;
|
||||||
|
}
|
||||||
|
|
||||||
protected static class ResourceBundleCondition extends SpringBootCondition {
|
protected static class ResourceBundleCondition extends SpringBootCondition {
|
||||||
|
|
||||||
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<String, ConditionOutcome>();
|
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<String, ConditionOutcome>();
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||||
|
|
@ -26,72 +27,59 @@ import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link MessageSourceAutoConfiguration}.
|
* Tests for {@link MessageSourceAutoConfiguration}.
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
* @author Eddú Meléndez
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class MessageSourceAutoConfigurationTests {
|
public class MessageSourceAutoConfigurationTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void closeContext() {
|
||||||
|
if (context != null) {
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultMessageSource() throws Exception {
|
public void testDefaultMessageSource() throws Exception {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
load();
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertEquals("Foo message",
|
assertEquals("Foo message",
|
||||||
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMessageSourceCreated() throws Exception {
|
public void testMessageSourceCreated() throws Exception {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
load("spring.messages.basename:test/messages");
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
|
||||||
"spring.messages.basename:test/messages");
|
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertEquals("bar",
|
assertEquals("bar",
|
||||||
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncodingWorks() throws Exception {
|
public void testEncodingWorks() throws Exception {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
load("spring.messages.basename:test/swedish");
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
|
||||||
"spring.messages.basename:test/swedish");
|
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertEquals("Some text with some swedish öäå!",
|
assertEquals("Some text with some swedish öäå!",
|
||||||
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultipleMessageSourceCreated() throws Exception {
|
public void testMultipleMessageSourceCreated() throws Exception {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
load("spring.messages.basename:test/messages,test/messages2");
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
assertEquals("bar", this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
||||||
"spring.messages.basename:test/messages,test/messages2");
|
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertEquals("bar",
|
|
||||||
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
|
||||||
assertEquals("bar-bar",
|
assertEquals("bar-bar",
|
||||||
this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK));
|
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();
|
load("spring.messages.encoding:rubbish");
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
|
||||||
"spring.messages.encoding:rubbish");
|
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
// Bad encoding just means the messages are ignored
|
// Bad encoding just means the messages are ignored
|
||||||
assertEquals("blah", this.context.getMessage("foo", null, "blah", Locale.UK));
|
assertEquals("blah", this.context.getMessage("foo", null, "blah", Locale.UK));
|
||||||
}
|
}
|
||||||
|
|
@ -107,6 +95,30 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFallbackDefault() throws Exception {
|
||||||
|
load("spring.messages.basename:test/messages");
|
||||||
|
|
||||||
|
assertTrue(this.context.getBean(MessageSourceAutoConfiguration.class)
|
||||||
|
.isFallbackToSystemLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFallbackTurnOff() throws Exception {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource("classpath:/switch-messages.properties")
|
@PropertySource("classpath:/switch-messages.properties")
|
||||||
protected static class Config {
|
protected static class Config {
|
||||||
|
|
|
||||||
|
|
@ -279,6 +279,7 @@ content into your application; rather pick only the properties that you need.
|
||||||
spring.messages.basename=messages
|
spring.messages.basename=messages
|
||||||
spring.messages.cache-seconds=-1
|
spring.messages.cache-seconds=-1
|
||||||
spring.messages.encoding=UTF-8
|
spring.messages.encoding=UTF-8
|
||||||
|
spring.messages.fallback-to-system-locale=true # set whether to fall back to the system Locale
|
||||||
|
|
||||||
[[common-application-properties-security]]
|
[[common-application-properties-security]]
|
||||||
# SECURITY ({sc-spring-boot-autoconfigure}/security/SecurityProperties.{sc-ext}[SecurityProperties])
|
# SECURITY ({sc-spring-boot-autoconfigure}/security/SecurityProperties.{sc-ext}[SecurityProperties])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue