Skip to content

Composed MessageSource [SPR-16614] #21155

@spring-projects-issues

Description

@spring-projects-issues

Dmytro Nosan opened SPR-16614 and commented

Sometimes to localize some bean we have to use several sources. e.g.

MessageSource m1 = db.getTranslations()
MessageSource m2 = someService.getTranslations();

and then we will use a custom method for localizing

private String translate(MessageSource m1, MessageSource m2, String code, Locale locale) {
		try {
			return m1.getMessage(code, null, locale);
		} catch (NoSuchMessageException ex) {
			return m2.getMessage(code, null, locale);
		}
	}

But this method has several disadvantages:

  1. If m1 has (useCodeAsDefaultMessage) we will never get a message from m2.
  2. A lot of exceptions could be thrown
  3. What would we do, if we need one or more MessageSource(s)?

My idea is to create a new ComposedMessageSource class which should handle:

  1. Resolve message from both MessageSources
  2. Resolve situation when one of the MessageSources has a property useCodeAsDefaultMessage (we should find the best option for localization)
  3. Unnecessary exception generation

also, it would be great to have a utility class for composing MessageSources.

public abstract class MessageSources {
	@Nullable
	public static MessageSource compose(@Nullable MessageSource messageSource, @Nullable MessageSource parentMessageSource){
...
}

Affects: 5.0.4

Attachments:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions