格式化错误信息

新建一个MyResources_zh_CN.properties资源,定义以下错误信息:
10010=ContractNo[%s] is not exist.

使用ResourceBundle来实现多国语言的格式化,错误信息中可以有一个或多个参数,通过定义一个或多个s%,然后在代码里面定义一个数组,然后调用String.format方法即可动态生成提示信息。

代码:
public class FormatString {

public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("MyResources",
Locale.CHINA);

String errorMsg = bundle.getString("10010");

Object[] o = { "CN01" };
System.out.println(String.format(errorMsg, o));
}
}



猜你喜欢

转载自myfwz.iteye.com/blog/1262501