2

How do you change the resource bundle of validation messages? The default one is reading my ValidationMessages.properties as ISO-8859-1 (I guess) but it is UTF-8. I tried this:

@Configuration
@EnableWebMvc
@ComponentScan
public class ApplicationConfiguration extends WebMvcConfigurerAdapter{

    @Bean( name = "messageSource" )
    public ReloadableResourceBundleMessageSource validationMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("ValidationMessages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
        validatorFactoryBean.setValidationMessageSource(validationMessageSource());
        return validatorFactoryBean;
    }

    @Override
    public Validator getValidator() {
        return validator();
    }

Whatever I put here this is not used by Hibernate to get messages. Any tips? I'm using Hibernate 5 and Spring 4.

Jérôme Herry
  • 187
  • 2
  • 7
  • Are you sure it's not using "UTF-8"? Can you call `messageSource.getDefaultEncoding()` at runtime and double check? – ltalhouarne Oct 30 '15 at 14:20

3 Answers3

3

.properties file are per specification read in ISO-8859-1 encoding (aka Latin-1. (See https://en.wikipedia.org/wiki/.properties and How to use UTF-8 in resource properties with ResourceBundle). So you need to specify the unicode characters instead of saving the file as UTF-8.

To use the LocalValidatorFactoryBean with Hibernate you need to wire it up to JPA else it will use the default one.

Add the following property to your JPA properties of the LocalContainerEntityManagerFactoryBean.

<entry key="java.persistence.validation.factory" value-ref="validator" />

Adding that will make it use the Spring Configured instance.

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
0
    @Bean
    public MessageSource validationMessageResource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("ValidationMessages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public Validator validator() {
        return Validation.byDefaultProvider()
                .configure()
                .messageInterpolator(
                        new ResourceBundleMessageInterpolator(
                                new MessageSourceResourceBundleLocator(validationMessageResource())
                        )
                )
                .buildValidatorFactory()
                .getValidator()
                ;
    }
Declow0
  • 11
  • 1
0

One option to make UTF-8 to work is to use java class instead of properties file.

Just add ValidationsMessages.java, ValidationMessages_fi.java etc. to your src folder root.

public class ValidationMessages extends ListResourceBundle {

@Override
protected Object[][] getContents() {
    return new Object[][] { 
        { "user.name", "Name is not valid" } 
    };
}

}

Heikki
  • 1