我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。

我的应用程序运行在谷歌应用程序引擎。

谁能给我举个例子?我找不到这份工作。


当前回答

打开设置/首选项对话框(Ctrl + Alt + S),然后单击编辑器和文件编码。

然后,在底部,您将找到属性文件的默认编码。选择您的编码类型。

或者,您可以在资源包中使用unicode符号而不是文本(例如“ів”等于\u0456\u0432)

其他回答

As one suggested, i went through implementation of resource bundle.. but that did not help.. as the bundle was always called under en_US locale... i tried to set my default locale to a different language and still my implementation of resource bundle control was being called with en_US... i tried to put log messages and do a step through debug and see if a different local call was being made after i change locale at run time through xhtml and JSF calls... that did not happend... then i tried to do a system set default to a utf8 for reading files by my server (tomcat server).. but that caused pronlem as all my class libraries were not compiled under utf8 and tomcat started to read then in utf8 format and server was not running properly... then i ended up with implementing a method in my java controller to be called from xhtml files.. in that method i did the following:

        public String message(String key, boolean toUTF8) throws Throwable{
            String result = "";
            try{
                FacesContext context = FacesContext.getCurrentInstance();
                String message = context.getApplication().getResourceBundle(context, "messages").getString(key);

                result = message==null ? "" : toUTF8 ? new String(message.getBytes("iso8859-1"), "utf-8") : message;
            }catch(Throwable t){}
            return result;
        }

我特别紧张,因为这可能会降低我应用程序的性能……然而,在实现这个之后,看起来好像我的应用程序现在更快了。我认为这是因为,我现在直接访问属性,而不是让JSF解析其访问属性的方式…我特别在这个调用中传递布尔参数,因为我知道一些属性不会被翻译,不需要在utf8格式…

现在我已经以UTF8格式保存了我的属性文件,它工作正常,因为我的应用程序中的每个用户都有一个引用的区域设置偏好。

打开设置/首选项对话框(Ctrl + Alt + S),然后单击编辑器和文件编码。

然后,在底部,您将找到属性文件的默认编码。选择您的编码类型。

或者,您可以在资源包中使用unicode符号而不是文本(例如“ів”等于\u0456\u0432)

我们创建了一个资源。utf8文件,包含UTF-8格式的资源,并有一个规则运行如下:

native2ascii -encoding utf8 resources.utf8 resources.properties

下面是一个Java 7解决方案,它使用了Guava出色的支持库和try-with-resources结构。它使用UTF-8读取和写入属性文件,以获得最简单的整体体验。

读取一个UTF-8属性文件:

File file =  new File("/path/to/example.properties");

// Create an empty set of properties
Properties properties = new Properties();

if (file.exists()) {

  // Use a UTF-8 reader from Guava
  try (Reader reader = Files.newReader(file, Charsets.UTF_8)) {
    properties.load(reader);
  } catch (IOException e) {
    // Do something
  }
}

用UTF-8格式写一个属性文件:

File file =  new File("/path/to/example.properties");

// Use a UTF-8 writer from Guava
try (Writer writer = Files.newWriter(file, Charsets.UTF_8)) {
  properties.store(writer, "Your title here");
  writer.flush();
} catch (IOException e) {
  // Do something
}

这个问题终于在Java 9中得到了解决: https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9

属性文件的默认编码现在是UTF-8。

大多数现有属性文件不应受到影响:UTF-8和 ISO-8859-1对ASCII字符有相同的编码 人类可读的非ascii ISO-8859-1编码不是有效的UTF-8。如果一个 检测到无效的UTF-8字节序列,Java运行时 自动重新读取ISO-8859-1中的文件。