在這裏SO繼其他線程,我想出了這個解決方案,也考慮用於屬性文件的編碼(這可能會很麻煩作爲資源包默認使用「ISO-8859-1」):
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");
這很容易,它的工作prefectly – 2010-08-17 15:00:42