【Java常用类库】_国际化程序

来源:互联网 发布:sql酒店管理系统 编辑:程序博客网 时间:2024/06/05 04:45

【Java常用类库】_国际化程序

分类: Java

目录(?)[+]

  1. 实例一Message不需加入properties了程序会自动识别
  2. 实例二使用Locale进行地区识别使用MessageFormat进行格式化
  3. 实例三可变参数一
  4. 实例四可变参数二
  5. 实例五不使用资源文件使用资源类的方式调用

【Java常用类库】_国际化程序

实例一:(Message不需加入properties了,程序会自动识别)

[java] view plaincopyprint?
  1. import java.util.ResourceBundle ;  
  2. public class InterDemo01{  
  3.     public static void main(String args[]){  
  4.         ResourceBundle rb = ResourceBundle.getBundle("Message") ;    // 找到资源文件,不用编写后缀  
  5.         System.out.println("内容:" + rb.getString("info")) ;        // 从资源文件中取得内容  
  6.     }  
  7. };  



Message.properties

info = 1111HELLO


实例二:(使用Locale进行地区识别,使用MessageFormat进行格式化)

[java] view plaincopyprint?
  1. import java.util.ResourceBundle ;  
  2. import java.util.Locale ;  
  3. import java.text.* ;  
  4. public class InterDemo03{  
  5.     public static void main(String args[]){  
  6.         Locale zhLoc = new Locale("zh","CN") ;        // 表示中国地区  
  7.         Locale enLoc = new Locale("en","US") ;        // 表示美国地区  
  8.         Locale frLoc = new Locale("fr","FR") ;        // 表示法国地区  
  9.         // 找到中文的属性文件,需要指定中文的Locale对象  
  10.         ResourceBundle zhrb = ResourceBundle.getBundle("Message",zhLoc) ;  
  11.         // 找到英文的属性文件,需要指定英文的Locale对象  
  12.         ResourceBundle enrb = ResourceBundle.getBundle("Message",enLoc) ;  
  13.         // 找到法文的属性文件,需要指定法文的Locale对象  
  14.         ResourceBundle frrb = ResourceBundle.getBundle("Message",frLoc) ;  
  15.         // 依次读取各个属性文件的内容,通过键值读取,此时的键值名称统一为info  
  16.         String str1 = zhrb.getString("info") ;  
  17.         String str2 = enrb.getString("info") ;  
  18.         String str3 = frrb.getString("info") ;  
  19.         System.out.println("中文:" + MessageFormat.format(str1,"李兴华")) ;  
  20.         System.out.println("英语:" + MessageFormat.format(str2,"LiXingHua")) ;  
  21.         System.out.println("法语:" + MessageFormat.format(str3,"LiXingHua")) ;  
  22.     }  
  23. };  



Message_zh_CN.properties(内容使用\jdk1.6.0_10\bin\native2ascii.exe来得到)

info =\u4f60\u597d\uff0c{0}\uff01

Message_en_US.properties

info = Hello,{0}!

Message_fr_FR.properties

info = Bonjour,{0}!


实例三:(可变参数一)


[java] view plaincopyprint?
  1. public class InterDemo04{  
  2.     public static void main(String args[]){  
  3.         System.out.print("第一次运行:") ;  
  4.         fun("LXH","Li","李兴华") ;    // 传入三个参数  
  5.         System.out.print("\n第二次运行:") ;  
  6.         fun("MLDN") ;                // 传入一个参数  
  7.     }  
  8.     public static void fun(Object...args){    // 固定语法,输入任意多个数据,使用数组表示  
  9.         for(int i=0;i<args.length;i++){  
  10.             System.out.print(args[i] + "、") ;  
  11.         }  
  12.     }  
  13. };  



实例四:(可变参数二)


[java] view plaincopyprint?
  1. public class InterDemo05{  
  2.     public static void main(String args[]){  
  3.         System.out.print("第一次运行:") ;  
  4.         Object[] arg1 = {"LXH","Li","李兴华"} ;  
  5.         fun(arg1) ;    // 传入三个参数  
  6.         System.out.print("\n第二次运行:") ;  
  7.         Object[] arg2 = {"MLDN"} ;  
  8.         fun(arg2) ;                // 传入一个参数  
  9.         System.out.print("\n第三次运行:") ;  
  10.         Object[] arg3 = {} ;        // 没有参数传入  
  11.         fun(arg3) ;  
  12.     }  
  13.     public static void fun(Object...args){    // 固定语法,输入任意多个数据,使用数组表示  
  14.         for(int i=0;i<args.length;i++){  
  15.             System.out.print(args[i] + "、") ;  
  16.         }  
  17.     }  
  18. };  



实例五:(不使用资源文件,使用资源类的方式调用)


资源类:

[java] view plaincopyprint?
  1. import java.util.ListResourceBundle ;  
  2. public class Message_zh_CN extends ListResourceBundle{  
  3.     private final Object data[][] = {  
  4.         {"info","中文,你好,{0}!"}  
  5.     } ;  
  6.     public Object[][] getContents(){    // 覆写的方法  
  7.         return data ;  
  8.     }  
  9. };  



调用程序:

[java] view plaincopyprint?
  1. import java.util.ResourceBundle ;  
  2. import java.util.Locale ;  
  3. import java.text.* ;  
  4. public class InterDemo06{  
  5.     public static void main(String args[]){  
  6.         Locale zhLoc = new Locale("zh","CN") ;        // 表示中国地区  
  7.         // 找到中文的属性文件,需要指定中文的Locale对象  
  8.         ResourceBundle zhrb = ResourceBundle.getBundle("Message",zhLoc) ;  
  9.         String str1 = zhrb.getString("info") ;  
  10.         System.out.println("中文:" + MessageFormat.format(str1,"李兴华")) ;  
  11.     }  
  12. };  
0 0
原创粉丝点击