7
7
package org .hibernate .validator .messageinterpolation ;
8
8
9
9
import static org .hibernate .validator .internal .util .ConcurrentReferenceHashMap .ReferenceType .SOFT ;
10
+ import static org .hibernate .validator .internal .util .logging .Messages .MESSAGES ;
10
11
11
12
import java .lang .invoke .MethodHandles ;
12
13
import java .util .Collections ;
20
21
import java .util .regex .Pattern ;
21
22
22
23
import javax .validation .MessageInterpolator ;
23
- import javax .validation .ValidationException ;
24
24
25
+ import org .hibernate .validator .Incubating ;
25
26
import org .hibernate .validator .internal .engine .messageinterpolation .InterpolationTermType ;
26
27
import org .hibernate .validator .internal .engine .messageinterpolation .LocalizedMessage ;
27
28
import org .hibernate .validator .internal .engine .messageinterpolation .parser .MessageDescriptorFormatException ;
28
29
import org .hibernate .validator .internal .engine .messageinterpolation .parser .Token ;
29
30
import org .hibernate .validator .internal .engine .messageinterpolation .parser .TokenCollector ;
30
31
import org .hibernate .validator .internal .engine .messageinterpolation .parser .TokenIterator ;
31
32
import org .hibernate .validator .internal .util .ConcurrentReferenceHashMap ;
33
+ import org .hibernate .validator .internal .util .Contracts ;
32
34
import org .hibernate .validator .internal .util .logging .Log ;
33
35
import org .hibernate .validator .internal .util .logging .LoggerFactory ;
34
36
import org .hibernate .validator .resourceloading .PlatformResourceBundleLocator ;
@@ -131,7 +133,7 @@ public abstract class AbstractMessageInterpolator implements MessageInterpolator
131
133
* {@code MessageInterpolator} using the default resource bundle locators.
132
134
*/
133
135
public AbstractMessageInterpolator () {
134
- this ( Collections .emptySet () );
136
+ this ( Collections .emptySet (), Locale . getDefault () );
135
137
}
136
138
137
139
/**
@@ -140,7 +142,7 @@ public AbstractMessageInterpolator() {
140
142
* @param userResourceBundleLocator {@code ResourceBundleLocator} used to load user provided resource bundle
141
143
*/
142
144
public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator ) {
143
- this ( userResourceBundleLocator , Collections .emptySet () );
145
+ this ( userResourceBundleLocator , Collections .emptySet (), Locale . getDefault () );
144
146
}
145
147
146
148
/**
@@ -152,7 +154,7 @@ public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocat
152
154
*/
153
155
public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator ,
154
156
ResourceBundleLocator contributorResourceBundleLocator ) {
155
- this ( userResourceBundleLocator , contributorResourceBundleLocator , Collections .emptySet () );
157
+ this ( userResourceBundleLocator , contributorResourceBundleLocator , Collections .emptySet (), Locale . getDefault () );
156
158
}
157
159
158
160
/**
@@ -166,61 +168,74 @@ public AbstractMessageInterpolator(ResourceBundleLocator userResourceBundleLocat
166
168
public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator ,
167
169
ResourceBundleLocator contributorResourceBundleLocator ,
168
170
boolean cacheMessages ) {
169
- this ( userResourceBundleLocator , contributorResourceBundleLocator , Collections .emptySet (), cacheMessages );
171
+ this ( userResourceBundleLocator , contributorResourceBundleLocator , Collections .emptySet (), Locale . getDefault (), cacheMessages );
170
172
}
171
173
172
174
/**
173
175
* {@code MessageInterpolator} using the default resource bundle locators.
174
176
*
175
- * @param localesToInitialize The set of locales to initialize at bootstrap.
177
+ * @param localesToInitialize the set of locales to initialize at bootstrap
178
+ * @param defaultLocale the default locale
176
179
*
177
- * @since 6.1
180
+ * @since 6.1.1
178
181
*/
179
- public AbstractMessageInterpolator (Set <Locale > localesToInitialize ) {
180
- this ( null , localesToInitialize );
182
+ @ Incubating
183
+ public AbstractMessageInterpolator (Set <Locale > localesToInitialize , Locale defaultLocale ) {
184
+ this ( null , localesToInitialize , defaultLocale );
181
185
}
182
186
183
187
/**
184
188
* {@code MessageInterpolator} taking a resource bundle locator.
185
189
*
186
190
* @param userResourceBundleLocator {@code ResourceBundleLocator} used to load user provided resource bundle
187
- * @param localesToInitialize The set of locales to initialize at bootstrap.
191
+ * @param localesToInitialize the set of locales to initialize at bootstrap
192
+ * @param defaultLocale the default locale
188
193
*
189
- * @since 6.1
194
+ * @since 6.1.1
190
195
*/
191
- public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator , Set <Locale > localesToInitialize ) {
192
- this ( userResourceBundleLocator , null , localesToInitialize );
196
+ @ Incubating
197
+ public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator , Set <Locale > localesToInitialize , Locale defaultLocale ) {
198
+ this ( userResourceBundleLocator , null , localesToInitialize , defaultLocale );
193
199
}
194
200
195
201
/**
196
202
* {@code MessageInterpolator} taking two resource bundle locators.
197
203
*
198
204
* @param userResourceBundleLocator {@code ResourceBundleLocator} used to load user provided resource bundle
199
205
* @param contributorResourceBundleLocator {@code ResourceBundleLocator} used to load resource bundle of constraint contributor
200
- * @param localesToInitialize The set of locales to initialize at bootstrap.
206
+ * @param localesToInitialize the set of locales to initialize at bootstrap
207
+ * @param defaultLocale the default locale
201
208
*
202
- * @since 6.1
209
+ * @since 6.1.1
203
210
*/
211
+ @ Incubating
204
212
public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator ,
205
- ResourceBundleLocator contributorResourceBundleLocator , Set <Locale > localesToInitialize ) {
206
- this ( userResourceBundleLocator , contributorResourceBundleLocator , localesToInitialize , true );
213
+ ResourceBundleLocator contributorResourceBundleLocator , Set <Locale > localesToInitialize ,
214
+ Locale defaultLocale ) {
215
+ this ( userResourceBundleLocator , contributorResourceBundleLocator , localesToInitialize , defaultLocale , true );
207
216
}
208
217
209
218
/**
210
219
* {@code MessageInterpolator} taking two resource bundle locators.
211
220
*
212
221
* @param userResourceBundleLocator {@code ResourceBundleLocator} used to load user provided resource bundle
213
222
* @param contributorResourceBundleLocator {@code ResourceBundleLocator} used to load resource bundle of constraint contributor
214
- * @param localesToInitialize The set of locales to initialize at bootstrap.
215
- * @param cacheMessages Whether resolved messages should be cached or not.
223
+ * @param localesToInitialize the set of locales to initialize at bootstrap
224
+ * @param defaultLocale the default locale
225
+ * @param cacheMessages whether resolved messages should be cached or not
216
226
*
217
- * @since 6.1
227
+ * @since 6.1.1
218
228
*/
229
+ @ Incubating
219
230
public AbstractMessageInterpolator (ResourceBundleLocator userResourceBundleLocator ,
220
231
ResourceBundleLocator contributorResourceBundleLocator ,
221
232
Set <Locale > localesToInitialize ,
233
+ Locale defaultLocale ,
222
234
boolean cacheMessages ) {
223
- defaultLocale = Locale .getDefault ();
235
+ Contracts .assertNotNull ( localesToInitialize , MESSAGES .parameterMustNotBeNull ( "localesToInitialize" ) );
236
+ Contracts .assertNotNull ( defaultLocale , MESSAGES .parameterMustNotBeNull ( "defaultLocale" ) );
237
+
238
+ this .defaultLocale = defaultLocale ;
224
239
225
240
if ( userResourceBundleLocator == null ) {
226
241
this .userResourceBundleLocator = new PlatformResourceBundleLocator ( USER_VALIDATION_MESSAGES , localesToInitialize );
@@ -297,7 +312,7 @@ public String interpolate(String message, Context context, Locale locale) {
297
312
try {
298
313
interpolatedMessage = interpolateMessage ( message , context , locale );
299
314
}
300
- catch (ValidationException e ) {
315
+ catch (MessageDescriptorFormatException e ) {
301
316
LOG .warn ( e .getMessage () );
302
317
}
303
318
return interpolatedMessage ;
0 commit comments