Search code examples
javapropertiescharacter-encodingvaadinhibernate-validator

How to use a UTF8 properties file with Vaadin Bean Validation


I'm currently using Vaadin and an add-on named Vaadin Bean Validation for Java Bean Validation API 1.0 (JSR-303). The implementation of this API is hibernate-validator.

I have a custom property file with UTF8 as charset. But with this mechanism, special letters like "éèà" are always displayed wrong.

How can I fix this?


Solution

  • Properties files are as per specification read using ISO-8859-1.

    ... the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

    So, any character which is not covered by the ISO-8859-1 range needs to be escaped in the Unicode escape sequences \uXXXX. You can use the JDK-supplied native2ascii tool to convert them. You can find it in JDK's /bin folder.

    Here's an example assuming that foo_utf8.properties is the one which you saved using UTF-8 and that foo.properties is the one which you'd like to use in your application:

    native2ascii –encoding UTF-8 foo_utf8.properties foo.properties
    

    If you're using an IDE such as Eclipse, then you can just use the builtin properties file editor which should automatically be associated with .properties files. If you use this editor instead of the plain text editor, then it'll automatically escape the characters which are not covered by the ISO-8859-1 range.