读取资源绑定为UTF-8。 getString()方法似乎将编码更改为ISO-8859 [英] read resourcebundle as UTF-8. getString() Method seems to change encoding to ISO-8859
问题描述
我有这个荣幸的任务是将完整的工作空间,项目和文件的编码改为UTF-8编码。我们有几个Resourcebundles用于使用unicode编码特殊字符。我们也想通过切换到UTF-8来摆脱这个unicode的东西,所以我更改了Resourcebundles(.properties)文件的编码,并替换了Unicode字符。
我们还有德国资源分类和一些字符,如
Ä,Ö,Ü,ß。 ä,ö,ü以及特殊字符如或
在浏览器中未正确显示。例如:
Resourcebundleentry:
executeShellCommand.label = Shellkommandoausführen
浏览器中的结果:
资源绑定使用Java.util.ResourceBundle.getString(String key)读取方法:
public String getLocalizedString(ResourceBundle bundle,String key){
try {
System.out.println(getLocalizedString,key:+键+,resourcebundle:+ bundle.getString(key));
return bundle.getString(key);
} catch(MissingResourceException e){
return key;
}
}
如果我检查上面的Sysout的输出,我得到以下:
getLocalizedString,key:executeShellCommand.label,resourcebundle:Shellkommandoausführen
看起来, getString(key)方法可以从包中读取从标准资源编码(ISO-8859)中更改字符的编码。
我试图解决这个问题:
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;
}
}
这有助于恢复最特别的字符,但有仍然有很多他们没有正确显示:
我还检查了WebApp的内容类型配置以及获取资源束的每个请求,一切都是utf-8。有没有人有一个想法如何阻止getString() - 方法更改编码还是有更好的方法来解决这个问题? / p>
Java ResourceBundles假定ISO-8859。我想你需要使用Properties而不是ResourceBundle。
InputStream utf8in = getClass()。getClassLoader()。getResourceAsStream /path/to/utf8.properties);
读者阅读器=新的InputStreamReader(utf8in,UTF-8);
属性props = new Properties();
props.load(reader);
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);
这篇关于读取资源绑定为UTF-8。 getString()方法似乎将编码更改为ISO-8859的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!