相关疑难解决方法(0)

将resourcebundle读为UTF-8.getString()方法似乎将编码更改为ISO-8859

我有幸将完整的工作区,项目和文件的编码更改为UTF-8编码.我们有几个Resourcebundles,用于使用unicode编写特殊字符.我们还希望通过切换到UTF-8来摆脱那个unicode的东西,所以我也改变了Resourcebundles(.properties)文件的编码并替换了Unicode字符.

我们也有德国资源捆绑和一些像

Ä,Ö,Ü,ß.ä,ö,ü以及像"或"这样的特殊字符

在浏览器中未正确显示.例:

Resourcebundleentry:

executeShellCommand.label =Shellkommandoausführen

浏览器结果: 在此输入图像描述

使用Java.util.ResourceBundle.getString(String key)方法读取resourcebundle:

    public String getLocalizedString(ResourceBundle bundle, String key) {
    try {
        System.out.println("getLocalizedString, key: " + key + ", resourcebundle: " + bundle.getString(key));
        return bundle.getString(key);
    } catch (MissingResourceException e) {
        return key;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我检查上面的Sysout的输出我得到以下: getLocalizedString, key: executeShellCommand.label, resourcebundle: Shellkommando ausführen

似乎getString(key)方法在将字符串从字符串读取到标准resourcbundleencoding(ISO-8859)时更改字符的编码.

我试图解决这个问题:

    public String getLocalizedString(ResourceBundle bundle, String key) {
    try {
        System.out.println("getLocalizedString, key: " + key + ", resourcebundle: " + new String (bundle.getString(key).getBytes(), "UTF-8"));
        return …
Run Code Online (Sandbox Code Playgroud)

java encoding properties resourcebundle utf-8

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

encoding ×1

java ×1

properties ×1

resourcebundle ×1

utf-8 ×1