Skip to content

Allow common messages to be specified for message sources #42472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

package org.springframework.boot.autoconfigure.context;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -39,7 +45,9 @@
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ConcurrentReferenceHashMap;
Expand Down Expand Up @@ -81,6 +89,25 @@ public MessageSource messageSource(MessageSourceProperties properties) {
}
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());

try {
if (StringUtils.hasText(properties.getCommonMessages())) {
PropertiesFactoryBean propertiesFactory = new PropertiesFactoryBean();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be better off using PropertiesLoaderUtils.fillProperties here rather than PropertiesFactoryBean. Using a FactoryBean isn't ideal since we need to call afterPropertiesSet etc.

ResourceLoader resourceLoader = new DefaultResourceLoader();
String[] commonMessages = StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(properties.getCommonMessages()));
List<Resource> commonResources = Arrays.stream(commonMessages)
.map(resourceLoader::getResource)
.toList();
propertiesFactory.setLocations(commonResources.toArray(Resource[]::new));
propertiesFactory.setSingleton(true);
propertiesFactory.setIgnoreResourceNotFound(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should fail hard if the resource is not found.

propertiesFactory.afterPropertiesSet();
messageSource.setCommonMessages(propertiesFactory.getObject());
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to load common messages", e);
}
return messageSource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class MessageSourceProperties {
*/
private String basename = "messages";

/**
* Comma-separated list of locale-independent common messages.
*/
private String commonMessages;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might be better off using a List<Resource> here, that way the Binder can take care of creating the resource instances.


/**
* Message bundles encoding.
*/
Expand Down Expand Up @@ -121,4 +126,11 @@ public void setUseCodeAsDefaultMessage(boolean useCodeAsDefaultMessage) {
this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
}

public String getCommonMessages() {
return this.commonMessages;
}

public void setCommonMessages(String commonMessages) {
this.commonMessages = commonMessages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ void testMessageSourceFromPropertySourceAnnotation() {
.run((context) -> assertThat(context.getMessage("foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
}

@Test
void testCommonMessages() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages", "spring.messages.common-messages:test/common-messages")
.run((context) -> assertThat(context.getMessage("hello", null, "Hello!", Locale.UK)).isEqualTo("world"));
}

@Test
void testFallbackDefault() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello=world
Loading