46
46
import org .hibernate .validator .internal .engine .valueextraction .ValueExtractorManager ;
47
47
import org .hibernate .validator .internal .properties .DefaultGetterPropertySelectionStrategy ;
48
48
import org .hibernate .validator .internal .properties .javabean .JavaBeanHelper ;
49
+ import org .hibernate .validator .internal .util .CollectionHelper ;
49
50
import org .hibernate .validator .internal .util .Contracts ;
50
51
import org .hibernate .validator .internal .util .Version ;
51
52
import org .hibernate .validator .internal .util .logging .Log ;
71
72
* @author Gunnar Morling
72
73
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
73
74
* @author Chris Beckey <cbeckey@paypal.com>
75
+ * @author Guillaume Smet
74
76
*/
75
77
public abstract class AbstractConfigurationImpl <T extends BaseHibernateValidatorConfiguration <T >>
76
78
implements BaseHibernateValidatorConfiguration <T >, ConfigurationState {
@@ -113,6 +115,7 @@ public abstract class AbstractConfigurationImpl<T extends BaseHibernateValidator
113
115
private Duration temporalValidationTolerance ;
114
116
private Object constraintValidatorPayload ;
115
117
private GetterPropertySelectionStrategy getterPropertySelectionStrategy ;
118
+ private Set <Locale > locales = Collections .emptySet ();
116
119
private Locale defaultLocale = Locale .getDefault ();
117
120
private LocaleResolver localeResolver ;
118
121
@@ -338,6 +341,14 @@ public T getterPropertySelectionStrategy(GetterPropertySelectionStrategy getterP
338
341
return thisAsT ();
339
342
}
340
343
344
+ @ Override
345
+ public T locales (Set <Locale > locales ) {
346
+ Contracts .assertNotNull ( defaultLocale , MESSAGES .parameterMustNotBeNull ( "locales" ) );
347
+
348
+ this .locales = locales ;
349
+ return thisAsT ();
350
+ }
351
+
341
352
@ Override
342
353
public T defaultLocale (Locale defaultLocale ) {
343
354
Contracts .assertNotNull ( defaultLocale , MESSAGES .parameterMustNotBeNull ( "defaultLocale" ) );
@@ -543,8 +554,9 @@ public ClassLoader getExternalClassLoader() {
543
554
@ Override
544
555
public final MessageInterpolator getDefaultMessageInterpolator () {
545
556
if ( defaultMessageInterpolator == null ) {
546
- defaultMessageInterpolator = new ResourceBundleMessageInterpolator ( getDefaultResourceBundleLocator (), getAllLocalesToInitialize (),
547
- defaultLocale , ValidatorFactoryConfigurationHelper .determineLocaleResolver ( this , this .getProperties (), externalClassLoader ) );
557
+ defaultMessageInterpolator = new ResourceBundleMessageInterpolator ( getDefaultResourceBundleLocator (), getAllSupportedLocales (),
558
+ defaultLocale , ValidatorFactoryConfigurationHelper .determineLocaleResolver ( this , this .getProperties (), externalClassLoader ),
559
+ preloadResourceBundles () );
548
560
}
549
561
550
562
return defaultMessageInterpolator ;
@@ -564,7 +576,7 @@ public final ConstraintValidatorFactory getDefaultConstraintValidatorFactory() {
564
576
public final ResourceBundleLocator getDefaultResourceBundleLocator () {
565
577
if ( defaultResourceBundleLocator == null ) {
566
578
defaultResourceBundleLocator = new PlatformResourceBundleLocator (
567
- ResourceBundleMessageInterpolator .USER_VALIDATION_MESSAGES , getAllLocalesToInitialize () );
579
+ ResourceBundleMessageInterpolator .USER_VALIDATION_MESSAGES , preloadResourceBundles (), getAllSupportedLocales () );
568
580
}
569
581
570
582
return defaultResourceBundleLocator ;
@@ -714,12 +726,14 @@ private MessageInterpolator getDefaultMessageInterpolatorConfiguredWithClassLoad
714
726
if ( externalClassLoader != null ) {
715
727
PlatformResourceBundleLocator userResourceBundleLocator = new PlatformResourceBundleLocator (
716
728
ResourceBundleMessageInterpolator .USER_VALIDATION_MESSAGES ,
717
- getAllLocalesToInitialize (),
729
+ preloadResourceBundles (),
730
+ getAllSupportedLocales (),
718
731
externalClassLoader
719
732
);
720
733
PlatformResourceBundleLocator contributorResourceBundleLocator = new PlatformResourceBundleLocator (
721
734
ResourceBundleMessageInterpolator .CONTRIBUTOR_VALIDATION_MESSAGES ,
722
- getAllLocalesToInitialize (),
735
+ preloadResourceBundles (),
736
+ getAllSupportedLocales (),
723
737
externalClassLoader ,
724
738
true
725
739
);
@@ -733,9 +747,10 @@ private MessageInterpolator getDefaultMessageInterpolatorConfiguredWithClassLoad
733
747
return new ResourceBundleMessageInterpolator (
734
748
userResourceBundleLocator ,
735
749
contributorResourceBundleLocator ,
736
- getAllLocalesToInitialize (),
750
+ getAllSupportedLocales (),
737
751
defaultLocale ,
738
- ValidatorFactoryConfigurationHelper .determineLocaleResolver ( this , this .getProperties (), externalClassLoader )
752
+ ValidatorFactoryConfigurationHelper .determineLocaleResolver ( this , this .getProperties (), externalClassLoader ),
753
+ preloadResourceBundles ()
739
754
);
740
755
}
741
756
finally {
@@ -747,15 +762,22 @@ private MessageInterpolator getDefaultMessageInterpolatorConfiguredWithClassLoad
747
762
}
748
763
}
749
764
750
- protected final Locale getDefaultLocale () {
751
- return defaultLocale ;
752
- }
765
+ private Set <Locale > getAllSupportedLocales () {
766
+ if ( locales .isEmpty () ) {
767
+ return Collections .singleton ( defaultLocale );
768
+ }
769
+ if ( locales .contains ( defaultLocale ) ) {
770
+ return locales ;
771
+ }
753
772
754
- protected Set <Locale > getAllLocalesToInitialize () {
755
- // By default, we return an empty set meaning that we will dynamically initialize the locales.
756
- return Collections .emptySet ();
773
+ Set <Locale > allLocales = CollectionHelper .newHashSet ( locales .size () + 1 );
774
+ allLocales .addAll ( locales );
775
+ allLocales .add ( defaultLocale );
776
+ return allLocales ;
757
777
}
758
778
779
+ protected abstract boolean preloadResourceBundles ();
780
+
759
781
@ SuppressWarnings ("unchecked" )
760
782
protected T thisAsT () {
761
783
return (T ) this ;
0 commit comments