📄 locale.java
字号:
/** * Creates a new locale for the given language and country. * * @param language lowercase two-letter ISO-639 A2 language code * @param country uppercase two-letter ISO-3166 A2 country code * @throws NullPointerException if either argument is null */ public Locale(String language, String country) { this(language, country, ""); } /** * Creates a new locale for a language. * * @param language lowercase two-letter ISO-639 A2 language code * @throws NullPointerException if either argument is null * @since 1.4 */ public Locale(String language) { this(language, "", ""); } /** * Returns the default Locale. The default locale is generally once set * on start up and then never changed. Normally you should use this locale * for everywhere you need a locale. The initial setting matches the * default locale, the user has chosen. * * @return the default locale for this virtual machine */ public static Locale getDefault() { return defaultLocale; } /** * Changes the default locale. Normally only called on program start up. * Note that this doesn't change the locale for other programs. This has * a security check, * <code>PropertyPermission("user.language", "write")</code>, because of * its potential impact to running code. * * @param newLocale the new default locale * @throws NullPointerException if newLocale is null * @throws SecurityException if permission is denied */ public static void setDefault(Locale newLocale) { if (newLocale == null) throw new NullPointerException(); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkPermission(new PropertyPermission("user.language", "write")); defaultLocale = newLocale; } /** * Returns the list of available locales. * * @return the installed locales */ public static synchronized Locale[] getAvailableLocales() { if (availableLocales == null) { String[] localeNames = LocaleData.localeNames; availableLocales = new Locale[localeNames.length]; for (int i = 0; i < localeNames.length; i++) { String language; String region = ""; String variant = ""; String name = localeNames[i]; language = name.substring(0, 2); if (name.length() > 2) region = name.substring(3); int index = region.indexOf("_"); if (index > 0) { variant = region.substring(index + 1); region = region.substring(0, index - 1); } availableLocales[i] = getLocale(language, region, variant); } } return availableLocales; } /** * Returns a list of all 2-letter uppercase country codes as defined * in ISO 3166. * * @return a list of acceptable country codes */ public static String[] getISOCountries() { if (countryCache == null) { countryCache = getISOStrings("territories"); } return countryCache; } /** * Returns a list of all 2-letter lowercase language codes as defined * in ISO 639 (both old and new variant). * * @return a list of acceptable language codes */ public static String[] getISOLanguages() { if (languageCache == null) { languageCache = getISOStrings("languages"); } return languageCache; } /** * Returns the set of keys from the specified resource hashtable, filtered * so that only two letter strings are returned. * * @param tableName the name of the table from which to retrieve the keys. * @return an array of two-letter strings. */ private static String[] getISOStrings(String tableName) { int count = 0; ResourceBundle bundle = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation"); Enumeration e = bundle.getKeys(); ArrayList tempList = new ArrayList(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); if (key.startsWith(tableName + ".")) { String str = key.substring(tableName.length() + 1); if (str.length() == 2 && Character.isLetter(str.charAt(0)) && Character.isLetter(str.charAt(1))) { tempList.add(str); ++count; } } } String[] strings = new String[count]; for (int a = 0; a < count; ++a) strings[a] = (String) tempList.get(a); return strings; } /** * Returns the language code of this locale. Some language codes have changed * as ISO 639 has evolved; this returns the old name, even if you built * the locale with the new one. * * @return language code portion of this locale, or an empty String */ public String getLanguage() { return language; } /** * Returns the country code of this locale. * * @return country code portion of this locale, or an empty String */ public String getCountry() { return country; } /** * Returns the variant code of this locale. * * @return the variant code portion of this locale, or an empty String */ public String getVariant() { return variant; } /** * Gets the string representation of the current locale. This consists of * the language, the country, and the variant, separated by an underscore. * The variant is listed only if there is a language or country. Examples: * "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC". * * @return the string representation of this Locale * @see #getDisplayName() */ public String toString() { if (language.length() == 0 && country.length() == 0) return ""; else if (country.length() == 0 && variant.length() == 0) return language; StringBuffer result = new StringBuffer(language); result.append('_').append(country); if (variant.length() != 0) result.append('_').append(variant); return result.toString(); } /** * Returns the three-letter ISO language abbrevation of this locale. * * @throws MissingResourceException if the three-letter code is not known */ public String getISO3Language() { // We know all strings are interned so we can use '==' for better performance. if (language == "") return ""; int index = ("aa,ab,af,am,ar,as,ay,az,ba,be,bg,bh,bi,bn,bo,br,ca,co,cs,cy,da," + "de,dz,el,en,eo,es,et,eu,fa,fi,fj,fo,fr,fy,ga,gd,gl,gn,gu,ha,iw," + "hi,hr,hu,hy,ia,in,ie,ik,in,is,it,iu,iw,ja,ji,jw,ka,kk,kl,km,kn," + "ko,ks,ku,ky,la,ln,lo,lt,lv,mg,mi,mk,ml,mn,mo,mr,ms,mt,my,na,ne," + "nl,no,oc,om,or,pa,pl,ps,pt,qu,rm,rn,ro,ru,rw,sa,sd,sg,sh,si,sk," + "sl,sm,sn,so,sq,sr,ss,st,su,sv,sw,ta,te,tg,th,ti,tk,tl,tn,to,tr," + "ts,tt,tw,ug,uk,ur,uz,vi,vo,wo,xh,ji,yo,za,zh,zu") .indexOf(language); if (index % 3 != 0 || language.length() != 2) throw new MissingResourceException ("Can't find ISO3 language for " + language, "java.util.Locale", language); // Don't read this aloud. These are the three letter language codes. return ("aarabkaframharaasmaymazebakbelbulbihbisbenbodbrecatcoscescymdandeu" + "dzoellengepospaesteusfasfinfijfaofrafrygaigdhglggrngujhauhebhinhrv" + "hunhyeinaindileipkindislitaikuhebjpnyidjawkatkazkalkhmkankorkaskur" + "kirlatlinlaolitlavmlgmrimkdmalmonmolmarmsamltmyanaunepnldnorociorm" + "oripanpolpusporquerohrunronruskinsansndsagsrpsinslkslvsmosnasomsqi" + "srpsswsotsunsweswatamteltgkthatirtuktgltsntonturtsotattwiuigukrurd" + "uzbvievolwolxhoyidyorzhazhozul") .substring(index, index + 3); } /** * Returns the three-letter ISO country abbrevation of the locale. * * @throws MissingResourceException if the three-letter code is not known */ public String getISO3Country() { // We know all strings are interned so we can use '==' for better performance. if (country == "") return ""; int index = ("AD,AE,AF,AG,AI,AL,AM,AN,AO,AQ,AR,AS,AT,AU,AW,AZ,BA,BB,BD,BE,BF," + "BG,BH,BI,BJ,BM,BN,BO,BR,BS,BT,BV,BW,BY,BZ,CA,CC,CF,CG,CH,CI,CK," + "CL,CM,CN,CO,CR,CU,CV,CX,CY,CZ,DE,DJ,DK,DM,DO,DZ,EC,EE,EG,EH,ER," + "ES,ET,FI,FJ,FK,FM,FO,FR,FX,GA,GB,GD,GE,GF,GH,GI,GL,GM,GN,GP,GQ," + "GR,GS,GT,GU,GW,GY,HK,HM,HN,HR,HT,HU,ID,IE,IL,IN,IO,IQ,IR,IS,IT," + "JM,JO,JP,KE,KG,KH,KI,KM,KN,KP,KR,KW,KY,KZ,LA,LB,LC,LI,LK,LR,LS," + "LT,LU,LV,LY,MA,MC,MD,MG,MH,MK,ML,MM,MN,MO,MP,MQ,MR,MS,MT,MU,MV," + "MW,MX,MY,MZ,NA,NC,NE,NF,NG,NI,NL,NO,NP,NR,NU,NZ,OM,PA,PE,PF,PG," + "PH,PK,PL,PM,PN,PR,PT,PW,PY,QA,RE,RO,RU,RW,SA,SB,SC,SD,SE,SG,SH," + "SI,SJ,SK,SL,SM,SN,SO,SR,ST,SV,SY,SZ,TC,TD,TF,TG,TH,TJ,TK,TM,TN," + "TO,TP,TR,TT,TV,TW,TZ,UA,UG,UM,US,UY,UZ,VA,VC,VE,VG,VI,VN,VU,WF," + "WS,YE,YT,YU,ZA,ZM,ZR,ZW") .indexOf(country); if (index % 3 != 0 || country.length() != 2) throw new MissingResourceException ("Can't find ISO3 country for " + country, "java.util.Locale", country); // Don't read this aloud. These are the three letter country codes. return ("ANDAREAFGATGAIAALBARMANTAGOATAARGASMAUTAUSABWAZEBIHBRBBGDBELBFABGR" + "BHRBDIBENBMUBRNBOLBRABHSBTNBVTBWABLRBLZCANCCKCAFCOGCHECIVCOKCHLCMR" + "CHNCOLCRICUBCPVCXRCYPCZEDEUDJIDNKDMADOMDZAECUESTEGYESHERIESPETHFIN" + "FJIFLKFSMFROFRAFXXGABGBRGRDGEOGUFGHAGIBGRLGMBGINGLPGNQGRCSGSGTMGUM" + "GNBGUYHKGHMDHNDHRVHTIHUNIDNIRLISRINDIOTIRQIRNISLITAJAMJORJPNKENKGZ" + "KHMKIRCOMKNAPRKKORKWTCYMKAZLAOLBNLCALIELKALBRLSOLTULUXLVALBYMARMCO" + "MDAMDGMHLMKDMLIMMRMNGMACMNPMTQMRTMSRMLTMUSMDVMWIMEXMYSMOZNAMNCLNER" + "NFKNGANICNLDNORNPLNRUNIUNZLOMNPANPERPYFPNGPHLPAKPOLSPMPCNPRIPRTPLW" + "PRYQATREUROMRUSRWASAUSLBSYCSDNSWESGPSHNSVNSJMSVKSLESMRSENSOMSURSTP" + "SLVSYRSWZTCATCDATFTGOTHATJKTKLTKMTUNTONTMPTURTTOTUVTWNTZAUKRUGAUMI" + "USAURYUZBVATVCTVENVGBVIRVNMVUTWLFWSMYEMMYTYUGZAFZMBZARZWE") .substring(index, index + 3); } /** * Gets the country name suitable for display to the user, formatted * for the default locale. This has the same effect as * <pre> * getDisplayLanguage(Locale.getDefault()); * </pre> * * @return the language name of this locale localized to the default locale, * with the ISO code as backup */ public String getDisplayLanguage() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -