aspose file tools*
The moose likes Java in General and the fly likes How to read Franch from ResourceBundle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read Franch from ResourceBundle" Watch "How to read Franch from ResourceBundle" New topic
Author

How to read Franch from ResourceBundle

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
I am working on a English language PC. I have a message_fr_FR.properties file and some franch entries:

user-is-disabled=utilisateur est désactivé

When I read it from Java code I get “utilisateur est désactivé”

The code looks like this:



Why?
Thanks

BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Looks like a character set issue. Make sure everyone is talking UTF-8.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Bear.

How to Make sure everyone is talking UTF-8?

I know the property file is encoded UTF-8.

Thanks.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
How to Make sure everyone is talking UTF-8?
Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Looks to me like something is encoded in UTF-8 but actually read in some other encoding.

You said your properties file is encoded in UTF-8. However properties files are assumed to be encoded in ISO-8859-1 (as the API documentation states).

Which confirms my impression.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Paul Clapham wrote:Looks to me like something is encoded in UTF-8 but actually read in some other encoding.


Yes. The question is how to actually read in UTF-8?

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, that isn't the question. Or that's only one of the possible questions.

The answer to one possible question is to follow the rules and use ISO-8859-1 as the encoding for your properties file. Note that it's possible to do that for French text without having to mess about with Unicode escapes.

The answer to that question might be to not use ResourceBundle, which I expect follows the existing rules for properties files, and to use various Java 6 features which allow you to specify the charset while reading a properties file in your own substitute for ResourceBundle. Or it might be to use an XML-formatted properties file, for which the charset does default to UTF-8, if a ResourceBundle could use that.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
For now I use a ResourceBundle.Control to solve to problem.
Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Aha! A real solution! Would you care to post some details about it? I'm sure you aren't the only one burned by Sun's short-sighted decision to force the use of ISO-8859-1.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
This is the Control class:
Thanks.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

It's definitely an encoding issue. I just wrote a little test program; if the properties file is encoded using ANSI I get the expected output, but if I save it using UTF-8 I get the mess you got. I also get the same results if I use java.util.Properties with a FileReader with the system encoding. Only if I switch that do I get the actual content.

So that's what we need to do for ResourceBundle as well. ResourceBundle itself doesn't have any methods / constructors to help us with, so we have to go to ResourceBundle.Control. I've checked the source (available in src.zip inside the JDK folder), and it uses a PropertyResourceBundle combined with an InputStream. And guess what - this uses Properties.load with an InputStream, thereby getting the same problem as I got with a direct Properties object.

The solution is obvious - create a new ResourceBundle.Control subclass, and override newBundle to use a Reader instead of an InputStream. The following is my quick (but tested and working) attempt:
I'm actually disappointed that Sun/Oracle haven't provided such a class themselves.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Wow, I've spent half an hour testing with the default control, getting to the cause, writing the class, testing it and posting it...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read Franch from ResourceBundle