Mario B
Mario B

Reputation: 2355

Java ResourceBundles umlauts messed up

I have an issue with german "umlauts" in properties files. Now i totally get that i have to escape characters that are out of ISO-8859-1 range and i used native-to-ascii conversion in the past for this.

But why are umlauts like "ü" messed up too? They ARE part of ISO-8859-1 and if read the documentation right ResourceBundles in Java ARE read using ISO-8859-1. Also i use that encoding for my properties files. Can someone enlighten me why this is happening?

Example Code:

create a .properties containing the following message (make sure it is saved as ISO-8859-1)

testMessage=ä ü ö Ä Ü Ö this message is used to test ResourceBundle - loading!

create a testclass with the following testmethod:

@Test
public void testEncoding() {
    String mess = ResourceBundle.getBundle("i18n.messages").getString("testMessage");
    Assert.assertEquals("ä ü ö Ä Ü Ö this message is used to test ResourceBundle - loading!", mess);
}

Now in my case the result is:

org.junit.ComparisonFailure: 
Expected :ä ü ö Ä Ü Ö this message is used to test ResourceBundle - loading!
Actual   :� � � � � � this message is used to test ResourceBundle - loading!

*EDIT: It seems like this is a problem with my IntelliJ. If i execute the unit test and check target/i18n/messages.properties than the properties are already messed up there. I double checked the encoding of the source-file (in notepad) and also verified that my IDE interprets it as ISO-8859-1. Still no clue :(

Upvotes: 5

Views: 5578

Answers (2)

Raphael Roth
Raphael Roth

Reputation: 27383

Your test runs fine for me using Eclipse and ISO-8859-1 encoded properties file. But note that the two strings you provided differ:

ä ü ö Ä Ü Ö this message is used to test ResourceBundle - loading

vs

ä ü ö Ä Ü Ö this message is used to test resource Bundle-loading

So maybe the test is failing because of this, and the output is just messed up because the encoding og your console/logfile is not correct?

Upvotes: 1

GHajba
GHajba

Reputation: 3691

ResourceBundle loads contents in ISO-8859-1 format when converted to Strings. One solution (bad-hack?) I use in my project is the following:

I get the String value, get the bytes of it and create a new String with UTF-8 encoding. This solves my issues with umlauts.

new String(ResourceBundle.getBundle("your.folder").getString("testMessage").getBytes("ISO-8859-1"), "UTF-8");

Upvotes: 2

Related Questions