为什么资源包中的瑞典语文本显示为乱码?
可能的重复:
如何在 ResourceBundle 的资源属性中使用 UTF-8< /a>
我想允许我的 Java Swing 应用程序国际化。我使用捆绑文件将所有标签保存在其中。
作为测试,我尝试为 JButton
设置瑞典语标题。所以在捆绑文件中我写道:
nextStepButton=nästa
在Java代码中我写道:
nextStepButton.setText(bundle.getString("nextStepButton"));
但是按钮的标题字符在运行时出现错误:
我使用的是 Tahoma 字体,它支持 Unicode。 当我通过代码手动设置按钮标题时,它看起来很好:
nextStepButton.setText("nästa");
知道为什么它在捆绑文件中失败吗?
--------------------- -----------------------> 编辑:对标题进行编码:
我尝试使用代码对来自捆绑文件的文本进行编码:
nextStepButton.setText(new String(bundle.getString("nextStepButton").getBytes("UTF-8")));
结果仍然是:
Possible Duplicate:
How to use UTF-8 in resource properties with ResourceBundle
I want to allow internationalization to my Java Swing application. I use a bundle file to keep all labels inside it.
As a test I tried to set a Swedish title to a JButton
. So in the bundle file I wrote:
nextStepButton=nästa
And in the Java code I wrote:
nextStepButton.setText(bundle.getString("nextStepButton"));
But the title characters of the button appear wrong at runtime:
I am using the Tahoma font, which supports Unicode.
When I set the button title manually through code it appears fine:
nextStepButton.setText("nästa");
Any idea why it fails in bundle file ?
--------------------------------------------> Edit: Encoding the title:
I have tried encoding the text coming from the bundle file using the code:
nextStepButton.setText(new String(bundle.getString("nextStepButton").getBytes("UTF-8")));
And still the result is:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 javadoc,读取属性文件使用 ISO-8859-1。
除了使用native2ascii工具将UTF-8属性文件转换为ISO-8859-1属性文件之外,您还可以使用自定义的
ResourceBundle.Control
以便您可以控制属性文件的加载并在那里使用 UTF-8。这是一个启动示例:按如下方式使用它:
这样您就不需要使用 native2ascii 工具,并且最终会得到更好的可维护属性文件。
另请参阅:
As per the javadoc, properties files are read using ISO-8859-1.
Apart from using the native2ascii tool to convert UTF-8 properties files to ISO-8859-1 properties files, you can also use a custom
ResourceBundle.Control
so that you can control the loading of properties files and use UTF-8 there. Here's a kickoff example:Use it as follows:
This way you don't need to hassle with native2ascii tool and you end up with better maintainable properties files.
See also:
请查看 Java 国际化常见问题解答。如果您在
.properties
文件中放入了非 ASCII 字符,则必须使用 native2ascii 工具。然后一切都应该正常。Take a look at Java Internationalization FAQ. If you've put non ASCII characters in your
.properties
file, you must convert it using the native2ascii tool. Then everything should work.问题在于资源包属性文件以 UTF-8 编码,但您的应用程序使用 Latin-1 加载它。
如果您采用“LATIN SMALL A WITH DIAERESIS”(Latin-1 中的 E4 或作为 Unicode 代码点的 0000E4)并将其表示为 UTF-8,则会得到 C3 A4。如果您将这些视为 Latin-1 字节,您将得到“带有波浪号的拉丁文大写字母 A”和方形“货币符号”字符......这就是这些字符在按钮屏幕截图中显示的方式!
(顺便说一句,这是由于使用错误的字符编码而导致的损坏的新词...... mojibake< /a>. 在对话中使用它来迷惑你的朋友。)
The problem is that the resource bundle properties file is encoded in UTF-8 but your application is loading it using Latin-1.
If you take "LATIN SMALL A WITH DIAERESIS" (E4 in Latin-1 or 0000E4 as a Unicode codepoint) and represent it as UTF-8, you get C3 A4. If you then treat those as Latin-1 bytes you get "LATIN CAPITAL LETTER A WITH TILDE" and the square "CURRENCY SIGN" character ... which is how the characters are showing in your screenshot of the button!!
(Incidentally, here's a neologism for the mangling you get as a result of using the wrong character encoding ... mojibake. Baffle your friends by using it in conversation.)