问题
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?
回答1:
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);
回答2:
How's this for a hack!!!
public class HackClassLoader extends ClassLoader {
public HackClassLoader(ClassLoader parent) {
super(parent);
}
@Override
public InputStream getResourceAsStream(String name) {
InputStream utf8in = getParent().getResourceAsStream(name);
if (utf8in != null) {
try {
byte[] utf8Bytes = new byte[utf8in.available()];
utf8in.read(utf8Bytes, 0, utf8Bytes.length);
byte[] iso8859Bytes = new String(utf8Bytes, "UTF-8").getBytes("ISO-8859-1");
return new ByteArrayInputStream(iso8859Bytes);
} catch (IOException ex) {
throw new RuntimeException("Could not load " + name, e);
} finally {
utf8in.close();
}
}
return null;
}
}
ClassLoader hackLoader = new HackClassLoader(getClass().getClassLoader());
ResourceBundle bundle = ResourceBundle.getBundle("foo", Locale.UK, hackLoader);
来源:https://stackoverflow.com/questions/25053710/read-resourcebundle-as-utf-8-getstring-method-seems-to-change-encoding-to-iso