1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -52,7 +52,8 @@ public abstract class MessageSourceSupport {
52
52
* Used for passed-in default messages. MessageFormats for resolved
53
53
* codes are cached on a specific basis in subclasses.
54
54
*/
55
- private final Map <String , MessageFormat > cachedMessageFormats = new HashMap <String , MessageFormat >();
55
+ private final Map <String , Map <Locale , MessageFormat >> messageFormatsPerMessage =
56
+ new HashMap <String , Map <Locale , MessageFormat >>();
56
57
57
58
58
59
/**
@@ -114,9 +115,16 @@ protected String formatMessage(String msg, Object[] args, Locale locale) {
114
115
if (msg == null || (!this .alwaysUseMessageFormat && ObjectUtils .isEmpty (args ))) {
115
116
return msg ;
116
117
}
117
- MessageFormat messageFormat ;
118
- synchronized (this .cachedMessageFormats ) {
119
- messageFormat = this .cachedMessageFormats .get (msg );
118
+ MessageFormat messageFormat = null ;
119
+ synchronized (this .messageFormatsPerMessage ) {
120
+ Map <Locale , MessageFormat > messageFormatsPerLocale = this .messageFormatsPerMessage .get (msg );
121
+ if (messageFormatsPerLocale != null ) {
122
+ messageFormat = messageFormatsPerLocale .get (locale );
123
+ }
124
+ else {
125
+ messageFormatsPerLocale = new HashMap <Locale , MessageFormat >();
126
+ this .messageFormatsPerMessage .put (msg , messageFormatsPerLocale );
127
+ }
120
128
if (messageFormat == null ) {
121
129
try {
122
130
messageFormat = createMessageFormat (msg , locale );
@@ -130,7 +138,7 @@ protected String formatMessage(String msg, Object[] args, Locale locale) {
130
138
// silently proceed with raw message if format not enforced
131
139
messageFormat = INVALID_MESSAGE_FORMAT ;
132
140
}
133
- this . cachedMessageFormats . put (msg , messageFormat );
141
+ messageFormatsPerLocale . put (locale , messageFormat );
134
142
}
135
143
}
136
144
if (messageFormat == INVALID_MESSAGE_FORMAT ) {
@@ -153,9 +161,8 @@ protected MessageFormat createMessageFormat(String msg, Locale locale) {
153
161
154
162
/**
155
163
* Template method for resolving argument objects.
156
- * <p>The default implementation simply returns the given argument
157
- * array as-is. Can be overridden in subclasses in order to resolve
158
- * special argument types.
164
+ * <p>The default implementation simply returns the given argument array as-is.
165
+ * Can be overridden in subclasses in order to resolve special argument types.
159
166
* @param args the original argument array
160
167
* @param locale the Locale to resolve against
161
168
* @return the resolved argument array
0 commit comments