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

  可能重复:   如何在ResourceBundle的资源属性中使用UTF-8 我想允许国际化到我的Java Swing应用程序。我使用捆绑文件来保留其中的所有标签。 作为测试,我试图将瑞典标题设置为
JButton
。所以在我写的包文件中:
nextStepButton=nästa
在我写的Java代码中:
nextStepButton.setText(bundle.getString("nextStepButton"));
但是按钮的标题字符在运行时出现错误: 我正在使用支持Unicode的Tahoma字体。 当我通过代码手动设置按钮标题时,它看起来很好:
nextStepButton.setText("nästa");
知道为什么它在捆绑文件中失败了吗? -------------------------------------------->编辑:编码标题: 我尝试使用代码编码来自bundle文件的文本:
nextStepButton.setText(new String(bundle.getString("nextStepButton").getBytes("UTF-8")));
结果仍然是:     
已邀请:
根据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工具,并且最终得到了更好的可维护属性文件。 也可以看看: Unicode - 如何使角色正确?     
看看Java Internationalization FAQ。如果您在
.properties
文件中放入非ASCII字符,则必须使用native2ascii工具进行转换。一切都应该有效。     
问题是资源包属性文件以UTF-8编码,但您的应用程序使用Latin-1加载它。 如果您将“LATIN SMALL A WITH DIAERESIS”(拉丁语-1中的E4或0000E4作为Unicode代码点)并将其表示为UTF-8,则会获得C3 A4。如果你把它们当作拉丁文1字节,你会得到“带有TILDE的LATIN CAPITAL LETTER A”和正方形的“CURRENCY SIGN”字符...这就是字符在按钮截图中的显示方式! (顺便说一下,这是因使用错误的字符编码而导致的错误的新词... mojibake。通过在谈话中使用它来阻止你的朋友。)     

要回复问题请先登录注册