我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。
我的应用程序运行在谷歌应用程序引擎。
谁能给我举个例子?我找不到这份工作。
我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。
我的应用程序运行在谷歌应用程序引擎。
谁能给我举个例子?我找不到这份工作。
当前回答
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格式保存了我的属性文件,它工作正常,因为我的应用程序中的每个用户都有一个引用的区域设置偏好。
其他回答
假设你有一个ResourceBundle实例,你可以通过以下方式获取String:
String val = bundle.getString(key);
我解决了我的日语显示问题:
return new String(val.getBytes("ISO-8859-1"), "UTF-8");
Properties prop = new Properties();
String fileName = "./src/test/resources/predefined.properties";
FileInputStream inputStream = new FileInputStream(fileName);
InputStreamReader reader = new InputStreamReader(inputStream,"UTF-8");
ResourceBundle。例如,如果属性文件使用cp1251字符集,则使用UTF-8和new String方法的控件将不起作用。
所以我建议使用一种常用的方法:使用unicode符号。:
IDEA -有一个特殊的“透明本机到ascii转换”选项(设置>文件编码)。
Eclipse——有一个插件“属性编辑器”。它可以作为单独的应用程序工作。
从Java 9开始,加载属性文件的默认值已更改为UTF-8。https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9.htm
我尝试使用Rod提供的方法,但考虑到BalusC关注的是不要在所有应用程序中重复相同的工作,并附带了这个类:
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyResourceBundle {
// feature variables
private ResourceBundle bundle;
private String fileEncoding;
public MyResourceBundle(Locale locale, String fileEncoding){
this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
this.fileEncoding = fileEncoding;
}
public MyResourceBundle(Locale locale){
this(locale, "UTF-8");
}
public String getString(String key){
String value = bundle.getString(key);
try {
return new String(value.getBytes("ISO-8859-1"), fileEncoding);
} catch (UnsupportedEncodingException e) {
return value;
}
}
}
使用它的方式与常规的ResourceBundle使用非常相似:
private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)
或者你可以使用默认使用UTF-8的替代构造函数:
private MyResourceBundle labels = new MyResourceBundle("es");