java 国际化

来源:互联网 发布:淘宝真鞋店铺 编辑:程序博客网 时间:2024/06/02 03:15

package cn.maodun.locale;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.ResourceBundle;

public class TestLocale {
 public static void main(String[] args) {
  test() ;
  land() ;
  read() ;
 }
 public static  void test(){
  Locale locale = new Locale("zh","CN") ;
  NumberFormat currFmt = NumberFormat.getCurrencyInstance(locale) ;
  double amt = 123456.78 ;
  System.out.println(currFmt.format(amt));
  
  Locale locale2 = new Locale("en","US") ;
  Date date = new Date() ;
  DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM,locale2) ;
  System.out.println(df.format(date));
 }
 public static void land(){
  String pattern1 = "{0},您好!您与{1}在工商银行存入{2}元."  ;
  String pattern2 = "At {1,time,short} On {1,date,long}, {0}paid {2,number,currency}." ;
  Object[] params = {"徐磊",new GregorianCalendar().getTime(),new Double(1.0E3)} ;
  String msg1 = MessageFormat.format(pattern1,params) ;
  MessageFormat mf = new MessageFormat(pattern2,Locale.US) ;
  String msg2 = mf.format(params) ;
  System.out.println(msg1);
  System.out.println(msg2);
 }
 public static void read(){
  Locale locale = new Locale("en","US") ;
  Locale locale2 = new Locale("zh","CN") ;
  ResourceBundle rb = ResourceBundle.getBundle("resource",locale) ;
  ResourceBundle rb2 = ResourceBundle.getBundle("resource",locale2) ;
  String s  = rb.getString("greeting.common") ;
  String s2 = rb2.getString("greeting.common") ;
  System.out.println(s2);
  System.out.println(s);
  
   ResourceBundle resb2 = ResourceBundle.getBundle("resource", Locale.getDefault());
         System.out.println(resb2.getString("greeting.common"));
  
  
 }
}

资源配置文件有三个:

resource.properties

resource_en_US.properties

resource_zh_CN.properties

里面内容为属性=内容

比如在resource_en_US.properties里面写成如下代码:

greeting.common=How are you!
greeting.common=Good morning!
greeting.afternoon = Good Afternoon!

 

ResourceBundle.getBundle()方法的使用参考java api

public static final ResourceBundle getBundle(String baseName,                       Locale locale)
Gets a resource bundle using the specified base name and locale, and the caller's class loader. Calling this method is equivalent to calling 
getBundle(baseName, locale, this.getClass().getClassLoader())
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation strategy.
Parameters:
baseName - the base name of the resource bundle, a fully qualified class name
locale - the locale for which a resource bundle is desired
Returns:
a resource bundle for the given base name and locale
Throws:
NullPointerException - ifbaseName or locale is null
MissingResourceException - if no resource bundle for the specified base name can be found

 


注意:资源配置文件需要放在src目录下,或者自己定义的classpath路径下。

         其次在jdk安装目录下/bin下有一个native2ascii.exe程序。输入文字转化为asill码值。这个码值正好是写在resource_zh_CN.properties资源文件里面的内容(属性=内容),里面不能直接写成中文。

原创粉丝点击