| 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 <
|