1 7 8 22 23 package java.util; 24 25 import java.io.*; 26 import java.security.AccessController ; 27 import java.text.MessageFormat ; 28 import sun.security.action.GetPropertyAction; 29 import sun.text.resources.LocaleData; 30 31 142 143 public final class Locale implements Cloneable , Serializable { 144 145 147 static public final Locale ENGLISH = new Locale ("en","",""); 148 149 151 static public final Locale FRENCH = new Locale ("fr","",""); 152 153 155 static public final Locale GERMAN = new Locale ("de","",""); 156 157 159 static public final Locale ITALIAN = new Locale ("it","",""); 160 161 163 static public final Locale JAPANESE = new Locale ("ja","",""); 164 165 167 static public final Locale KOREAN = new Locale ("ko","",""); 168 169 171 static public final Locale CHINESE = new Locale ("zh","",""); 172 173 175 static public final Locale SIMPLIFIED_CHINESE = new Locale ("zh","CN",""); 176 177 179 static public final Locale TRADITIONAL_CHINESE = new Locale ("zh","TW",""); 180 181 183 static public final Locale FRANCE = new Locale ("fr","FR",""); 184 185 187 static public final Locale GERMANY = new Locale ("de","DE",""); 188 189 191 static public final Locale ITALY = new Locale ("it","IT",""); 192 193 195 static public final Locale JAPAN = new Locale ("ja","JP",""); 196 197 199 static public final Locale KOREA = new Locale ("ko","KR",""); 200 201 203 static public final Locale CHINA = new Locale ("zh","CN",""); 204 205 207 static public final Locale PRC = new Locale ("zh","CN",""); 208 209 211 static public final Locale TAIWAN = new Locale ("zh","TW",""); 212 213 215 static public final Locale UK = new Locale ("en","GB",""); 216 217 219 static public final Locale US = new Locale ("en","US",""); 220 221 223 static public final Locale CANADA = new Locale ("en","CA",""); 224 225 227 static public final Locale CANADA_FRENCH = new Locale ("fr","CA",""); 228 229 231 static final long serialVersionUID = 9149081749638150636L; 232 233 244 public Locale(String language, String country, String variant) { 245 this.language = convertOldISOCodes(language); 246 this.country = toUpperCase(country).intern(); 247 this.variant = variant.intern(); 248 } 249 250 260 public Locale(String language, String country) { 261 this(language, country, ""); 262 } 263 264 274 public Locale(String language) { 275 this(language, "", ""); 276 } 277 278 279 291 public static Locale getDefault() { 292 if (defaultLocale == null) { 295 String language, region, country, variant; 296 language = (String ) AccessController.doPrivileged( 297 new GetPropertyAction("user.language", "en")); 298 region = (String ) AccessController.doPrivileged( 300 new GetPropertyAction("user.region")); 301 if (region != null) { 302 int i = region.indexOf('_'); 304 if (i >= 0) { 305 country = region.substring(0, i); 306 variant = region.substring(i + 1); 307 } else { 308 country = region; 309 variant = ""; 310 } 311 } else { 312 country = (String ) AccessController.doPrivileged( 313 new GetPropertyAction("user.country", "")); 314 variant = (String ) AccessController.doPrivileged( 315 new GetPropertyAction("user.variant", "")); 316 } 317 defaultLocale = new Locale (language, country, variant); 318 } 319 return defaultLocale; 320 } 321 322 347 public static synchronized void setDefault(Locale newLocale) { 348 if (newLocale == null) 349 throw new NullPointerException ("Can't set default locale to NULL"); 350 351 SecurityManager sm = System.getSecurityManager(); 352 if (sm != null) sm.checkPermission(new PropertyPermission 353 ("user.language", "write")); 354 defaultLocale = newLocale; 355 } 356 357 364 public static Locale [] getAvailableLocales() { 365 return LocaleData.getAvailableLocales("LocaleString"); 366 } 367 368 372 public static String [] getISOCountries() { 373 if (isoCountries == null) { 374 isoCountries = new String [compressedIsoCountries.length() / 6]; 375 for (int i = 0; i < isoCountries.length; i++) 376 isoCountries[i] = compressedIsoCountries.substring((i * 6) + 1, (i * 6) + 3); 377 } 378 String [] result = new String [isoCountries.length]; 379 System.arraycopy(isoCountries, 0, result, 0, isoCountries.length); 380 return result; 381 } 382 383 390 public static String [] getISOLanguages() { 391 if (isoLanguages == null) { 392 isoLanguages = new String [compressedIsoLanguages.length() / 6]; 393 for (int i = 0; i < isoLanguages.length; i++) 394 isoLanguages[i] = compressedIsoLanguages.substring((i * 6) + 1, (i * 6) + 3); 395 } 396 String [] result = new String [isoLanguages.length]; 397 System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length); 398 return result; 399 } 400 401 415 public String getLanguage() { 416 return language; 417 } 418 419 424 public String getCountry() { 425 return country; 426 } 427 428 432 public String getVariant() { 433 return variant; 434 } 435 436 448 public final String toString() { 449 boolean l = language.length() != 0; 450 boolean c = country.length() != 0; 451 boolean v = variant.length() != 0; 452 StringBuffer result = new StringBuffer (language); 453 if (c||(l&&v)) { 454 result.append('_').append(country); } 456 if (v&&(l||c)) { 457 result.append('_').append(variant); 458 } 459 return result.toString(); 460 } 461 462 471 public String getISO3Language() throws MissingResourceException { 472 int length = language.length(); 473 474 if (length == 0) { 475 return ""; 476 } 477 478 int index = compressedIsoLanguages.indexOf("," + language); 479 if (index == -1 || length != 2) { 480 throw new MissingResourceException ("Couldn't find 3-letter language code for " 481 + language, "LocaleElements_" + toString(), "ShortLanguage"); 482 } 483 return compressedIsoLanguages.substring(index + 3, index + 6); 484 } 485 486 495 public String getISO3Country() throws MissingResourceException { 496 int length = country.length(); 497 498 if (length == 0) { 499 return ""; 500 } 501 502 int index = compressedIsoCountries.indexOf("," + country); 503 if (index == -1 || length != 2) { 504 throw new MissingResourceException ("Couldn't find 3-letter country code for " 505 + country, "LocaleElements_" + toString(), "ShortCountry"); 506 } 507 return compressedIsoCountries.substring(index + 3, index + 6); 508 } 509 510 522 public final String getDisplayLanguage() { 523 return getDisplayLanguage(getDefault()); 524 } 525 526 539 public String getDisplayLanguage(Locale inLocale) { 540 String langCode = language; 541 if (langCode.length() == 0) 542 return ""; 543 544 Locale workingLocale = (Locale )inLocale.clone(); 545 String result = null; 546 int phase = 0; 547 boolean done = false; 548 549 if (workingLocale.variant.length() == 0) 550 phase = 1; 551 if (workingLocale.country.length() == 0) 552 phase = 2; 553 554 while (!done) { 555 try { 556 ResourceBundle bundle = LocaleData.getLocaleElements(workingLocale); 557 result = findStringMatch((String [][])bundle.getObject("Languages"), 558 langCode, langCode); 559 if (result.length() != 0) 560 done = true; 561 } 562 catch (Exception e) { 563 } 565 566 if (!done) { 567 switch (phase) { 568 case 0: 569 workingLocale = new Locale (workingLocale.language, 570 workingLocale.country, 571 ""); 572 break; 573 574 case 1: 575 workingLocale = new Locale (workingLocale.language, 576 "", 577 workingLocale.variant); 578 break; 579 580 case 2: 581 workingLocale = getDefault(); 582 break; 583 584 case 3: 585 workingLocale = new Locale ("", "", ""); 586 break; 587 588 default: 589 return langCode; 590 } 591 phase++; 592 } 593 } 594 return result; 595 } 596 597 609 public final String getDisplayCountry() { 610 return getDisplayCountry(getDefault()); 611 } 612 613 626 public String getDisplayCountry(Locale inLocale) { 627 String ctryCode = country; 628 if (ctryCode.length() == 0) 629 return ""; 630 631 Locale workingLocale = (Locale )inLocale.clone(); 632 String result = null; 633 int phase = 0; 634 boolean done = false; 635 636 if (workingLocale.variant.length() == 0) 637 phase = 1; 638 if (workingLocale.country.length() == 0) 639 phase = 2; 640 641 while (!done) { 642 try { 643 ResourceBundle bundle = LocaleData.getLocaleElements(workingLocale); 644 result = findStringMatch((String [][])bundle.getObject("Countries"), 645 ctryCode, ctryCode); 646 if (result.length() != 0) 647 done = true; 648 } 649 catch (Exception e) { 650 } 652 653 if (!done) { 654 switch (phase) { 655 case 0: 656 workingLocale = new Locale (workingLocale.language, 657 workingLocale.country, 658 ""); 659 break; 660 661 case 1: 662 workingLocale = new Locale (workingLocale.language, 663 "", 664 workingLocale.variant); 665 break; 666 667 case 2: 668 workingLocale = getDefault(); 669 break; 670 671 case 3: 672 workingLocale = new Locale ("", "", ""); 673 break; 674 675 default: 676 return ctryCode; 677 } 678 phase++; 679 } 680 } 681 return result; 682 } 683 684 689 public final String getDisplayVariant() { 690 return getDisplayVariant(getDefault()); 691 } 692 693 698 public String getDisplayVariant(Locale inLocale) { 699 if (variant.length() == 0) 700 return ""; 701 702 ResourceBundle bundle = LocaleData.getLocaleElements(inLocale); 703 704 String names[] = getDisplayVariantArray(bundle); 705 706 String [] patterns; 709 try { 710 patterns = (String [])bundle.getObject("LocaleNamePatterns"); 711 } 712 catch (MissingResourceException e) { 713 patterns = null; 714 } 715 return formatList(patterns, names); 716 } 717 718 733 public final String getDisplayName() { 734 return getDisplayName(getDefault()); 735 } 736 737 752 public String getDisplayName(Locale inLocale) { 753 ResourceBundle bundle = LocaleData.getLocaleElements(inLocale); 754 755 String languageName = getDisplayLanguage(inLocale); 756 String countryName = getDisplayCountry(inLocale); 757 String [] variantNames = getDisplayVariantArray(bundle); 758 759 String [] patterns; 761 try { 762 patterns = (String [])bundle.getObject("LocaleNamePatterns"); 763 } 764 catch (MissingResourceException e) { 765 patterns = null; 766 } 767 768 String mainName = null; 772 String [] qualifierNames = null; 773 774 if (languageName.length() != 0) { 778 mainName = languageName; 779 if (countryName.length() != 0) { 780 qualifierNames = new String [variantNames.length + 1]; 781 System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length); 782 qualifierNames[0] = countryName; 783 } 784 else qualifierNames = variantNames; 785 } 786 else if (countryName.length() != 0) { 787 mainName = countryName; 788 qualifierNames = variantNames; 789 } 790 else { 791 return formatList(patterns, variantNames); 792 } 793 794 Object [] displayNames = { 800 new Integer (qualifierNames.length != 0 ? 2 : 1), 801 mainName, 802 qualifierNames.length != 0 ? formatList(patterns, qualifierNames) : null 807 }; 808 809 if (patterns != null) { 810 return new MessageFormat (patterns[0]).format(displayNames); 811 } 812 else { 813 StringBuffer result = new StringBuffer (); 817 result.append((String )displayNames[1]); 818 if (displayNames.length > 2) { 819 result.append(" ("); 820 result.append((String )displayNames[2]); 821 result.append(")"); 822 } 823 return result.toString(); 824 } 825 } 826 827 830 public Object clone() 831 { 832 try { 833 Locale that = (Locale )super.clone(); 834 return that; 835 } catch (CloneNotSupportedException e) { 836 throw new InternalError (); 837 } 838 } 839 840 845 public int hashCode() { 846 int hc = hashCodeValue; 847 if (hc == 0) { 848 hc = (language.hashCode() << 8) ^ country.hashCode() ^ (variant.hashCode() << 4); 849 hashCodeValue = hc; 850 } 851 return hc; 852 } 853 854 856 863 864 public boolean equals(Object obj) { 865 if (this == obj) return true; 867 if (!(obj instanceof Locale )) 868 return false; 869 Locale other = (Locale ) obj; 870 return language == other.language 871 && country == other.country 872 && variant == other.variant; 873 } 874 875 877 880 884 private final String language; 885 886 890 private final String country; 891 892 896 private final String variant; 897 898 902 private volatile int hashcode = -1; 904 907 private transient volatile int hashCodeValue = 0; 908 909 private static Locale defaultLocale = null; 910 911 916 private String [] getDisplayVariantArray(ResourceBundle bundle) { 917 StringTokenizer tokenizer = new StringTokenizer (variant, "_"); 919 String [] names = new String [tokenizer.countTokens()]; 920 921 for (int i=0; i<names.length; ++i) { 924 String token = tokenizer.nextToken(); 925 try { 926 names[i] = (String )bundle.getObject("%%" + token); 927 } 928 catch (MissingResourceException e) { 929 names[i] = token; 930 } 931 } 932 933 return names; 934 } 935 936 946 private static String formatList(String [] patterns, String [] stringList) { 947 if (patterns == null) { 950 StringBuffer result = new StringBuffer (); 951 for (int i=0; i<stringList.length; ++i) { 952 if (i>0) result.append(','); 953 result.append(stringList[i]); 954 } 955 return result.toString(); 956 } 957 958 if (stringList.length > 3) { 960 MessageFormat format = new MessageFormat (patterns[2]); 961 stringList = composeList(format, stringList); 962 } 963 964 Object [] args = new Object [stringList.length + 1]; 966 System.arraycopy(stringList, 0, args, 1, stringList.length); 967 args[0] = new Integer (stringList.length); 968 969 MessageFormat format = new MessageFormat (patterns[1]); 971 return format.format(args); 972 } 973 974 983 private static String [] composeList(MessageFormat format, String [] list) { 984 if (list.length <= 3) return list; 985 986 String [] listItems = { list[0], list[1] }; 988 String newItem = format.format(listItems); 989 990 String [] newList = new String [list.length-1]; 992 System.arraycopy(list, 2, newList, 1, newList.length-1); 993 newList[0] = newItem; 994 995 return composeList(format, newList); 997 } 998 999 1005 private Object readResolve() throws java.io.ObjectStreamException { 1006 return new Locale (language, country, variant); 1007 } 1008 1009 1010 1021 private static String [] isoLanguages = null; 1022 private static final String compressedIsoLanguages = 1023 ",aaaar,ababk,aeave,afafr,akaka,amamh,anarg,arara,asasm,avava" 1024 + ",ayaym,azaze,babak,bebel,bgbul,bhbih,bibis,bmbam,bnben,bobod" 1025 + ",brbre,bsbos,cacat,ceche,chcha,cocos,crcre,csces,cuchu,cvchv" 1026 + ",cycym,dadan,dedeu,dvdiv,dzdzo,eeewe,elell,eneng,eoepo,esspa" 1027 + ",etest,eueus,fafas,ffful,fifin,fjfij,fofao,frfra,fyfry,gagle" 1028 + ",gdgla,glglg,gngrn,guguj,gvglv,hahau,heheb,hihin,hohmo,hrhrv" 1029 + ",hthat,huhun,hyhye,hzher,iaina,idind,ieile,igibo,iiiii,ikipk" 1030 + ",inind,ioido,isisl,itita,iuiku,iwheb,jajpn,jiyid,jvjav,kakat" 1031 + ",kgkon,kikik,kjkua,kkkaz,klkal,kmkhm,knkan,kokor,krkau,kskas" 1032 + ",kukur,kvkom,kwcor,kykir,lalat,lbltz,lglug,lilim,lnlin,lolao" 1033 + ",ltlit,lulub,lvlav,mgmlg,mhmah,mimri,mkmkd,mlmal,mnmon,momol" 1034 + ",mrmar,msmsa,mtmlt,mymya,nanau,nbnob,ndnde,nenep,ngndo,nlnld" 1035 + ",nnnno,nonor,nrnbl,nvnav,nynya,ococi,ojoji,omorm,orori,ososs" 1036 + ",papan,pipli,plpol,pspus,ptpor,quque,rmroh,rnrun,roron,rurus" 1037 + ",rwkin,sasan,scsrd,sdsnd,sesme,sgsag,sisin,skslk,slslv,smsmo" 1038 + ",snsna,sosom,sqsqi,srsrp,ssssw,stsot,susun,svswe,swswa,tatam" 1039 + ",tetel,tgtgk,ththa,titir,tktuk,tltgl,tntsn,toton,trtur,tstso" 1040 + ",tttat,twtwi,tytah,uguig,ukukr,ururd,uzuzb,veven,vivie,vovol" 1041 + ",wawln,wowol,xhxho,yiyid,yoyor,zazha,zhzho,zuzul"; 1042 1043 1054 private static String [] isoCountries = null; 1055 private static final String compressedIsoCountries = 1056 ",ADAND,AEARE,AFAFG,AGATG,AIAIA,ALALB,AMARM,ANANT,AOAGO,AQATA" 1057 + ",ARARG,ASASM,ATAUT,AUAUS,AWABW,AXALA,AZAZE,BABIH,BBBRB,BDBGD,BEBEL" 1058 + ",BFBFA,BGBGR,BHBHR,BIBDI,BJBEN,BMBMU,BNBRN,BOBOL,BRBRA,BSBHS" 1059 + ",BT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SDN,SESWE,SGSGP" 1075 + ",SHSHN,SISVN,SJSJM,SKSVK,SLSLE,SMSMR,SNSEN,SOSOM,SRSUR,STSTP" 1076 + ",SVSLV,SYSYR,SZSWZ,TCTCA,TDTCD,TFATF,TGTGO,THTHA,TJTJK,TKTKL" 1077 + ",TLTLS,TMTKM,TNTUN,TOTON,TRTUR,TTTTO,TVTUV,TWTWN,TZTZA,UAUKR" 1078 + ",UGUGA,UMUMI,USUSA,UYURY,UZUZB,VAVAT,VCVCT,VEVEN,VGVGB,VIVIR" 1079 + ",VNVNM,VUVUT,WFWLF,WSWSM,YEYEM,YTMYT,ZAZAF,ZMZMB,ZWZWE"; 1080 1081 1086 private String toLowerCase(String str) { 1087 char[] buf = new char[str.length()]; 1088 for (int i = 0; i < buf.length; i++) { 1089 buf[i] = Character.toLowerCase(str.charAt(i)); 1090 } 1091 return new String ( buf ); 1092 } 1093 1094 1099 private String toUpperCase(String str) { 1100 char[] buf = new char[str.length()]; 1101 for (int i = 0; i < buf.length; i++) { 1102 buf[i] = Character.toUpperCase(str.charAt(i)); 1103 } 1104 return new String ( buf ); 1105 } 1106 1107 private String findStringMatch(String [][] languages, 1108 String desiredLanguage, String fallbackLanguage) 1109 { 1110 for (int i = 0; i < languages.length; ++i) 1111 if (desiredLanguage.equals(languages[i][0])) 1112 return languages[i][1]; 1113 if (!fallbackLanguage.equals(desiredLanguage)) 1114 for (int i = 0; i < languages.length; ++i) 1115 if (fallbackLanguage.equals(languages[i][0])) 1116 return languages[i][1]; 1117 if (!"EN".equals(desiredLanguage) && "EN".equals(fallbackLanguage)) 1118 for (int i = 0; i < languages.length; ++i) 1119 if ("EN".equals(languages[i][0])) 1120 return languages[i][1]; 1121 return ""; 1122 } 1123 1124 private String convertOldISOCodes(String language) { 1125 language = toLowerCase(language).intern(); 1128 if (language == "he") { 1129 return "iw"; 1130 } else if (language == "yi") { 1131 return "ji"; 1132 } else if (language == "id") { 1133 return "in"; 1134 } else { 1135 return language; 1136 } 1137 } 1138} 1139 | Popular Tags |