目录结构:
所有的文件都在包demo下.
MlLogin.java
package demo;
import java.util.ListResourceBundle;
public class MlLogin extends ListResourceBundle
{
public MlLogin()
{
}
static final java.lang.Object[][] contents = {
{ "lblUserName", "lblUserName" },
{ "lblPassword", "lblUserName" },
{ "caseName", "lblUserName" }
};
public java.lang.Object[][] getContents() {
return contents;
}
}
ApplicationResource_zh_TW.properties
error.requiredfield=The field is required to save
label.phone=\u96fb\u8a71
label.name=\u540d\u5b57
MlLogin_zh_TW.java
package demo;
import java.util.ListResourceBundle;
public class MlLogin_zh_TW extends ListResourceBundle
{
public MlLogin_zh_TW()
{
}
static final java.lang.Object[][] contents = {
{ "lblUserName", "帳號" },
{ "lblPassword", "密碼" },
{ "caseName", "登入" }
};
public java.lang.Object[][] getContents() {
return contents;
}
}
FormatExample.java
package demo;
import java.util.ResourceBundle;
import java.util.Locale;
import java.text.MessageFormat;
/**
*
* @author limohao
*/
public class FormatExample {
/** Creates a new instance of FormatExample */
public FormatExample() {
}
public static void main(String [] args){
String ss="demo.ApplicationResource";
ResourceBundle bundle=ResourceBundle.getBundle(ss);
String requiredFieldMessage =bundle.getString("error.requiredfield");
String [] messageArgs=new String[1];
messageArgs[0]=bundle.getString("label.name");
String formatNameMessage=MessageFormat.format(requiredFieldMessage,messageArgs);
System.out.println(formatNameMessage);
messageArgs[0]=bundle.getString("label.phone");
String formatPhoneMessage=MessageFormat.format(requiredFieldMessage,messageArgs);
System.out.println(formatPhoneMessage);
try{
// Class test=Class.forName("demo.mlLogin");
// String cname=test.getName();
String cname="demo.MlLogin";
bundle=bundle.getBundle(cname);
String temp=bundle.getString("lblUserName");
System.out.println(temp);
}catch(Exception e){
e.printStackTrace();
}
}
}
Note:如果是在简体环境下测试可将zh_TW改为zn_CN