📄 locale.java
字号:
* @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 Locale[] getAvailableLocales() { /* I only return those for which localized language * or country information exists. * XXX - remove hard coded list, and implement more locales (Sun's JDK 1.4 * has 148 installed locales!). */ return new Locale[] { ENGLISH, FRENCH, GERMAN, new Locale("ga", "") }; } /** * 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() { return new String[] { "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" }; } /** * 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() { return new String[] { "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", "he", "hi", "hr", "hu", "hy", "ia", "id", "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", "yi", "yo", "za", "zh", "zu" }; } /** * 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() { return getDisplayLanguage(defaultLocale); } /** * <p> * Gets the name of the language specified by this locale, in a form suitable * for display to the user. If possible, the display name will be localized * to the specified locale. For example, if the locale instance is * <code>Locale.GERMANY</code>, and the specified locale is <code>Locale.UK</code>, * the result would be 'German'. Using the German locale would instead give
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -