PDA

View Full Version : Solution for UTF-8 encoding problem inside property files



YoannM
Mar 12th, 2008, 11:50 AM
Hi everybody,

I might be blind or something but I couldn't find anything on the Spring forum to deal with the UTF-8 problem inside the internationalization i18n property files.

I found a good solution, so I am posting it here for those who, like me, don't find anything about it.

The secret is here to use the "ReloadableResourceBundleMessageSource" instead of the "ResourceBundleMessageSource" and then forcing it to read the UTF-8 encoding.



<bean id="messageSource" class="org.springframework.context.support.ReloadableReso urceBundleMessageSource">
<property name="basename" value="WEB-INF/classes/messages" />
<property name="fileEncodings" value="UTF-8" />
<property name="defaultEncoding" value="UTF-8" />
</bean>


However, beware of the value of basename !! With the "ResourceBundleMessageSource" it would be something like value="messages" but with the "ReloadableResourceBundleMessageSource" it has to be the 'full path' from the WEB-INF directory.

I believe that is a good solution, but if somebody else knows something better, please let me know.

Cheers,
Yoann

iwein
Mar 12th, 2008, 01:47 PM
You're right to do it like this. You're not the first person to bump into this problem by the way.

http://osdir.com/ml/java.springframework.user/2004-08/msg00060.html



Note that Spring's ResourceBundleMessageSource, as used by the Countries
sample app, builds on the standard J2SE java.util.ResourceBundle and
java.util.Properties, which are not able to handle file encodings other than
ISO-8859-1.