1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2017 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.
@@ -160,30 +160,21 @@ public final String getMessage(String code, Object[] args, Locale locale) throws
160
160
}
161
161
162
162
@ Override
163
- public final String getMessage (MessageSourceResolvable resolvable , Locale locale )
164
- throws NoSuchMessageException {
165
-
163
+ public final String getMessage (MessageSourceResolvable resolvable , Locale locale ) throws NoSuchMessageException {
166
164
String [] codes = resolvable .getCodes ();
167
- if (codes == null ) {
168
- codes = new String [0 ];
169
- }
170
- for (String code : codes ) {
171
- String msg = getMessageInternal (code , resolvable .getArguments (), locale );
172
- if (msg != null ) {
173
- return msg ;
165
+ if (codes != null ) {
166
+ for (String code : codes ) {
167
+ String message = getMessageInternal (code , resolvable .getArguments (), locale );
168
+ if (message != null ) {
169
+ return message ;
170
+ }
174
171
}
175
172
}
176
- String defaultMessage = resolvable . getDefaultMessage ();
173
+ String defaultMessage = getDefaultMessage (resolvable , locale );
177
174
if (defaultMessage != null ) {
178
- return renderDefaultMessage ( defaultMessage , resolvable . getArguments (), locale ) ;
175
+ return defaultMessage ;
179
176
}
180
- if (codes .length > 0 ) {
181
- String fallback = getDefaultMessage (codes [0 ]);
182
- if (fallback != null ) {
183
- return fallback ;
184
- }
185
- }
186
- throw new NoSuchMessageException (codes .length > 0 ? codes [codes .length - 1 ] : null , locale );
177
+ throw new NoSuchMessageException (!ObjectUtils .isEmpty (codes ) ? codes [codes .length - 1 ] : null , locale );
187
178
}
188
179
189
180
@@ -194,7 +185,7 @@ public final String getMessage(MessageSourceResolvable resolvable, Locale locale
194
185
* @param code the code to lookup up, such as 'calculator.noRateSet'
195
186
* @param args array of arguments that will be filled in for params
196
187
* within the message
197
- * @param locale the Locale in which to do the lookup
188
+ * @param locale the locale in which to do the lookup
198
189
* @return the resolved message, or {@code null} if not found
199
190
* @see #getMessage(String, Object[], String, Locale)
200
191
* @see #getMessage(String, Object[], Locale)
@@ -249,11 +240,11 @@ protected String getMessageInternal(String code, Object[] args, Locale locale) {
249
240
}
250
241
251
242
/**
252
- * Try to retrieve the given message from the parent MessageSource, if any.
243
+ * Try to retrieve the given message from the parent {@code MessageSource} , if any.
253
244
* @param code the code to lookup up, such as 'calculator.noRateSet'
254
245
* @param args array of arguments that will be filled in for params
255
246
* within the message
256
- * @param locale the Locale in which to do the lookup
247
+ * @param locale the locale in which to do the lookup
257
248
* @return the resolved message, or {@code null} if not found
258
249
* @see #getParentMessageSource()
259
250
*/
@@ -274,11 +265,36 @@ protected String getMessageFromParent(String code, Object[] args, Locale locale)
274
265
return null ;
275
266
}
276
267
268
+ /**
269
+ * Get a default message for the given {@code MessageSourceResolvable}.
270
+ * <p>This implementation fully renders the default message if available,
271
+ * or just returns the plain default message {@code String} if the primary
272
+ * message code is being used as a default message.
273
+ * @param resolvable the value object to resolve a default message for
274
+ * @param locale the current locale
275
+ * @return the default message, or {@code null} if none
276
+ * @since 4.3.6
277
+ * @see #renderDefaultMessage(String, Object[], Locale)
278
+ * @see #getDefaultMessage(String)
279
+ */
280
+ protected String getDefaultMessage (MessageSourceResolvable resolvable , Locale locale ) {
281
+ String defaultMessage = resolvable .getDefaultMessage ();
282
+ String [] codes = resolvable .getCodes ();
283
+ if (defaultMessage != null ) {
284
+ if (!ObjectUtils .isEmpty (codes ) && defaultMessage .equals (codes [0 ])) {
285
+ // Never format a code-as-default-message, even with alwaysUseMessageFormat=true
286
+ return defaultMessage ;
287
+ }
288
+ return renderDefaultMessage (defaultMessage , resolvable .getArguments (), locale );
289
+ }
290
+ return (!ObjectUtils .isEmpty (codes ) ? getDefaultMessage (codes [0 ]) : null );
291
+ }
292
+
277
293
/**
278
294
* Return a fallback default message for the given code, if any.
279
295
* <p>Default is to return the code itself if "useCodeAsDefaultMessage" is activated,
280
296
* or return no fallback else. In case of no fallback, the caller will usually
281
- * receive a NoSuchMessageException from {@code getMessage}.
297
+ * receive a {@code NoSuchMessageException} from {@code getMessage}.
282
298
* @param code the message code that we couldn't resolve
283
299
* and that we didn't receive an explicit default message for
284
300
* @return the default message to use, or {@code null} if none
@@ -328,7 +344,7 @@ protected Object[] resolveArguments(Object[] args, Locale locale) {
328
344
* pattern doesn't contain argument placeholders in the first place. Therefore,
329
345
* it is advisable to circumvent MessageFormat for messages without arguments.
330
346
* @param code the code of the message to resolve
331
- * @param locale the Locale to resolve the code for
347
+ * @param locale the locale to resolve the code for
332
348
* (subclasses are encouraged to support internationalization)
333
349
* @return the message String, or {@code null} if not found
334
350
* @see #resolveCode
@@ -352,7 +368,7 @@ protected String resolveCodeWithoutArguments(String code, Locale locale) {
352
368
* for messages without arguments, not involving MessageFormat.</b>
353
369
* See the {@link #resolveCodeWithoutArguments} javadoc for details.
354
370
* @param code the code of the message to resolve
355
- * @param locale the Locale to resolve the code for
371
+ * @param locale the locale to resolve the code for
356
372
* (subclasses are encouraged to support internationalization)
357
373
* @return the MessageFormat for the message, or {@code null} if not found
358
374
* @see #resolveCodeWithoutArguments(String, java.util.Locale)
0 commit comments