- Notifications
You must be signed in to change notification settings - Fork 41.6k
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
mmoayyed wants to merge 5 commits into spring-projects:main from mmoayyed:message-source-common-messages
Closed
Changes from 1 commit
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
a9b64dc Allow common messages to be specified for message sources
mmoayyed 7d70b39 updates after code review
mmoayyed 2aea08f Merge branch 'main' into message-source-common-messages
mmoayyed 298899c updates after code review
mmoayyed 2015f53 updates after code review
mmoayyed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow common messages to be specified for message sources
Extend the configuration schema to support the specification of common messages, when defining a messageSource bean
- Loading branch information
commit a9b64dc220ff8eeffd73a5c21cefc816dbac51cb
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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); | ||
| ||
| propertiesFactory.afterPropertiesSet(); | ||
| messageSource.setCommonMessages(propertiesFactory.getObject()); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to load common messages", e); | ||
| } | ||
| return messageSource; | ||
| } | ||
| | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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; | ||
| ||
| | ||
| /** | ||
| * 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions 1 ...boot-project/spring-boot-autoconfigure/src/test/resources/test/common-messages.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| hello=world |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.fillPropertieshere rather thanPropertiesFactoryBean. Using aFactoryBeanisn't ideal since we need to callafterPropertiesSetetc.