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?

2
  • Try to set the -Dfile.encoding=utf-8 to the server. Commented Aug 27, 2014 at 15:29
  • Thanks, where I should apply that? I tried on JUnit->Run Configurations -> Arguments in VM arguments or Program arguments and same bad results. Commented Aug 27, 2014 at 15:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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..
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
nativetoascii, it is part of the JDK. See also stackoverflow.com/questions/4659929/…
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.