本章已纯代码演示
package test;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import org.junit.Test;
public class i18nTest {
@Test
public void testLocale(){
Locale locale=Locale.CHINA;
//zh_CN
locale.toString();
//CN
locale.getCountry();
//zh
locale.getLanguage();
//中国
locale.getDisplayCountry();
//中文
locale.getDisplayLanguage();
//中文(中国)
locale.getDisplayName();
//得到所有国家简写 (数组)
locale.getISOCountries();
//得到所有语言简写 (数组)
locale.getISOLanguages();
//得到所有类型Locale对象 (Locale数组)
locale.getAvailableLocales();
}
@Test
public void testDateFormat() throws ParseException{
//创建一个Locale
Locale locale=Locale.CHINA;
//创建一个DateFormat,设置时间格式
DateFormat df=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale);
//当前系统的时间
Date date=new Date();
//转换
String d=df.format(date);
//2017-4-9 15:29:39
System.out.println(d);
}
@Test
public void testNumberFormat(){
//创建一个Locale
Locale locale=Locale.CHINA;
//一个double类型
double d=89757.78;
//格式化数字
NumberFormat nf1=NumberFormat.getNumberInstance(locale);
String s1=nf1.format(d);
//89,757.78
System.out.println(s1);
//格式化货币
NumberFormat nf2=NumberFormat.getCurrencyInstance(locale);
String s2=nf2.format(d);
//¥89,757.78
System.out.println(s2);
}
@Test
public void testMessageFormat(){
//设置占位符
String msg="{0}{1}{2}";
//...可变参数(可变数组)
String s=MessageFormat.format(msg,"你","是","猪");
//打印 你是猪
System.out.println(s);
}
@Test
public void testResourceBundle(){
//创建一个Locale
Locale locale=Locale.CHINA;
//创建一个ResourceBundle
ResourceBundle resourceBundle=ResourceBundle.getBundle("i18n", locale);
//打印:用户名 如果是Locale.US 打印:name
System.out.println(resourceBundle.getString("uname"));
}
}