-
Notifications
You must be signed in to change notification settings - Fork 41.3k
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
Changes from 1 commit
a9b64dc
7d70b39
2aea08f
298899c
2015f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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(); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,11 @@ public class MessageSourceProperties { | |
*/ | ||
private String basename = "messages"; | ||
|
||
/** | ||
* Comma-separated list of locale-independent common messages. | ||
*/ | ||
private String commonMessages; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we might be better off using a |
||
|
||
/** | ||
* Message bundles encoding. | ||
*/ | ||
|
@@ -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 |
---|---|---|
@@ -0,0 +1 @@ | ||
hello=world |
There was a problem hiding this comment.
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 thanPropertiesFactoryBean
. Using aFactoryBean
isn't ideal since we need to callafterPropertiesSet
etc.