为什么资源包中的瑞典语文本显示为乱码?

发布于 2024-10-07 08:13:27 字数 1174 浏览 4 评论 0原文

可能的重复:
如何在 ResourceBundle 的资源属性中使用 UTF-8< /a>

我想允许我的 Java Swing 应用程序国际化。我使用捆绑文件将所有标签保存在其中。

作为测试,我尝试为 JButton 设置瑞典语标题。所以在捆绑文件中我写道:

nextStepButton=nästa

在Java代码中我写道:

nextStepButton.setText(bundle.getString("nextStepButton"));

但是按钮的标题字符在运行时出现错误:
alt text

我使用的是 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:
alt text

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:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-10-14 08:13:28

根据 javadoc,读取属性文件使用 ISO-8859-1。

.. 输入/输出流采用 ISO 8859-1 字符编码进行编码。不能直接用这种编码表示的字符可以使用 Unicode 转义来编写;转义序列中只允许使用单个“u”字符。 native2ascii 工具可用于将属性文件与其他字符编码相互转换。

除了使用native2ascii工具将UTF-8属性文件转换为ISO-8859-1属性文件之外,您还可以使用自定义的ResourceBundle.Control 以便您可以控制属性文件的加载并在那里使用 UTF-8。这是一个启动示例:

public class UTF8Control extends Control {
    public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException
    {
        // The below is a copy of the default implementation.
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                // Only this line is changed to make it to read properties files as UTF-8.
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }
}

按如下方式使用它:

ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", new UTF8Control());

这样您就不需要使用 native2ascii 工具,并且最终会得到更好的可维护属性文件。

另请参阅:

As per the javadoc, properties files are read using ISO-8859-1.

.. the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

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:

public class UTF8Control extends Control {
    public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException
    {
        // The below is a copy of the default implementation.
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                // Only this line is changed to make it to read properties files as UTF-8.
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }
}

Use it as follows:

ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", new UTF8Control());

This way you don't need to hassle with native2ascii tool and you end up with better maintainable properties files.

See also:

影子的影子 2024-10-14 08:13:28

请查看 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.

画尸师 2024-10-14 08:13:28

问题在于资源包属性文件以 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文