Added locale-independent "commonMessages" property to AbstractMessageSource
Issue: SPR-10291
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,7 @@ import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.context.HierarchicalMessageSource;
|
||||
import org.springframework.context.MessageSource;
|
||||
@@ -64,6 +65,8 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
|
||||
|
||||
private MessageSource parentMessageSource;
|
||||
|
||||
private Properties commonMessages;
|
||||
|
||||
private boolean useCodeAsDefaultMessage = false;
|
||||
|
||||
|
||||
@@ -75,6 +78,23 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
|
||||
return this.parentMessageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify locale-independent common messages, with the message code as key
|
||||
* and the full message String (may contain argument placeholders) as value.
|
||||
* <p>May also link to an externally defined Properties object, e.g. defined
|
||||
* through a {@link org.springframework.beans.factory.config.PropertiesFactoryBean}.
|
||||
*/
|
||||
public void setCommonMessages(Properties commonMessages) {
|
||||
this.commonMessages = commonMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Properties object defining locale-independent common messages, if any.
|
||||
*/
|
||||
protected Properties getCommonMessages() {
|
||||
return this.commonMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to use the message code as default message instead of
|
||||
* throwing a NoSuchMessageException. Useful for development and debugging.
|
||||
@@ -210,6 +230,15 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
|
||||
}
|
||||
}
|
||||
|
||||
// Check locale-independent common messages for the given message code.
|
||||
Properties commonMessages = getCommonMessages();
|
||||
if (commonMessages != null) {
|
||||
String commonMessage = commonMessages.getProperty(code);
|
||||
if (commonMessage != null) {
|
||||
return formatMessage(commonMessage, args, locale);
|
||||
}
|
||||
}
|
||||
|
||||
// Not found -> check parent, if any.
|
||||
return getMessageFromParent(code, argsToUse, locale);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user