commit
a3db04f872
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -34,6 +34,8 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
*/
|
*/
|
||||||
class MessageSourceMessageInterpolator implements MessageInterpolator {
|
class MessageSourceMessageInterpolator implements MessageInterpolator {
|
||||||
|
|
||||||
|
private static final String DEFAULT_MESSAGE = MessageSourceMessageInterpolator.class.getName();
|
||||||
|
|
||||||
private static final char PREFIX = '{';
|
private static final char PREFIX = '{';
|
||||||
|
|
||||||
private static final char SUFFIX = '}';
|
private static final char SUFFIX = '}';
|
||||||
|
@ -115,13 +117,11 @@ class MessageSourceMessageInterpolator implements MessageInterpolator {
|
||||||
|
|
||||||
private String replaceParameter(String parameter, Locale locale, Set<String> visitedParameters) {
|
private String replaceParameter(String parameter, Locale locale, Set<String> visitedParameters) {
|
||||||
parameter = replaceParameters(parameter, locale, visitedParameters);
|
parameter = replaceParameters(parameter, locale, visitedParameters);
|
||||||
String value = this.messageSource.getMessage(parameter, null, null, locale);
|
String value = this.messageSource.getMessage(parameter, null, DEFAULT_MESSAGE, locale);
|
||||||
return (value != null && !isUsingCodeAsDefaultMessage(value, parameter))
|
if (value == null || value.equals(DEFAULT_MESSAGE)) {
|
||||||
? replaceParameters(value, locale, visitedParameters) : null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return replaceParameters(value, locale, visitedParameters);
|
||||||
private boolean isUsingCodeAsDefaultMessage(String value, String parameter) {
|
|
||||||
return value.equals(parameter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -66,6 +66,19 @@ class MessageSourceMessageInterpolatorTests {
|
||||||
.isEqualTo("{foo}{child}+{child}{bar}");
|
.isEqualTo("{foo}{child}+{child}{bar}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateShouldReplaceParameterThatReferencesAMessageThatMatchesItsCode() {
|
||||||
|
this.messageSource.addMessage("foo", Locale.getDefault(), "foo");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}", this.context)).isEqualTo("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpolateUsingCodeAsDefaultShouldReplaceParameterThatReferencesAMessageThatMatchesItsCode() {
|
||||||
|
this.messageSource.setUseCodeAsDefaultMessage(true);
|
||||||
|
this.messageSource.addMessage("foo", Locale.getDefault(), "foo");
|
||||||
|
assertThat(this.interpolator.interpolate("{foo}", this.context)).isEqualTo("foo");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() {
|
void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() {
|
||||||
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
|
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
|
||||||
|
|
Loading…
Reference in New Issue