tags:

views:

436

answers:

3

Hi. Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using native2ascii and it solved my problem. Is there no other way of converting the file through setting the encoding to ascii in the configuration files instead of manually converting it by executing native2ascii in command prompt

+1  A: 

AfAIK in property files and resource bundles you have to use ASCII. Inside Spring XML configuration files, Unicode should work fine. If you prefer you can edit property files in Unicode and run native2ascii automatically as part of your build process (in Ant, Maven, etc).

Dan
What do you mean by inside spring xml configurations unicode works fine?By the way I already have this line too in my Spring config
cedric
<filter> <filter-name>encoding-filter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>encodingScheme</param-name> <param-value>UTF-8</param-value> </init-param>     <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
cedric
You didn't say what the messages are for exactly. If they are associated with some sort of Properties object that you're creating as a Spring bean, you can put UTF-8 text as String property values in your beans.xml (or whatever) file as long as you mark the file with the proper ?xml declaration. But for .properties files, I think you have to use native2ascii.
Dan
+1  A: 

hey, i googled for the same issue and found something written in german that was a help for me: http://www.stefanglase.de/2009/10/13/spring-messagesource-mit-utf-8-encoding/

Dan
A: 

As per the java.util.Properties API document:

The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except 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.

(note that ISO 8859-1 is not the same as ASCII as many are incorrectly talking about here).

So, to fix the particular problem without the need for native2ascii, you should use Properties#load(Reader) with an InputStreamReader(input, charset) instead.

Properties properties = new Properties();
properties.load(new InputStreamReader(classLoader.getResourceAsStream("file.properties"), "UTF-8"));

Note that this method was introduced in Java 1.6 over 4 years ago. Ensure that you're using it as well.

I don't do Spring, so I can't go in detail about how to get Spring to work that way, but it would be obvious that you need to override/replace the Spring's resource bundle manager, if any.

BalusC