When validating with Spring Boot, the default error message is English.
Meinotto Bienputi. Perapera.
I'm making an app for Japanese people, so I want to make it Japanese. Japanese localization itself is possible by creating a file called ValidationMessages.properties and defining error messages, but the properties file requires Unicode conversion, so it cannot be read live.
org.hibernate.validator.constraints.NotEmpty.message = \u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002
There is also a method to support with an IDE-specific plug-in, but this time I tried a method to accept UTF8 on the Spring Boot side.
By the way, ValidationMessages.properties written in UTF8 can be read with an editor, but when displayed on the screen, the Japanese part is garbled like this.
Below, I have received comments on the differences between Spring Boot versions, so I will capture them!
Spring Boot 2.1
In Spring Boot 2.1, it was possible to convert to UTF-8 without doing anything. The JDK version must be 11.
Spring Boot 1.5.3
I tried it with Spring Boot 1.5.3, but it seems that an error occurs when overriding public Validator getValidator (). (The error content is the same as the one displayed in the reference URL) If I set the bean of mvcValidator instead, it worked!
Thank you to everyone who taught me: bow_tone1:
Override getValidator of WebMvcConfigurerAdapter. It was just.
WebMvcConfig.java
WebMvcConfig.java
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/*・ ・ ・ Omitting unrelated settings ・ ・ ・*/
/**
*UTF message Validation message-Allow to set with 8
*/
@Override
public Validator getValidator() {
return validator();
}
@Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(messageSource());
return validator;
}
private MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//You can also change the name and directory of the property file
messageSource.setBasename("classpath:/ValidationMessages");
//UTF-Set to 8
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
ValidationMessages.properties
ValidationMessages.properties
org.hibernate.validator.constraints.NotEmpty.message=Please enter.
I can read it. You can read it.
Hyahoi: relaxed:
Spring Framework Documentation
end
Recommended Posts