diff --git a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java index 58e6b9b5d76c..b7601375a70b 100644 --- a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java +++ b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -52,7 +52,8 @@ public abstract class MessageSourceSupport { * Used for passed-in default messages. MessageFormats for resolved * codes are cached on a specific basis in subclasses. */ - private final Map cachedMessageFormats = new HashMap(); + private final Map> messageFormatsPerMessage = + new HashMap>(); /** @@ -114,9 +115,16 @@ protected String formatMessage(String msg, Object[] args, Locale locale) { if (msg == null || (!this.alwaysUseMessageFormat && ObjectUtils.isEmpty(args))) { return msg; } - MessageFormat messageFormat; - synchronized (this.cachedMessageFormats) { - messageFormat = this.cachedMessageFormats.get(msg); + MessageFormat messageFormat = null; + synchronized (this.messageFormatsPerMessage) { + Map messageFormatsPerLocale = this.messageFormatsPerMessage.get(msg); + if (messageFormatsPerLocale != null) { + messageFormat = messageFormatsPerLocale.get(locale); + } + else { + messageFormatsPerLocale = new HashMap(); + this.messageFormatsPerMessage.put(msg, messageFormatsPerLocale); + } if (messageFormat == null) { try { messageFormat = createMessageFormat(msg, locale); @@ -130,7 +138,7 @@ protected String formatMessage(String msg, Object[] args, Locale locale) { // silently proceed with raw message if format not enforced messageFormat = INVALID_MESSAGE_FORMAT; } - this.cachedMessageFormats.put(msg, messageFormat); + messageFormatsPerLocale.put(locale, messageFormat); } } if (messageFormat == INVALID_MESSAGE_FORMAT) { @@ -153,9 +161,8 @@ protected MessageFormat createMessageFormat(String msg, Locale locale) { /** * Template method for resolving argument objects. - *

The default implementation simply returns the given argument - * array as-is. Can be overridden in subclasses in order to resolve - * special argument types. + *

The default implementation simply returns the given argument array as-is. + * Can be overridden in subclasses in order to resolve special argument types. * @param args the original argument array * @param locale the Locale to resolve against * @return the resolved argument array