Problem in retrieving Chinese strings stored in Resource Bundle fiels

Hello,

I have installed Chinese fonts on my m/c.

I have also added Chinese language in Regional and Language settings. I am able to print Chinese charecters thro' Sys.out.pr.

I am using Win XP.

I have reource bundle file containg :

greetings = ?湄楮札?潮

farewell = ?湄楮札?潮

inquiry = How are you?

Named MessageBundle_zh_CN.properties

Source code :

ResourceBundle messages;

Locale currentLocale = new Locale("zh", "CN");

messages = ResourceBundle.getBundle("MessagesBundle",

currentLocale);

try {

String greetings = new String(messages.getString("greetings").getBytes("GB2312"));

System.out.println("gr :: " +greetings);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

My problem is :

When I execute this program 1st of all it's not finding the required "MessageBundle_zh_CN.properties". It's taking values from base "MessageBundle.properties". And showing "?" for Chinese values.

[1031 byte] By [kalyani_bhadekara] at [2007-11-26 21:51:19]
# 1
Note that properties files must follow defined encoding and character escaping rules described in the [url= http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html#encoding]Properties API docs[/url].You might use the native2ascii tool to convert your file.
TimTheEnchantora at 2007-7-10 3:44:33 > top of Java-index,Java Essentials,Java Programming...
# 2

Do I understand you correct if the .properties file can't contain chinese characters but unicode escape characters?

It seems so "unneccessary" to do the convert to unicode escapes.

In that case, is it the resourceBundles that do not handle native characters? Or is it java in general?

Marita at 2007-7-10 3:44:33 > top of Java-index,Java Essentials,Java Programming...
# 3

from

String greetings = new String(messages.getString ("greetings").getBytes("GB2312"));

to

String greetings = new String(messages.getString ("greetings").getBytes("ISO8859-1"), "GB2312");

rym82a at 2007-7-10 3:44:34 > top of Java-index,Java Essentials,Java Programming...
# 4

> Do I understand you correct if the .properties file

> can't contain chinese characters but unicode escape

> characters?

Correct

> It seems so "unneccessary" to do the convert to

> unicode escapes.

It is, as soon as you are using properties files and load them through ResourceBundle or Properties class.

> In that case, is it the resourceBundles that do not

> handle native characters? Or is it java in general?

It is linked to the way the Properties class stores and loads data (see link from reply#1.)

It is not a limitation of Java, with which you can load text files in many encodings (see [url=http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html]here[/url]).

TimTheEnchantora at 2007-7-10 3:44:34 > top of Java-index,Java Essentials,Java Programming...
# 5

Sorry about interfering the discussion, but you see, I have had nearly the same problems for days.....

My .properties file is in traditional chinese. I saved the .properties file in UTF format (the chinese signes are still the original, not unicode escapes)

String greetings = new String(messages.getString ("greetings").getBytes("UTF8")"Big5");

But my GUI shows some chinese signs, but they are not at all the same as read from the file. I read 4 signs from the file, and 12 signs are displayed. 2 of them as "empty squares". Do you know why?

Marita at 2007-7-10 3:44:34 > top of Java-index,Java Essentials,Java Programming...
# 6

> My .properties file is in traditional chinese. I saved the .properties

> file in UTF format.

As said (here and in your own [url=http://forum.java.sun.com/thread.jspa?threadID=5147997&messageID=9552258#9552258]thread[/url]), you have to convert it using native2ascii. A properties file loaded with the Properties or ResourceBundle class must comply with the docs/specs:

"The [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)]load[/url] and [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#store(java.io.OutputStream, java.lang.String)]store[/url] methods load and store properties in a simple line-oriented format specified below. This format uses the ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using [url=http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.3]Unicode escapes[/url] ; only a single 'u' character is allowed in an escape sequence. The [url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/native2ascii.html]native2ascii[/url] tool can be used to convert property files to and from other character encodings."

TimTheEnchantora at 2007-7-10 3:44:34 > top of Java-index,Java Essentials,Java Programming...
# 7

I am converting the .properties file with Chinese chars. in UTF-8 to output file containing "\u585e" e.g. Then I am pasting the contents of this file to original properties file and now saving it with "ANSI" encoding.

Now everything is wirking 5n..

But How can I restore the properties file written with Chinese charecters. I mean I need this file for my translators. I used the reverse option of native2ascii --> native2ascii -encoding UTF-8 MessagesBundle_zh_CN.properties inputfile.properties

But it's not working.

Cheers

Kalyani....

kalyani_bhadekara at 2007-7-10 3:44:34 > top of Java-index,Java Essentials,Java Programming...
# 8
> used the reverse option of native2ascii --> native2ascii -encoding > UTF-8 MessagesBundle_zh_CN.properties inputfile.propertiesIs it a typo, or you simply forgot to actually use the -reverse option ?
TimTheEnchantora at 2007-7-10 3:44:34 > top of Java-index,Java Essentials,Java Programming...
# 9
It was a typo.... i got my mistake, when I executed the command the defult locale set by me was English(US) in the Regional and Language settings...Thanx for all the replies..
kalyani_bhadekara at 2007-7-10 3:44:35 > top of Java-index,Java Essentials,Java Programming...
# 10

One more thing, I saved my Chinese reource bundle file with UTF-8 encoding, and after using native2ascii the converted file was having extra unicode charecter appended at the start of the 1st key in the file

as "\ufeffgreetings = \u897f\u73ed\u7259\u6587"

When I changed the encoding of the input file to unicode, everything worked fine.

I want to know why this happened?

How to use native2ascii using eclipse?

Is there any plugin avilable for it?

Message was edited by:

kalyani_bhadekar

kalyani_bhadekara at 2007-7-10 3:44:35 > top of Java-index,Java Essentials,Java Programming...