• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read Franch from ResourceBundle

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to Make sure everyone is talking UTF-8?
Thanks!
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For now I use a ResourceBundle.Control to solve to problem.
Thanks.
 
Paul Clapham
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the Control class:
Thanks.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic