From 2302b9b48b952f254ca96df71855b716bdbfddfd Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 6 Mar 2013 09:53:12 +0100 Subject: [PATCH] Added locale-independent "commonMessages" property to AbstractMessageSource Issue: SPR-10291 --- .../support/AbstractMessageSource.java | 31 ++++++++++++++++++- .../ResourceBundleMessageSourceTests.java | 12 +++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java index 768c963d3b5..ddf87c93324 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java @@ -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. + *

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); } diff --git a/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java b/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java index c0121a4a2dd..3a265516cdf 100644 --- a/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java @@ -249,6 +249,18 @@ public class ResourceBundleMessageSourceTests extends TestCase { assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN)); } + public void testReloadableResourceBundleMessageSourceWithCommonMessages() { + ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); + Properties commonMessages = new Properties(); + commonMessages.setProperty("warning", "Do not do {0}"); + ms.setCommonMessages(commonMessages); + ms.setBasename("org/springframework/context/support/messages"); + assertEquals("message1", ms.getMessage("code1", null, Locale.ENGLISH)); + assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN)); + assertEquals("Do not do this", ms.getMessage("warning", new Object[] {"this"}, Locale.ENGLISH)); + assertEquals("Do not do that", ms.getMessage("warning", new Object[] {"that"}, Locale.GERMAN)); + } + public void testReloadableResourceBundleMessageSourceWithWhitespaceInBasename() { ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); ms.setBasename(" org/springframework/context/support/messages ");