Locale类有如下几个构造器:
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
下面是通过构造方法实例化Locale对象:
1 public class localeTest { 2 public static void main(String[] args) { 3 4 //创建一个通用英语的locale. 5 Locale locale11 = new Locale("en"); 6 //创建一个加拿大英语的locale. 7 Locale locale12 = new Locale("en", "CA"); 8 //创建一个美式英语的locale,硅谷的英语 9 Locale locale13 = new Locale("en", "US", "SiliconValley"); 10 11 } 12 }
Locale类有一些预定义的Locale对象实例:
Locale Name Locale
=======================================
Locale.CHINA zh_CN
Locale.CHINESE zh
Locale.SIMPLIFIED_CHINESE zh_CN
Locale.TRADITIONAL_CHINESE zh_TW
Locale.PRC zh_CN
Locale.TAIWAN zh_TW
Locale.ENGLISH en
Locale.UK en_GB
Locale.US en_US
Locale.FRANCE fr_FR
Locale.FRENCH fr
下面的例子是德国locale实例以英语(默认情况下是作者的主机),德语和法语提供了信息:
1 public class localeTest { 2 3 public static void main(String[] args) { 4 Locale deLocale = new Locale("de", "DE"); 5 Locale frLocale = new Locale("fr", "FR"); 6 System.out.println("Default language name (default): " 7 + deLocale.getDisplayLanguage()); 8 System.out.println("German language name (German): " 9 + deLocale.getDisplayLanguage(deLocale)); 10 System.out.println("German language name (French): " 11 + deLocale.getDisplayLanguage(frLocale)); 12 } 13 14 }
输出结果为:
Default language name (default): German
German language name (German): Deutsch
German language name (French): allemand