ResourceBundle - French characters

I have a Swing application that I want to internationalize. I.E. I want to have an English resource bundle, and a French resource bundle.

I created the two resources files and I've placed them in the classpath, I then load the files like this ( for the French version ):

ResourceBundle.getBundle( "DisplayStrings", Locale.FRENCH, this.getClass().getClassLoader() );

And the resource load properly, but when I request a french string that containts French characters, the String doesn't bring over the french accent characters properly.

Is this an encoding problem? When viewed in a Text editor the French accents work fine, but when I run the application the French characters are coming across as funny garbage characters.

Does anyone have a suggestion?

Message was edited by:

bryano

[836 byte] By [bryanoa] at [2007-11-27 7:50:40]
# 1
Yes, it's an encoding problem. You need to use the "native2ascii" program (comes with Java) to convert the French file. It'll generate Unicode escapes where needed for non-ASCII charactersMessage was edited by: bsampieri
bsampieria at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 2

I've executed that native2ascii program on my properties file:

E.G. - It converted this line:

Des raccordements aux syst鑝es principaux n'ont pas pu 阾re 閠ablis.

to

Des raccordements aux syst\u00c3\u00a8mes principaux n'ont pas pu \u00c3\u00aatre \u00c3\u00a9tablis.

But when I execute the program, the characters are still not coming through properly. I end up with this string:

Des raccordements aux syst猫mes principaux n'ont pas pu 锚tre tablis.

bryanoa at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 3
Then the problem is in whatever you mean by "coming through" or "coming across".What I see there is what typically happens when you take UTF-8 encoded data and read it in with a single-byte encoding like ISO-8859-1 or Windows-1252.
DrClapa at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 4

When I extract the String from the resource bundle:

String string = resources.getString( propertykey );

the string value has the garbage characters.

I've put a breakpoint in my app to view the string right after I extract it from the resource bundle and that is where the characters are not shown properly.

And I've got this property set on the VM:

-Dfile.encoding=UTF-8

Message was edited by:

bryano

bryanoa at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 5

...and now that I look at it, that's what happened when you ran native2ascii. It's the thing that took UTF-8 data and interpreted it with the wrong encoding. You should have only one Unicode escape character in the converted version of "阾re" but instead you have two: "\u00c3\u00aatre". You need to tell native2ascii that your input is UTF-8.

DrClapa at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 6
Thank you. That solved it.A quick follow question.Is running the file through that native2ascii program the only way around my issue? That seems like a bit of a pain to run that command line util every time I make a change to the properties file.Thanks.
bryanoa at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 7

The real pain would be having to write the escape characters on your own instead of having a tool do it for you ;)

But in order to make it easier to build you application, you could use an ant buildfile to automatically take care of the conversion when a modification has been made to the properties file.

Dalzhima at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 8
Java 5 allows you to write properties in XML format, and of course that respects the XML rules for declaring the encoding (instead of forcing one on you). But I don't know if ResourceBundle can work with that format. Might be worth checking out.
DrClapa at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...
# 9

I'd definitely make Ant do the task for you. Keep a raw version of the file to edit and let Ant convert it during build.

The only other way is if you write the files in UTF-8 would be to write your own resource bundle lookup and reader class to read the data in UTF-8. Probably more work then it's worth.

bsampieria at 2007-7-12 19:31:43 > top of Java-index,Java Essentials,Java Programming...