目录结构:
所有的文件都在包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