i18n - Java Internationalization - Entity Validation Message

Asked

Viewed 184 times

0

I have an app Spring Boot where I’m developing a system Web using Thymeleaf. The system has the translation functionality according to the language of the user, however, when using in the entity, through the annotations validations, for example, the @Size with min and max is not passed to the entity. I would like to know what the solution to this problem. I stress that, in the other, the translation works correctly.

Below are more details: Entity:

@Column(name = "corporate_name", length = 255, nullable = false)
@Size(min = 3, max = 255, message = "{entities.messages.size.corporate-name}")
protected String corporateName;

@Column(name = "fantasy_name", nullable = false)
@Size(min = 3, max = 255, message = "{entities.messages.size.fantasy-name}")
protected String fantasyName;

The message in the translation file:

entities.messages.size.corporate-name=The corporate name must be between {0} and {1} characters.

entities.messages.size.Fantasy-name=The Fantasy name must be between {0} and {1} characters.

As far as I had researched and understood on this subject, the minimum and maximum values of the annotation Size, should be automatically replaced by the respective values {0} and {1} of the message, however, when performing the tests, is displayed 0 and 1 as can be seen below:

inserir a descrição da imagem aqui

It was expected to be displayed, for example, the message:

The corporate name must be between 3 and 255 characters.

  • Will your message file you do not have to structure as follows: Size.MinhaEntidade.meuAtributo=Exam title must contain between {2} and {1} characters.? I looked at that reference: https://dzone.com/articles/exception-handling-and-i18n-on-spring-boots-apis-p

  • 2

    @Fabio Souza, I answered with the same content of the other negative answer, but with links of documentation. After I saw that you commented that it did not work properly! What makes me wonder if it is not some configuration error of your project. I have an API that currently uses {min} and {max} with custom messages and works 100%. Could you kindly describe the class Imports and the project structure where @Configuration to load the Undles, and Validator you are using?

  • 1

    @nullptr, I found the definition https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/? v=7.0#_examples you are correct there is even this behavior on the part of the annotation @Size. The mistake is mine.

  • Included a project with a working example on my github, I also added unit tests to validate the feedback message.

  • I still suspect that there is some configuration problem in the @Fabio project

2 answers

0

I found the ultimate solution to my problem.

Turns out, when I created the @Bean MessageSource should have left the setUseCodeAsDefaultMessage as false (or had not included the line as the default is false) and had left as true.

To correct, it was enough to change messageSource.setUseCodeAsDefaultMessage(true); for messageSource.setUseCodeAsDefaultMessage(false); or remove the line. Follows:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/messages/i18n");
    messageSource.setUseCodeAsDefaultMessage(false);
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

@Bean
public LocalValidatorFactoryBean validator() {
    final LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
    bean.setValidationMessageSource(this.messageSource());
    return bean;
}

Another point, which I leave as an observation, is that the parameters as {0} and {1} for example, did not work properly, having to change to {min} and {max} as can be seen below: entities.messages.size.corporate-name=The corporate name must be between {min} and {max} characters. I thank everyone who helped me.

-3

Good morning! Try changing to:

entities.messages.size.corporate-name=The corporate name must be between {min} and {max} characteres. 
entities.messages.size.fantasy-name=The fantasy name must be between {min} and {min} characteres.
  • I already tried, it removes the keys and leaves 'min' and 'max', being like this: The corporate name must be between min and max characters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.