#1075 - Improvements in I18N configuration setup.
We now avoid a double-lookup of the default messages in the resource bundle setup. Tightened the test case to make sure the sample resource bundle is properly used, even if no default messages bundle is present.
This commit is contained in:
@@ -31,7 +31,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.context.support.AbstractMessageSource;
|
import org.springframework.context.support.AbstractMessageSource;
|
||||||
import org.springframework.context.support.AbstractResourceBasedMessageSource;
|
|
||||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.hateoas.client.LinkDiscoverer;
|
import org.springframework.hateoas.client.LinkDiscoverer;
|
||||||
@@ -63,6 +62,9 @@ import org.springframework.util.ClassUtils;
|
|||||||
@EnablePluginRegistries({ LinkDiscoverer.class })
|
@EnablePluginRegistries({ LinkDiscoverer.class })
|
||||||
public class HateoasConfiguration {
|
public class HateoasConfiguration {
|
||||||
|
|
||||||
|
static String I18N_BASE_NAME = "rest-messages";
|
||||||
|
static String I18N_DEFAULTS_BASE_NAME = "rest-default-messages";
|
||||||
|
|
||||||
private @Autowired ApplicationContext context;
|
private @Autowired ApplicationContext context;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -119,14 +121,15 @@ public class HateoasConfiguration {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private final AbstractMessageSource lookupMessageSource() {
|
private final AbstractMessageSource lookupMessageSource() {
|
||||||
|
|
||||||
List<Resource> candidates = loadProperties("rest-default-messages", false);
|
List<Resource> candidates = loadResourceBundleResources(I18N_DEFAULTS_BASE_NAME, false);
|
||||||
|
|
||||||
if (candidates.isEmpty() && loadProperties("rest-messages", true).isEmpty()) {
|
if (candidates.isEmpty() && loadResourceBundleResources(I18N_BASE_NAME, true).isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractResourceBasedMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||||
messageSource.setBasename("classpath:rest-messages");
|
messageSource.setResourceLoader(context);
|
||||||
|
messageSource.setBasename(I18N_BASE_NAME);
|
||||||
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
|
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
|
||||||
|
|
||||||
if (!candidates.isEmpty()) {
|
if (!candidates.isEmpty()) {
|
||||||
@@ -139,10 +142,8 @@ public class HateoasConfiguration {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private final Properties loadProperties(List<Resource> sources) {
|
private final Properties loadProperties(List<Resource> sources) {
|
||||||
|
|
||||||
Resource[] resources = loadProperties("rest-default-messages", false).stream().toArray(Resource[]::new);
|
|
||||||
|
|
||||||
PropertiesFactoryBean factory = new PropertiesFactoryBean();
|
PropertiesFactoryBean factory = new PropertiesFactoryBean();
|
||||||
factory.setLocations(resources);
|
factory.setLocations(sources.toArray(new Resource[sources.size()]));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@@ -154,7 +155,7 @@ public class HateoasConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final List<Resource> loadProperties(String baseName, boolean withWildcard) {
|
private final List<Resource> loadResourceBundleResources(String baseName, boolean withWildcard) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Arrays //
|
return Arrays //
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
package org.springframework.hateoas.config;
|
package org.springframework.hateoas.config;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.springframework.hateoas.mediatype.hal.HalConfiguration.RenderSingleLinks.*;
|
import static org.springframework.hateoas.mediatype.hal.HalConfiguration.RenderSingleLinks.*;
|
||||||
import static org.springframework.hateoas.support.ContextTester.*;
|
import static org.springframework.hateoas.support.ContextTester.*;
|
||||||
@@ -537,9 +538,23 @@ class EnableHypermediaSupportIntegrationTest {
|
|||||||
@Test // #1019
|
@Test // #1019
|
||||||
void registersMessageResolverIfMessagesBundleAvailable() {
|
void registersMessageResolverIfMessagesBundleAvailable() {
|
||||||
|
|
||||||
withServletContext(HateoasConfiguration.class, simulateResourceBundle(), context -> {
|
String originalBaseName = HateoasConfiguration.I18N_BASE_NAME;
|
||||||
assertThat(context.getBean(MessageResolver.class)).isNotEqualTo(MessageResolver.of(null));
|
|
||||||
});
|
try {
|
||||||
|
|
||||||
|
HateoasConfiguration.I18N_BASE_NAME = "org/springframework/hateoas/config/rest-messages";
|
||||||
|
|
||||||
|
withServletContext(HateoasConfiguration.class, simulateResourceBundle(), context -> {
|
||||||
|
|
||||||
|
MessageResolver bean = context.getBean(MessageResolver.class);
|
||||||
|
|
||||||
|
assertThat(bean).isNotEqualTo(MessageResolver.of(null));
|
||||||
|
assertThat(bean.resolve(() -> new String[] { "key" })).isEqualTo("Schlüssel");
|
||||||
|
});
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
HateoasConfiguration.I18N_BASE_NAME = originalBaseName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // #1019, DATAREST-686
|
@Test // #1019, DATAREST-686
|
||||||
@@ -654,7 +669,7 @@ class EnableHypermediaSupportIntegrationTest {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
doReturn(new Resource[0]).when(spy).getResources("classpath:rest-default-messages.properties");
|
doReturn(new Resource[0]).when(spy).getResources("classpath:rest-default-messages.properties");
|
||||||
doReturn(new Resource[] { resource }).when(spy).getResources("classpath:rest-messages*.properties");
|
doReturn(new Resource[] { resource }).when(spy).getResources(contains("rest-messages"));
|
||||||
|
|
||||||
} catch (IOException o_O) {
|
} catch (IOException o_O) {
|
||||||
fail("Couldn't mock resource lookup!", o_O);
|
fail("Couldn't mock resource lookup!", o_O);
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
key=Schlüssel
|
||||||
|
|||||||
Reference in New Issue
Block a user