UTF-8 encoding and Spring message sources

Take a look at the update from October 2011′s perspective here.

I was working on a pretty much straight-forward web application. As usual, i used Spring’s org.springframework.context.MessageSourcesupport, to be more precise, i used standard implementation – ResourceBundleMessageSource. Simple configuration, as following:

<bean id="messageSource"
                 class="org.springframework.context.support.ResourceBundleMessageSource">
          <property name="basename" value="messages"/>
</bean>

My messages properties file was standard as well, country names, so i had something like this:

country.name.unitedkindom=UK
country.name.ireland=Ireland
country.name.belgium=België
country.name.iceland=Ísland
country.name.israel=Israel...

As you can guess, the problems developed with specific characters in country names(ë, Í…). When i run the application, i got ‘???’ characters instead of specific UTF-8 characters for countries (so ‘België’ become ‘Belgi???’).

After some research, and digging in Spring source code, i located the problem, and the solution!

The problem was that ResourceBundleMessageSource uses the standard java.util.ResourceBundle and
java.util.Properties, which only support ISO-8859-1 encoding – so no UTF-8 encoding, no support for any special French, Spanish or Asian characters.

The solutions is very simple, and it comes with Spring as well. Use ReloadableResourceBundleMessageSource instead! Here is the correct configuration:

<bean id="messageSource";
                     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
         <property name="basename" value="classpath:messages"/>
         <property name="defaultEncoding" value="UTF-8"/>
</bean>

As you can see, we are now able ot set default encoding explicitelly, so the countries names in my example worked like charm. The only other change was that the basename is now specified as Resource, so i was able to use convenient classpath: identifier to specify my messages.properties wherever i need it in the source code

This entry was posted in Aleksa's Blog and tagged , , , , . Bookmark the permalink.

8 Responses to UTF-8 encoding and Spring message sources

  1. yobi says:

    thank you

  2. Neeraj says:

    Well there are couple of problems:
    1. You need to open every .properties file in notepad (or some other editor which can save in utf-8 format) and save it back in utf-8 format.

    2. If you have any file left in standard format then it will not display certain characters (say espanish) correctly.

    So the point is – save your properties file in UTF-8 format

    Thanks

  3. wil says:

    I used org.springframework.context.support.ResourceBundleMessageSource and could display Chinese characters on the web.
    The messages properties file is such as
    prod.buy=??
    prod.buy.account=??\:….

    Thanks

  4. Saurabh says:

    Great. I was struggling with the same problem and its solved. However, i noticed that with ResourceBundleMessageSource you don’t need to use classpath, but with ReloadableResourceBundleMessageSource using classpath is mendatory.

    Thanks.

  5. Vivin says:

    Thanks a bunch! This solved my problem perfectly!

  6. saravanan.r says:

    Thanks, i solved the problem.

  7. Kris says:

    Thank you, you are life-saver :)

  8. juraj says:

    Nice, thanks, this blog helps me a lot, thanks again:)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>