Java 为什么来自资源包的瑞典语文本显示为胡言乱语?

Java 为什么来自资源包的瑞典语文本显示为胡言乱语?,java,swing,internationalization,resourcebundle,mojibake,Java,Swing,Internationalization,Resourcebundle,Mojibake,可能重复: 我想允许我的JavaSwing应用程序国际化。我使用一个bundle文件将所有标签保存在其中 作为测试,我试着将一个瑞典语标题设置为JButton。所以我在bundle文件中写道: nextStepButton=nästa nextStepButton.setText(bundle.getString("nextStepButton")); 在我编写的Java代码中: nextStepButton=nästa nextStepButton.setText(bundle.get

可能重复:

我想允许我的JavaSwing应用程序国际化。我使用一个bundle文件将所有标签保存在其中

作为测试,我试着将一个瑞典语标题设置为
JButton
。所以我在bundle文件中写道:

nextStepButton=nästa
nextStepButton.setText(bundle.getString("nextStepButton"));
在我编写的Java代码中:

nextStepButton=nästa
nextStepButton.setText(bundle.getString("nextStepButton"));
但是按钮的标题字符在运行时出现错误:

我使用的是支持Unicode的Tahoma字体。 当我通过代码手动设置按钮标题时,它看起来很好:

nextStepButton.setText("nästa");
知道它在捆绑文件中失败的原因吗?

-------------------------------------------->编辑:对标题进行编码:
我已尝试使用以下代码对来自捆绑文件的文本进行编码:

nextStepButton.setText(new String(bundle.getString("nextStepButton").getBytes("UTF-8")));
结果仍然是:

看一看。如果在
.properties
文件中放入了非ASCII字符,则必须使用该工具进行转换。然后一切都会正常工作。

问题是资源包属性文件是用UTF-8编码的,但应用程序正在使用拉丁语-1加载它

如果您使用“带分音符的拉丁小A”(拉丁语中的E4-1或0000E4作为Unicode码点)并将其表示为UTF-8,则得到C3 A4。如果你将这些视为拉丁1字节,你会得到“带波浪号的拉丁大写字母A”和方形“货币符号”字符。。。这就是你的按钮截图中人物的显示方式

(顺便提一下,这里有一个新词,用来形容由于使用错误的字符编码而造成的混乱……在对话中使用它会让你的朋友感到困惑。)

根据,属性文件是使用ISO-8859-1读取的

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

除了使用native2ascii工具将UTF-8属性文件转换为ISO-8859-1属性文件外,还可以使用自定义工具,以便控制属性文件的加载并在其中使用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工具打交道,最终得到了更好的可维护属性文件

另见:

可能您的捆绑包文件编码不正确?如何创建属性文件?我使用Eclipse。。。我从eclipse内部创建属性文件作为普通文本文件,并将其编码设置为“UTF-8”。。。它真的很好用。。。现在是I18N,不客气。到底哪一部分很难理解?我可以编辑答案使它更清楚。在我的情况下&根据你的答案。。。从UTF8Control初始化对象将是:ResourceBundle=ResourceBundle.getBundle(“瑞典语”,新的UTF8Control());