Search code examples
javabean-validationhibernate-validator

Can't resolve validationmessages.properties


When I run my project, localized validation messages located in src/main/resources (when the project is not packaged and run with an IDE-embedded runner) or WEB-INF/classes (when the project is packaged and deployed) are not resolved. I always get the default messages.

Yet, when I am running tests, the messages are resolved properly...

I use vanilla validation (Spring does not interfere).

validator = Validation.byDefaultProvider().configure().messageInterpolator(new LocaleAwareMessageInterpolator(locale)).buildValidatorFactory().getValidator();

LocaleAwareMessageInterpolator extends the standard ResourceBundleMessageInterpolator.

public class LocaleAwareMessageInterpolator extends ResourceBundleMessageInterpolator {
  private Locale defaultLocale = Locale.FRENCH;

  public LocaleAwareMessageInterpolator(Locale defaultLocale) {
    this.defaultLocale = defaultLocale;
  }

  @Override
  public String interpolate(final String messageTemplate, final Context context) {
    return interpolate(messageTemplate, context, defaultLocale);
  }

  @Override
  public String interpolate(final String messageTemplate, final Context context, final Locale locale) {
    try {
      return super.interpolate(messageTemplate, context, locale);
    } catch (ExceptionInInitializerError e) {...}
  }
}

Should I move them to another location ? Should I set some parameters somewhere to give out the location of the .properties ?


Solution

  • I solved the problem byt passing a PlatformResourceBundleLocator in the ResourceBundleMessageInterpolator constructor. I don't know why the system couln't get the ValidationMessages from WEB-INF/classes (I thought it was automatic), but this works.

    public LocaleAwareMessageInterpolator(Locale defaultLocale) {
       super(new PlatformResourceBundleLocator("ValidationMessages"));
       this.defaultLocale = defaultLocale;
    }
    

    Hibernate doc is pretty clear about it. I missed that.