getBytes()使用UTF-8不适用于大写德语Umlaut [英] getBytes() With UTF-8 Doesn't Work for Upper-Case German Umlauts
问题描述
对于开发,我使用 ResourceBundle
读取一个UTF-8编码的属性文件(我在该文件的Eclipse文件属性中设置)直接从我的资源-directory在IDE中(native2ascii用于生产的方式),例如:
For development I'm using ResourceBundle
to read a UTF-8 encoded properties-file (I set that in Eclipse' file properties on that file) directly from my resources-directory in the IDE (native2ascii is used on the way to production), e.g.:
menu.file.open.label=&Öffnen...
label.btn.add.name=&Hinzufügen
label.btn.remove.name=&Löschen
由于这会导致使用非ASCII字符时的字符编码问题,我认为我会很高兴:
Since that causes issues with the character encoding when using non-ASCII characters I thought I'd be happy with:
ResourceBundle resourceBundle = ResourceBundle.getBundle("messages", Locale.getDefault());
String value = resourceBundle.getString(key);
value = new String(value.getBytes(), "UTF-8");
好吧,它适用于小写德语变音符号,但不适用于大写字母,ß
也不起作用。这里是使用 getString(key)
读取的值,以及使用 new String(value.getBytes(),UTF-8)转换后的值
:
Well, it does work nicely for lower-case German umlauts, but not for the upper-case ones, the ß
also doesn't work. Here's the value read with getString(key)
and the value after the conversion with new String(value.getBytes(), "UTF-8")
:
&Löschen => &Löschen
&Hinzufügen => &Hinzufügen
&Ã?ber => &??ber
&SchlieÃ?en => &Schlie??en
&Ã?ffnen... => &??ffnen...
最后三个应为:
&Ã?ber => &Über
&SchlieÃ?en => &Schließen
&Ã?ffnen... => &Öffnen...
我想我离真相不太远,我在这里缺少什么?
I guess that I'm not too far away from the truth, but what am I missing here?
Google发现类似的东西,但仍未得到答复。
Google found something similar, but that remained unanswered.
推荐答案
今天我和我的一个同事说话,他和其他答案都有同样的道路。所以我试图实现Jon Skeet所提到的,意味着创建与生产中相同的文件。因为在每次更改资源后重建项目是没有问题的,我没有做任何解决这个问题(我想这将是一些新的)让我排除它(即使它可能只是为了个人参考))。总之,这使用Eclipse的项目构建器。
Today I was talking to one of my colleagues and he was pretty much on the same path as the other answers have mentioned. So I tried to achieve what Jon Skeet had mentioned, meaning creating the same file as in production. Since rebuilding the project after each change of a resource is out of question and I hadn't done any of what solved this (and I guess this will be new to some) let me line it out (even if it may be just for personal reference ;) ). In short this uses Eclipse' project builders.
-
创建一个Ant风格的build.xml
Create an Ant-style build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<property name="dir.resources" value="src/main/resources" />
<property name="dir.target" value="bin/main" />
<target name="native-to-ascii">
<delete dir="${dir.target}" includes="**/*.properties" />
<native2ascii src="${dir.resources}" dest="${dir.target}" includes="**/*.properties" />
</target>
</project>
目的是删除目标目录中的属性文件,并使用 native2ascii
来重新创建它们。因为 native2ascii
不会覆盖现有文件,所以必须删除。
Its intention is to delete the properties-files in the target directory and use native2ascii
to recreate them. The delete is necessary as native2ascii
won't overwrite existing files.
如果编辑属性文件并保存,它应该在输出文件夹中自动转换为ASCII。您可以尝试输入ü
,其最终应为 \\\ü
。
That should have been it. If you edit a properties-file and save it, it should automatically be converted to ASCII in the output folder. You can try with entering ü
, which should end up as \u00fc
.
请注意,如果你有很多属性文件,这可能需要一些时间。只是不要保存每次按键。 :)
Note that if you have a lot of properties-files, this may take some time. Just don't save after every keypress. :)
这篇关于getBytes()使用UTF-8不适用于大写德语Umlaut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!