3

I am working with Spring (4.0.5) and Hibernate Validator (5.1.2.Final)

I already have working the error messages through a .properties file, it works too about i18n.

  • It in a standalone and testing (JUnit) environment, by the moment. I don't want include the Web environment yet to keep the things simple.

I have the following:

For the ValidationMessages_es_PE.properties file (in spanish)

person.salary.digits=Dato invalido '${validatedValue}', máximo tamaño no decimal permitido es {integer}, máximo tamaño decimal permitido es {fraction}

About the infrastructure

@Configuration
public class ValidatorConfiguration {

    @Bean
    public ResourceBundleMessageSource resourceBundleMessageSource(){
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
        resourceBundleMessageSource.setBasenames("com.manuel.jordan.validation.messages.ValidationMessages");
        return resourceBundleMessageSource;
    }

    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean(){

LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(resourceBundleMessageSource());

        MessageSourceResourceBundleLocator messageSourceResourceBundleLocator = new MessageSourceResourceBundleLocator(resourceBundleMessageSource()); 
        ResourceBundleMessageInterpolator resourceBundleMessageInterpolator = new ResourceBundleMessageInterpolator(messageSourceResourceBundleLocator);
        localValidatorFactoryBean.setMessageInterpolator(resourceBundleMessageInterpolator);

        return localValidatorFactoryBean;
    }


}

Observe I have setDefaultEncoding("UTF-8")

The test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ValidatorConfiguration.class)
public class InvalidPersonValidation03Test {

    private static final Logger logger = LoggerFactory.getLogger(InvalidPersonValidation03Test.class);

    @Autowired
    private LocalValidatorFactoryBean validator;

    public InvalidPersonValidation03Test(){
        LocaleContextHolder.setLocale(new Locale("es","PE"));
    }

    @Test
    public void testInvalidNullId(){
    ...

Part of the output is the following

- >>>testInvalidFractionSalary
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}

The characters ñ and á are not showing how it is expected

My IDE already has

Preferences -> Workspace -> Text file encoding with UTF-8

What extra missing configuration I need?

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

2 Answers2

1

You cannot use unicode characters like this in a properties file. See http://docs.oracle.com/javase/8/docs/api/java/util/PropertyResourceBundle.html

In that case, characters that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section 3.3 of The Java™ Language Specification...

á would be \u00E1 and ñ \u00F1

Hardy
  • 18,659
  • 3
  • 49
  • 65
  • Something is weird, I have other project working with .properties in spanish and portuguese and all works fine, the problem is now, the unique difference is that now I am working with Hibernate Validator.. – Manuel Jordan Aug 28 '14 at 13:42
  • Perhaps you know a tool that let me do quickly that conversion? creating perhaps a new file with the transformed data? Of course keeping the old one. Thanks – Manuel Jordan Aug 28 '14 at 13:44
  • 1
    nativetoascii, it is part of the JDK. See also http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle – Hardy Aug 28 '14 at 18:04
0

For the community

I have read this post Problem with Java properties utf8 encoding in Eclipse

Therefore consider in use this easy and very useful tool ResourceBundle Editor

It works for Eclipse and Spring Tool Suite

Problem solved

Community
  • 1
  • 1
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158