i18 ResourceBundle MessageFormat native2ascii
java.util.ResourceBundle
//支持i18国际化的基础类
Resource bundles contain locale-specific objects. When your program needs a
locale-specific resource, a String for example, your program can load it from the
resource bundle that is appropriate for the current user's locale. In this way, you
can write program code that is largely independent of the user's locale isolating
most, if not all, of the locale-specific information in resource bundles.
比如有一个lang_zh_CN.properties文件,为了国际化,将文件编码设成utf-8,这时候,如果用下面的类
public class I18Resources {
private ResourceBundle propers = null;
public I18Resources() {
propers = ResourceBundle.getBundle("res".concat(File.separator).concat("lang"));
// 在res目录中找到 lang_zh_CN.properties的文件,虽然文件是utf-8编码,但是得到的
将还会是乱码,因为默认是用iso来处理的,可以,用java.util.Properties来处理也是一样的乱码,
因为都是用iso的...网上有方法说 System.setProperty("file.encoding", "utf-8");
这也是不行的,这似乎只对System.out有影响,但不会影响jvm的
}
public String getI18(String name, String[] params) {
String value = propers.getString(name);
if(value == null)
return "";
return MessageFormat.format(value, params);
//这里是将参数替换 properties文件中某个key值可能存在的变量 {0}, {1}, {2} ...
}
public String getI18(String name) {
return getI18(name, new String[] {});
}
}
在上面的lang_zh_CN.properties为utf-8编码,和上面的类存的时候,使用得到的仍是乱码,
还差一步,就是要用 native2ascii 命令将 lang_zh_CN.properties转成 \uxxxx这种格式的
码.在ant中或cmd中使用都可以.
<target name="native2ascii">
<delete file="${targetclassdir}/res/lang_zh_CN.properties" />
<native2ascii encoding="utf-8" src="${sourcedir}" dest="${targetclassdir}" includes="res/lang_zh_CN.properties" />
</target>
Labels:
Trackback: http://cwq.iou1314.com/_a159
Trackback: http://cwq.iou1314.com/_a159




















