Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read Franch from ResourceBundle

 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a character set issue. Make sure everyone is talking UTF-8.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to Make sure everyone is talking UTF-8?
Thanks!
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For now I use a ResourceBundle.Control to solve to problem.
Thanks.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the Control class:
Thanks.

 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I've spent half an hour testing with the default control, getting to the cause, writing the class, testing it and posting it...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic