I have the honorable assignment to change the encoding of our complete workspace, projects and files to the UTF-8 encoding. We have several Resourcebundles which used to code special chars with unicode. We also wanted to get rid of that unicode stuff by switching to UTF-8 so I changed the encoding of the Resourcebundles (.properties) files too and replaced the Unicode characters.
We also have german resourcebundles and some chars like
Ä, Ö, Ü, ß. ä, ö, ü and also special characters like „ or “
are not shown properly in the browser. Example:
Resourcebundleentry:
executeShellCommand.label = Shellkommando ausführen
Result in browser:
The resourcebundles are read with the Java.util.ResourceBundle.getString(String key) Method:
public String getLocalizedString(ResourceBundle bundle, String key) {
try {
System.out.println("getLocalizedString, key: " + key + ", resourcebundle: " + bundle.getString(key));
return bundle.getString(key);
} catch (MissingResourceException e) {
return key;
}
}
If i check the output of the above Sysout i get following:
getLocalizedString, key: executeShellCommand.label, resourcebundle: Shellkommando ausführen
It seems that the getString(key) method changes the encoding of the chars while reading them from the bundles to the standard resourcbundleencoding(ISO-8859).
I tried to counter this issue:
public String getLocalizedString(ResourceBundle bundle, String key) {
try {
System.out.println("getLocalizedString, key: " + key + ", resourcebundle: " + new String (bundle.getString(key).getBytes(), "UTF-8"));
return new String (bundle.getString(key).getBytes(), "UTF-8");
} catch (MissingResourceException e) {
return key;
} catch (UnsupportedEncodingException e) {
return key;
}
}
This helped to recover the most special characters but there are still a plenty of them which are not shown properly:
I also checked the content-type configuration of the WebApp and of every single request which gets the resource bundles everything is utf-8.
Does anyone have an idea how to prevent the getString()-Method from changing the encoding or is there a better way to solve this issue?
Java ResourceBundles assume ISO-8859. I think you'll need to use Properties instead of ResourceBundle.
InputStream utf8in = getClass().getClassLoader().getResourceAsStream("/path/to/utf8.properties");
Reader reader = new InputStreamReader(utf8in, "UTF-8");
Properties props = new Properties();
props.load(reader);