1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.util.Locale; 13 14 import hunt.Exceptions; 15 import hunt.Functions; 16 import hunt.io.Common; 17 import hunt.logging.ConsoleLogger; 18 import hunt.collection; 19 import hunt.system.Environment; 20 import hunt.text.StringBuilder; 21 import hunt.util.Common; 22 import hunt.util.Configuration; 23 // import java.text.MessageFormat; 24 // import java.util.concurrent.ConcurrentHashMap; 25 // import java.util.spi.LocaleNameProvider; 26 // import java.util.stream.Collectors; 27 28 // import sun.security.action.GetPropertyAction; 29 // import sun.util.locale.BaseLocale; 30 // import sun.util.locale.InternalLocaleBuilder; 31 // import sun.util.locale.LanguageTag; 32 // import sun.util.locale.LocaleExtensions; 33 // import sun.util.locale.LocaleMatcher; 34 // import sun.util.locale.LocaleObjectCache; 35 // import sun.util.locale.LocaleSyntaxException; 36 // import sun.util.locale.LocaleUtils; 37 // import sun.util.locale.ParseStatus; 38 // import sun.util.locale.provider.LocaleProviderAdapter; 39 // import sun.util.locale.provider.LocaleResources; 40 // import sun.util.locale.provider.LocaleServiceProviderPool; 41 // import sun.util.locale.provider.TimeZoneNameUtility; 42 43 import std.array; 44 import std.concurrency : initOnce; 45 import std.string; 46 47 /** 48 * A <code>Locale</code> object represents a specific geographical, political, 49 * or cultural region. An operation that requires a <code>Locale</code> to perform 50 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code> 51 * to tailor information for the user. For example, displaying a number 52 * is a locale-sensitive operation— the number should be formatted 53 * according to the customs and conventions of the user's native country, 54 * region, or culture. 55 * 56 * <p> The {@code Locale} class implements IETF BCP 47 which is composed of 57 * <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 "Matching of Language 58 * Tags"</a> and <a href="http://tools.ietf.org/html/rfc5646">RFC 5646 "Tags 59 * for Identifying Languages"</a> with support for the LDML (UTS#35, "Unicode 60 * Locale Data Markup Language") BCP 47-compatible extensions for locale data 61 * exchange. 62 * 63 * <p> A <code>Locale</code> object logically consists of the fields 64 * described below. 65 * 66 * <dl> 67 * <dt><a id="def_language"><b>language</b></a></dt> 68 * 69 * <dd>ISO 639 alpha-2 or alpha-3 language code, or registered 70 * language subtags up to 8 alpha letters (for future enhancements). 71 * When a language has both an alpha-2 code and an alpha-3 code, the 72 * alpha-2 code must be used. You can find a full list of valid 73 * language codes in the IANA Language Subtag Registry (search for 74 * "Type: language"). The language field is case insensitive, but 75 * <code>Locale</code> always canonicalizes to lower case.</dd> 76 * 77 * <dd>Well-formed language values have the form 78 * <code>[a-zA-Z]{2,8}</code>. Note that this is not the full 79 * BCP47 language production, since it excludes extlang. They are 80 * not needed since modern three-letter language codes replace 81 * them.</dd> 82 * 83 * <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd> 84 * 85 * <dt><a id="def_script"><b>script</b></a></dt> 86 * 87 * <dd>ISO 15924 alpha-4 script code. You can find a full list of 88 * valid script codes in the IANA Language Subtag Registry (search 89 * for "Type: script"). The script field is case insensitive, but 90 * <code>Locale</code> always canonicalizes to title case (the first 91 * letter is upper case and the rest of the letters are lower 92 * case).</dd> 93 * 94 * <dd>Well-formed script values have the form 95 * <code>[a-zA-Z]{4}</code></dd> 96 * 97 * <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd> 98 * 99 * <dt><a id="def_region"><b>country (region)</b></a></dt> 100 * 101 * <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code. 102 * You can find a full list of valid country and region codes in the 103 * IANA Language Subtag Registry (search for "Type: region"). The 104 * country (region) field is case insensitive, but 105 * <code>Locale</code> always canonicalizes to upper case.</dd> 106 * 107 * <dd>Well-formed country/region values have 108 * the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd> 109 * 110 * <dd>Example: "US" (United States), "FR" (France), "029" 111 * (Caribbean)</dd> 112 * 113 * <dt><a id="def_variant"><b>variant</b></a></dt> 114 * 115 * <dd>Any arbitrary value used to indicate a variation of a 116 * <code>Locale</code>. Where there are two or more variant values 117 * each indicating its own semantics, these values should be ordered 118 * by importance, with most important first, separated by 119 * underscore('_'). The variant field is case sensitive.</dd> 120 * 121 * <dd>Note: IETF BCP 47 places syntactic restrictions on variant 122 * subtags. Also BCP 47 subtags are strictly used to indicate 123 * additional variations that define a language or its dialects that 124 * are not covered by any combinations of language, script and 125 * region subtags. You can find a full list of valid variant codes 126 * in the IANA Language Subtag Registry (search for "Type: variant"). 127 * 128 * <p>However, the variant field in <code>Locale</code> has 129 * historically been used for any kind of variation, not just 130 * language variations. For example, some supported variants 131 * available in Java SE Runtime Environments indicate alternative 132 * cultural behaviors such as calendar type or number script. In 133 * BCP 47 this kind of information, which does not identify the 134 * language, is supported by extension subtags or private use 135 * subtags.</dd> 136 * 137 * <dd>Well-formed variant values have the form <code>SUBTAG 138 * (('_'|'-') SUBTAG)*</code> where <code>SUBTAG = 139 * [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only 140 * uses hyphen ('-') as a delimiter, this is more lenient).</dd> 141 * 142 * <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd> 143 * 144 * <dt><a id="def_extensions"><b>extensions</b></a></dt> 145 * 146 * <dd>A map from single character keys to string values, indicating 147 * extensions apart from language identification. The extensions in 148 * <code>Locale</code> implement the semantics and syntax of BCP 47 149 * extension subtags and private use subtags. The extensions are 150 * case insensitive, but <code>Locale</code> canonicalizes all 151 * extension keys and values to lower case. Note that extensions 152 * cannot have empty values.</dd> 153 * 154 * <dd>Well-formed keys are single characters from the set 155 * <code>[0-9a-zA-Z]</code>. Well-formed values have the form 156 * <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x' 157 * <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys 158 * <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows 159 * single-character subtags).</dd> 160 * 161 * <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar), 162 * key="x"/value="java-1-7"</dd> 163 * </dl> 164 * 165 * <b>Note:</b> Although BCP 47 requires field values to be registered 166 * in the IANA Language Subtag Registry, the <code>Locale</code> class 167 * does not provide any validation features. The <code>Builder</code> 168 * only checks if an individual field satisfies the syntactic 169 * requirement (is well-formed), but does not validate the value 170 * itself. See {@link Builder} for details. 171 * 172 * <h3><a id="def_locale_extension">Unicode locale/language extension</a></h3> 173 * 174 * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional 175 * attributes and keywords to override or refine the default behavior 176 * associated with a locale. A keyword is represented by a pair of 177 * key and type. For example, "nu-thai" indicates that Thai local 178 * digits (value:"thai") should be used for formatting numbers 179 * (key:"nu"). 180 * 181 * <p>The keywords are mapped to a BCP 47 extension value using the 182 * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}). The above 183 * example, "nu-thai", becomes the extension "u-nu-thai". 184 * 185 * <p>Thus, when a <code>Locale</code> object contains Unicode locale 186 * attributes and keywords, 187 * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a 188 * string representing this information, for example, "nu-thai". The 189 * <code>Locale</code> class also provides {@link 190 * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and 191 * {@link #getUnicodeLocaleType} which allow you to access Unicode 192 * locale attributes and key/type pairs directly. When represented as 193 * a string, the Unicode Locale Extension lists attributes 194 * alphabetically, followed by key/type sequences with keys listed 195 * alphabetically (the order of subtags comprising a key's type is 196 * fixed when the type is defined) 197 * 198 * <p>A well-formed locale key has the form 199 * <code>[0-9a-zA-Z]{2}</code>. A well-formed locale type has the 200 * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it 201 * can be empty, or a series of subtags 3-8 alphanums in length). A 202 * well-formed locale attribute has the form 203 * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same 204 * form as a locale type subtag). 205 * 206 * <p>The Unicode locale extension specifies optional behavior in 207 * locale-sensitive services. Although the LDML specification defines 208 * various keys and values, actual locale-sensitive service 209 * implementations in a Java Runtime Environment might not support any 210 * particular Unicode locale attributes or key/type pairs. 211 * 212 * <h4>Creating a Locale</h4> 213 * 214 * <p>There are several different ways to create a <code>Locale</code> 215 * object. 216 * 217 * <h5>Builder</h5> 218 * 219 * <p>Using {@link Builder} you can construct a <code>Locale</code> object 220 * that conforms to BCP 47 syntax. 221 * 222 * <h5>Constructors</h5> 223 * 224 * <p>The <code>Locale</code> class provides three constructors: 225 * <blockquote> 226 * <pre> 227 * {@link #Locale(string language)} 228 * {@link #Locale(string language, string country)} 229 * {@link #Locale(string language, string country, string variant)} 230 * </pre> 231 * </blockquote> 232 * These constructors allow you to create a <code>Locale</code> object 233 * with language, country and variant, but you cannot specify 234 * script or extensions. 235 * 236 * <h5>Factory Methods</h5> 237 * 238 * <p>The method {@link #forLanguageTag} creates a <code>Locale</code> 239 * object for a well-formed BCP 47 language tag. 240 * 241 * <h5>Locale Constants</h5> 242 * 243 * <p>The <code>Locale</code> class provides a number of convenient constants 244 * that you can use to create <code>Locale</code> objects for commonly used 245 * locales. For example, the following creates a <code>Locale</code> object 246 * for the United States: 247 * <blockquote> 248 * <pre> 249 * Locale.US 250 * </pre> 251 * </blockquote> 252 * 253 * <h4><a id="LocaleMatching">Locale Matching</a></h4> 254 * 255 * <p>If an application or a system is internationalized and provides localized 256 * resources for multiple locales, it sometimes needs to find one or more 257 * locales (or language tags) which meet each user's specific preferences. Note 258 * that a term "language tag" is used interchangeably with "locale" in this 259 * locale matching documentation. 260 * 261 * <p>In order to do matching a user's preferred locales to a set of language 262 * tags, <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 Matching of 263 * Language Tags</a> defines two mechanisms: filtering and lookup. 264 * <em>Filtering</em> is used to get all matching locales, whereas 265 * <em>lookup</em> is to choose the best matching locale. 266 * Matching is done case-insensitively. These matching mechanisms are described 267 * in the following sections. 268 * 269 * <p>A user's preference is called a <em>Language Priority List</em> and is 270 * expressed as a list of language ranges. There are syntactically two types of 271 * language ranges: basic and extended. See 272 * {@link Locale.LanguageRange Locale.LanguageRange} for details. 273 * 274 * <h5>Filtering</h5> 275 * 276 * <p>The filtering operation returns all matching language tags. It is defined 277 * in RFC 4647 as follows: 278 * "In filtering, each language range represents the least specific language 279 * tag (that is, the language tag with fewest number of subtags) that is an 280 * acceptable match. All of the language tags in the matching set of tags will 281 * have an equal or greater number of subtags than the language range. Every 282 * non-wildcard subtag in the language range will appear in every one of the 283 * matching language tags." 284 * 285 * <p>There are two types of filtering: filtering for basic language ranges 286 * (called "basic filtering") and filtering for extended language ranges 287 * (called "extended filtering"). They may return different results by what 288 * kind of language ranges are included in the given Language Priority List. 289 * {@link Locale.FilteringMode} is a parameter to specify how filtering should 290 * be done. 291 * 292 * <h5>Lookup</h5> 293 * 294 * <p>The lookup operation returns the best matching language tags. It is 295 * defined in RFC 4647 as follows: 296 * "By contrast with filtering, each language range represents the most 297 * specific tag that is an acceptable match. The first matching tag found, 298 * according to the user's priority, is considered the closest match and is the 299 * item returned." 300 * 301 * <p>For example, if a Language Priority List consists of two language ranges, 302 * {@code "zh-Hant-TW"} and {@code "en-US"}, in prioritized order, lookup 303 * method progressively searches the language tags below in order to find the 304 * best matching language tag. 305 * <blockquote> 306 * <pre> 307 * 1. zh-Hant-TW 308 * 2. zh-Hant 309 * 3. zh 310 * 4. en-US 311 * 5. en 312 * </pre> 313 * </blockquote> 314 * If there is a language tag which matches completely to a language range 315 * above, the language tag is returned. 316 * 317 * <p>{@code "*"} is the special language range, and it is ignored in lookup. 318 * 319 * <p>If multiple language tags match as a result of the subtag {@code '*'} 320 * included in a language range, the first matching language tag returned by 321 * an {@link Iterator} over a {@link Collection} of language tags is treated as 322 * the best matching one. 323 * 324 * <h4>Use of Locale</h4> 325 * 326 * <p>Once you've created a <code>Locale</code> you can query it for information 327 * about itself. Use <code>getCountry</code> to get the country (or region) 328 * code and <code>getLanguage</code> to get the language code. 329 * You can use <code>getDisplayCountry</code> to get the 330 * name of the country suitable for displaying to the user. Similarly, 331 * you can use <code>getDisplayLanguage</code> to get the name of 332 * the language suitable for displaying to the user. Interestingly, 333 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive 334 * and have two versions: one that uses the default 335 * {@link LocaleCategory#DISPLAY DISPLAY} locale and one 336 * that uses the locale specified as an argument. 337 * 338 * <p>The Java Platform provides a number of classes that perform locale-sensitive 339 * operations. For example, the <code>NumberFormat</code> class formats 340 * numbers, currency, and percentages in a locale-sensitive manner. Classes 341 * such as <code>NumberFormat</code> have several convenience methods 342 * for creating a default object of that type. For example, the 343 * <code>NumberFormat</code> class provides these three convenience methods 344 * for creating a default <code>NumberFormat</code> object: 345 * <blockquote> 346 * <pre> 347 * NumberFormat.getInstance() 348 * NumberFormat.getCurrencyInstance() 349 * NumberFormat.getPercentInstance() 350 * </pre> 351 * </blockquote> 352 * Each of these methods has two variants; one with an explicit locale 353 * and one without; the latter uses the default 354 * {@link LocaleCategory#FORMAT FORMAT} locale: 355 * <blockquote> 356 * <pre> 357 * NumberFormat.getInstance(myLocale) 358 * NumberFormat.getCurrencyInstance(myLocale) 359 * NumberFormat.getPercentInstance(myLocale) 360 * </pre> 361 * </blockquote> 362 * A <code>Locale</code> is the mechanism for identifying the kind of object 363 * (<code>NumberFormat</code>) that you would like to get. The locale is 364 * <STRONG>just</STRONG> a mechanism for identifying objects, 365 * <STRONG>not</STRONG> a container for the objects themselves. 366 * 367 * <h4>Compatibility</h4> 368 * 369 * <p>In order to maintain compatibility with existing usage, Locale's 370 * constructors retain their behavior prior to the Java Runtime 371 * Environment version 1.7. The same is largely true for the 372 * <code>toString</code> method. Thus Locale objects can continue to 373 * be used as they were. In particular, clients who parse the output 374 * of toString into language, country, and variant fields can continue 375 * to do so (although this is strongly discouraged), although the 376 * variant field will have additional information in it if script or 377 * extensions are present. 378 * 379 * <p>In addition, BCP 47 imposes syntax restrictions that are not 380 * imposed by Locale's constructors. This means that conversions 381 * between some Locales and BCP 47 language tags cannot be made without 382 * losing information. Thus <code>toLanguageTag</code> cannot 383 * represent the state of locales whose language, country, or variant 384 * do not conform to BCP 47. 385 * 386 * <p>Because of these issues, it is recommended that clients migrate 387 * away from constructing non-conforming locales and use the 388 * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead. 389 * Clients desiring a string representation of the complete locale can 390 * then always rely on <code>toLanguageTag</code> for this purpose. 391 * 392 * <h5><a id="special_cases_constructor">Special cases</a></h5> 393 * 394 * <p>For compatibility reasons, two 395 * non-conforming locales are treated as special cases. These are 396 * <b>{@code ja_JP_JP}</b> and <b>{@code th_TH_TH}</b>. These are ill-formed 397 * in BCP 47 since the variants are too short. To ease migration to BCP 47, 398 * these are treated specially during construction. These two cases (and only 399 * these) cause a constructor to generate an extension, all other values behave 400 * exactly as they did prior to Java 7. 401 * 402 * <p>Java has used {@code ja_JP_JP} to represent Japanese as used in 403 * Japan together with the Japanese Imperial calendar. This is now 404 * representable using a Unicode locale extension, by specifying the 405 * Unicode locale key {@code ca} (for "calendar") and type 406 * {@code japanese}. When the Locale constructor is called with the 407 * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is 408 * automatically added. 409 * 410 * <p>Java has used {@code th_TH_TH} to represent Thai as used in 411 * Thailand together with Thai digits. This is also now representable using 412 * a Unicode locale extension, by specifying the Unicode locale key 413 * {@code nu} (for "number") and value {@code thai}. When the Locale 414 * constructor is called with the arguments "th", "TH", "TH", the 415 * extension "u-nu-thai" is automatically added. 416 * 417 * <h5>Serialization</h5> 418 * 419 * <p>During serialization, writeObject writes all fields to the output 420 * stream, including extensions. 421 * 422 * <p>During deserialization, readResolve adds extensions as described 423 * in <a href="#special_cases_constructor">Special Cases</a>, only 424 * for the two cases th_TH_TH and ja_JP_JP. 425 * 426 * <h5>Legacy language codes</h5> 427 * 428 * <p>Locale's constructor has always converted three language codes to 429 * their earlier, obsoleted forms: {@code he} maps to {@code iw}, 430 * {@code yi} maps to {@code ji}, and {@code id} maps to 431 * {@code in}. This continues to be the case, in order to not break 432 * backwards compatibility. 433 * 434 * <p>The APIs added in 1.7 map between the old and new language codes, 435 * maintaining the old codes internal to Locale (so that 436 * <code>getLanguage</code> and <code>toString</code> reflect the old 437 * code), but using the new codes in the BCP 47 language tag APIs (so 438 * that <code>toLanguageTag</code> reflects the new one). This 439 * preserves the equivalence between Locales no matter which code or 440 * API is used to construct them. Java's default resource bundle 441 * lookup mechanism also implements this mapping, so that resources 442 * can be named using either convention, see {@link ResourceBundle.Control}. 443 * 444 * <h5>Three-letter language/country(region) codes</h5> 445 * 446 * <p>The Locale constructors have always specified that the language 447 * and the country param be two characters in length, although in 448 * practice they have accepted any length. The specification has now 449 * been relaxed to allow language codes of two to eight characters and 450 * country (region) codes of two to three characters, and in 451 * particular, three-letter language codes and three-digit region 452 * codes as specified in the IANA Language Subtag Registry. For 453 * compatibility, the implementation still does not impose a length 454 * constraint. 455 * 456 * @see Builder 457 * @see ResourceBundle 458 * @see java.text.Format 459 * @see java.text.NumberFormat 460 * @see java.text.Collator 461 * @author Mark Davis 462 */ 463 final class Locale // : Cloneable 464 { 465 466 private __gshared Locale[size_t] LOCALECACHE; 467 468 /** Useful constant for language. 469 */ 470 static Locale ENGLISH() { 471 __gshared Locale m; 472 return initOnce!(m)(createConstant("en", "")); 473 } 474 475 /** Useful constant for language. 476 */ 477 static Locale FRENCH() { 478 __gshared Locale m; 479 return initOnce!(m)(createConstant("fr", "")); 480 } 481 482 /** Useful constant for language. 483 */ 484 static Locale GERMAN() { 485 __gshared Locale m; 486 return initOnce!(m)(createConstant("de", "")); 487 } 488 489 /** Useful constant for language. 490 */ 491 static Locale ITALIAN() { 492 __gshared Locale m; 493 return initOnce!(m)(createConstant("it", "")); 494 } 495 496 /** Useful constant for language. 497 */ 498 static Locale JAPANESE() { 499 __gshared Locale m; 500 return initOnce!(m)(createConstant("ja", "")); 501 } 502 503 /** Useful constant for language. 504 */ 505 static Locale KOREAN() { 506 __gshared Locale m; 507 return initOnce!(m)(createConstant("ko", "")); 508 } 509 510 /** Useful constant for language. 511 */ 512 static Locale CHINESE() { 513 __gshared Locale m; 514 return initOnce!(m)(createConstant("zh", "")); 515 } 516 517 /** Useful constant for language. 518 */ 519 static Locale SIMPLIFIED_CHINESE() { 520 __gshared Locale m; 521 return initOnce!(m)(createConstant("zh", "CN")); 522 } 523 524 /** Useful constant for language. 525 */ 526 static Locale TRADITIONAL_CHINESE() { 527 __gshared Locale m; 528 return initOnce!(m)(createConstant("zh", "TW")); 529 } 530 531 /** Useful constant for country. 532 */ 533 static Locale FRANCE() { 534 __gshared Locale m; 535 return initOnce!(m)(createConstant("fr", "FR")); 536 } 537 538 /** Useful constant for country. 539 */ 540 static Locale GERMANY() { 541 __gshared Locale m; 542 return initOnce!(m)(createConstant("de", "DE")); 543 } 544 545 /** Useful constant for country. 546 */ 547 static Locale ITALY() { 548 __gshared Locale m; 549 return initOnce!(m)(createConstant("it", "IT")); 550 } 551 552 /** Useful constant for country. 553 */ 554 static Locale JAPAN() { 555 __gshared Locale m; 556 return initOnce!(m)(createConstant("ja", "JP")); 557 } 558 559 /** Useful constant for country. 560 */ 561 static Locale KOREA() { 562 __gshared Locale m; 563 return initOnce!(m)(createConstant("ko", "KR")); 564 } 565 566 /** Useful constant for country. 567 */ 568 569 alias CHINA = SIMPLIFIED_CHINESE; 570 571 /** Useful constant for country. 572 */ 573 alias PRC = SIMPLIFIED_CHINESE; 574 575 /** Useful constant for country. 576 */ 577 alias TAIWAN = TRADITIONAL_CHINESE; 578 579 580 /** Useful constant for country. 581 */ 582 static Locale UK() { 583 __gshared Locale m; 584 return initOnce!(m)(createConstant("en", "GB")); 585 } 586 587 /** Useful constant for country. 588 */ 589 static Locale US() { 590 __gshared Locale m; 591 return initOnce!(m)(createConstant("en", "US")); 592 } 593 594 /** Useful constant for country. 595 */ 596 static Locale CANADA() { 597 __gshared Locale m; 598 return initOnce!(m)(createConstant("en", "CA")); 599 } 600 601 /** Useful constant for country. 602 */ 603 static Locale CANADA_FRENCH() { 604 __gshared Locale m; 605 return initOnce!(m)(createConstant("fr", "CA")); 606 } 607 608 /** 609 * Useful constant for the root locale. The root locale is the locale whose 610 * language, country, and variant are empty ("") strings. This is regarded 611 * as the base locale of all locales, and is used as the language/country 612 * neutral locale for the locale sensitive operations. 613 * 614 */ 615 static Locale ROOT() { 616 __gshared Locale m; 617 return initOnce!(m)(createConstant("", "")); 618 } 619 620 /** 621 * The key for the private use extension ('x'). 622 * 623 * @see #getExtension(char) 624 * @see Builder#setExtension(char, string) 625 */ 626 enum char PRIVATE_USE_EXTENSION = 'x'; 627 628 /** 629 * The key for Unicode locale extension ('u'). 630 * 631 * @see #getExtension(char) 632 * @see Builder#setExtension(char, string) 633 */ 634 enum char UNICODE_LOCALE_EXTENSION = 'u'; 635 636 637 /** 638 * Enum for specifying the type defined in ISO 3166. This enum is used to 639 * retrieve the two-letter ISO3166-1 alpha-2, three-letter ISO3166-1 640 * alpha-3, four-letter ISO3166-3 country codes. 641 * 642 * @see #getISOCountries(Locale.IsoCountryCode) 643 */ 644 static class IsoCountryCode 645 { 646 /** 647 * PART1_ALPHA2 is used to represent the ISO3166-1 alpha-2 two letter 648 * country codes. 649 */ 650 static IsoCountryCode PART1_ALPHA2; 651 652 static this() 653 { 654 PART1_ALPHA2 = new class IsoCountryCode 655 { 656 override Set!(string) createCountryCodeSet() 657 { 658 implementationMissing(); 659 return null; 660 // return Set.of(Locale.getISOCountries()); 661 } 662 }; 663 PART1_ALPHA3 = new class IsoCountryCode 664 { 665 override Set!(string) createCountryCodeSet() 666 { 667 implementationMissing(); 668 return null; 669 // return LocaleISOData.computeISO3166_1Alpha3Countries(); 670 } 671 }; 672 673 PART3 = new class IsoCountryCode 674 { 675 override Set!(string) createCountryCodeSet() 676 { 677 implementationMissing(); 678 return null; 679 // return Set.of(LocaleISOData.ISO3166_3); 680 } 681 }; 682 } 683 684 /** 685 * 686 * PART1_ALPHA3 is used to represent the ISO3166-1 alpha-3 three letter 687 * country codes. 688 */ 689 static IsoCountryCode PART1_ALPHA3; 690 691 /** 692 * PART3 is used to represent the ISO3166-3 four letter country codes. 693 */ 694 static IsoCountryCode PART3; 695 696 /** 697 * Concrete implementation of this method attempts to compute value 698 * for iso3166CodesMap for each IsoCountryCode type key. 699 */ 700 abstract Set!(string) createCountryCodeSet(); 701 702 /** 703 * Map to hold country codes for each ISO3166 part. 704 */ 705 private static Map!(IsoCountryCode, Set!(string)) iso3166CodesMap; 706 707 static this() 708 { 709 iso3166CodesMap = new HashMap!(IsoCountryCode, Set!(string))(); 710 } 711 712 /** 713 * This method is called from Locale class to retrieve country code set 714 * for getISOCountries(type) 715 */ 716 static Set!(string) retrieveISOCountryCodes(IsoCountryCode type) 717 { 718 implementationMissing(); 719 return null; 720 // return iso3166CodesMap.computeIfAbsent(type, IsoCountryCode.createCountryCodeSet); 721 } 722 } 723 724 /** 725 * Display types for retrieving localized names from the name providers. 726 */ 727 private enum int DISPLAY_LANGUAGE = 0; 728 private enum int DISPLAY_COUNTRY = 1; 729 private enum int DISPLAY_VARIANT = 2; 730 private enum int DISPLAY_SCRIPT = 3; 731 private enum int DISPLAY_UEXT_KEY = 4; 732 private enum int DISPLAY_UEXT_TYPE = 5; 733 734 private string language; 735 private string script; 736 private string region; 737 private string variant; 738 739 /** 740 * Private constructor used by getInstance method 741 */ 742 // private this(BaseLocale baseLocale, LocaleExtensions extensions) { 743 // this.baseLocale = baseLocale; 744 // this.localeExtensions = extensions; 745 // } 746 747 /** 748 * Construct a locale from language, country and variant. 749 * This constructor normalizes the language value to lowercase and 750 * the country value to uppercase. 751 * <p> 752 * <b>Note:</b> 753 * <ul> 754 * <li>ISO 639 is not a stable standard; some of the language codes it defines 755 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the 756 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other 757 * API on Locale will return only the OLD codes. 758 * <li>For backward compatibility reasons, this constructor does not make 759 * any syntactic checks on the input. 760 * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially, 761 * see <a href="#special_cases_constructor">Special Cases</a> for more information. 762 * </ul> 763 * 764 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag 765 * up to 8 characters in length. See the <code>Locale</code> class description about 766 * valid language values. 767 * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. 768 * See the <code>Locale</code> class description about valid country values. 769 * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>. 770 * See the <code>Locale</code> class description for the details. 771 * @exception NullPointerException thrown if any argument is null. 772 */ 773 this(string language, string country, string variant) 774 { 775 if (language is null || country is null || variant is null) 776 { 777 throw new NullPointerException(); 778 } 779 780 this.language = convertOldISOCodes(language); 781 this.region = country; 782 this.variant = variant; 783 } 784 785 /** 786 * Construct a locale from language and country. 787 * This constructor normalizes the language value to lowercase and 788 * the country value to uppercase. 789 * <p> 790 * <b>Note:</b> 791 * <ul> 792 * <li>ISO 639 is not a stable standard; some of the language codes it defines 793 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the 794 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other 795 * API on Locale will return only the OLD codes. 796 * <li>For backward compatibility reasons, this constructor does not make 797 * any syntactic checks on the input. 798 * </ul> 799 * 800 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag 801 * up to 8 characters in length. See the <code>Locale</code> class description about 802 * valid language values. 803 * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. 804 * See the <code>Locale</code> class description about valid country values. 805 * @exception NullPointerException thrown if either argument is null. 806 */ 807 this(string language, string country) 808 { 809 this(language, country, ""); 810 } 811 812 /** 813 * Construct a locale from a language code. 814 * This constructor normalizes the language value to lowercase. 815 * <p> 816 * <b>Note:</b> 817 * <ul> 818 * <li>ISO 639 is not a stable standard; some of the language codes it defines 819 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the 820 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other 821 * API on Locale will return only the OLD codes. 822 * <li>For backward compatibility reasons, this constructor does not make 823 * any syntactic checks on the input. 824 * </ul> 825 * 826 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag 827 * up to 8 characters in length. See the <code>Locale</code> class description about 828 * valid language values. 829 * @exception NullPointerException thrown if argument is null. 830 */ 831 this(string language) 832 { 833 this(language, "", ""); 834 } 835 836 /** 837 * This method must be called only for creating the Locale.* 838 * constants due to making shortcuts. 839 */ 840 private static Locale createConstant(string lang, string country) 841 { 842 // BaseLocale base = BaseLocale.createInstance(lang, country); 843 // return getInstance(base, null); 844 Locale le = new Locale(lang, country); 845 size_t key = hashOf(lang, country) ; 846 LOCALECACHE[key] = le; 847 848 return le; 849 } 850 851 static size_t hashOf(string language, string country, string script = null, string variant = null) { 852 size_t r = 0; 853 if(language !is null) 854 r += .hashOf(language.toLower()); 855 if(country !is null) 856 r += .hashOf(country.toLower()); 857 if(script !is null) 858 r += .hashOf(script.toLower()); 859 if(variant !is null) 860 r += .hashOf(variant.toLower()); 861 return r; 862 } 863 864 /** 865 * Returns a <code>Locale</code> constructed from the given 866 * <code>language</code>, <code>country</code> and 867 * <code>variant</code>. If the same <code>Locale</code> instance 868 * is available in the cache, then that instance is 869 * returned. Otherwise, a new <code>Locale</code> instance is 870 * created and cached. 871 * 872 * @param language lowercase 2 to 8 language code. 873 * @param country uppercase two-letter ISO-3166 code and numeric-3 UN M.49 area code. 874 * @param variant vendor and browser specific code. See class description. 875 * @return the <code>Locale</code> instance requested 876 * @exception NullPointerException if any argument is null. 877 */ 878 static Locale getInstance(string language, string country, string variant) { 879 return getInstance(language, "", country, variant); 880 } 881 882 static Locale getInstance(string language, string script, string country, string variant) { 883 if (language is null || script is null || country is null || variant is null) { 884 throw new NullPointerException(); 885 } 886 887 version(HUNT_DEBUG) { 888 tracef("language=%s, script=%s, country=%s, variant=%s", 889 language, script, country, variant); 890 } 891 size_t key = hashOf(language, country, script, variant); 892 Locale le = LOCALECACHE.get(key, null); 893 if(le is null) { 894 le = new Locale(language, country, variant); 895 LOCALECACHE[key] = le; 896 } 897 return le; 898 } 899 900 /** 901 * Gets the current value of the default locale for this instance 902 * of the Java Virtual Machine. 903 * <p> 904 * The Java Virtual Machine sets the default locale during startup 905 * based on the host environment. It is used by many locale-sensitive 906 * methods if no locale is explicitly specified. 907 * It can be changed using the 908 * {@link #setDefault(java.util.Locale) setDefault} method. 909 * 910 * @return the default locale for this instance of the Java Virtual Machine 911 */ 912 static Locale getDefault() 913 { 914 return initOnce!(defaultLocale)(initDefault()); 915 // do not synchronize this method - see 4071298 916 } 917 private __gshared Locale defaultLocale; 918 919 /** 920 * Gets the current value of the default locale for the specified Category 921 * for this instance of the Java Virtual Machine. 922 * <p> 923 * The Java Virtual Machine sets the default locale during startup based 924 * on the host environment. It is used by many locale-sensitive methods 925 * if no locale is explicitly specified. It can be changed using the 926 * setDefault(LocaleCategory, Locale) method. 927 * 928 * @param category - the specified category to get the default locale 929 * @throws NullPointerException if category is null 930 * @return the default locale for the specified Category for this instance 931 * of the Java Virtual Machine 932 * @see #setDefault(LocaleCategory, Locale) 933 */ 934 static Locale getDefault(LocaleCategory category) { 935 // do not synchronize this method - see 4071298 936 if(category == LocaleCategory.DISPLAY) { 937 if (defaultDisplayLocale is null) { 938 synchronized { 939 if (defaultDisplayLocale is null) { 940 defaultDisplayLocale = initDefault(category); 941 } 942 } 943 } 944 return defaultDisplayLocale; 945 } else if(category == LocaleCategory.FORMAT) { 946 if (defaultFormatLocale is null) { 947 synchronized { 948 if (defaultFormatLocale is null) { 949 defaultFormatLocale = initDefault(category); 950 } 951 } 952 } 953 return defaultFormatLocale; 954 } else { 955 assert(false, "Unknown locale category"); 956 } 957 } 958 959 private static Locale initDefault() { 960 ConfigBuilder props = Environment.getProperties(); 961 string language = props.getProperty("user.language", "en"); 962 string script = props.getProperty("user.script", ""); 963 string country = props.getProperty("user.country", ""); 964 string variant = props.getProperty("user.variant", ""); 965 966 return getInstance(language, script, country, variant); 967 } 968 969 private static Locale initDefault(LocaleCategory category) { 970 ConfigBuilder props = Environment.getProperties(); 971 Locale defaultLocale = getDefault(); 972 973 return getInstance( 974 props.getProperty(category.languageKey, 975 defaultLocale.getLanguage()), 976 props.getProperty(category.scriptKey, 977 defaultLocale.getScript()), 978 props.getProperty(category.countryKey, 979 defaultLocale.getCountry()), 980 props.getProperty(category.variantKey, 981 defaultLocale.getVariant())); 982 } 983 984 985 /** 986 * Sets the default locale for this instance of the Java Virtual Machine. 987 * This does not affect the host locale. 988 * <p> 989 * If there is a security manager, its <code>checkPermission</code> 990 * method is called with a <code>PropertyPermission("user.language", "write")</code> 991 * permission before the default locale is changed. 992 * <p> 993 * The Java Virtual Machine sets the default locale during startup 994 * based on the host environment. It is used by many locale-sensitive 995 * methods if no locale is explicitly specified. 996 * <p> 997 * Since changing the default locale may affect many different areas 998 * of functionality, this method should only be used if the caller 999 * is prepared to reinitialize locale-sensitive code running 1000 * within the same Java Virtual Machine. 1001 * <p> 1002 * By setting the default locale with this method, all of the default 1003 * locales for each Category are also set to the specified default locale. 1004 * 1005 * @throws SecurityException 1006 * if a security manager exists and its 1007 * <code>checkPermission</code> method doesn't allow the operation. 1008 * @throws NullPointerException if <code>newLocale</code> is null 1009 * @param newLocale the new default locale 1010 * @see SecurityManager#checkPermission 1011 * @see java.util.PropertyPermission 1012 */ 1013 static void setDefault(Locale newLocale) { 1014 setDefault(LocaleCategory.DISPLAY, newLocale); 1015 setDefault(LocaleCategory.FORMAT, newLocale); 1016 defaultLocale = newLocale; 1017 } 1018 1019 /** 1020 * Sets the default locale for the specified Category for this instance 1021 * of the Java Virtual Machine. This does not affect the host locale. 1022 * <p> 1023 * If there is a security manager, its checkPermission method is called 1024 * with a PropertyPermission("user.language", "write") permission before 1025 * the default locale is changed. 1026 * <p> 1027 * The Java Virtual Machine sets the default locale during startup based 1028 * on the host environment. It is used by many locale-sensitive methods 1029 * if no locale is explicitly specified. 1030 * <p> 1031 * Since changing the default locale may affect many different areas of 1032 * functionality, this method should only be used if the caller is 1033 * prepared to reinitialize locale-sensitive code running within the 1034 * same Java Virtual Machine. 1035 * 1036 * @param category - the specified category to set the default locale 1037 * @param newLocale - the new default locale 1038 * @throws SecurityException if a security manager exists and its 1039 * checkPermission method doesn't allow the operation. 1040 * @throws NullPointerException if category and/or newLocale is null 1041 * @see SecurityManager#checkPermission(java.security.Permission) 1042 * @see PropertyPermission 1043 * @see #getDefault(LocaleCategory) 1044 */ 1045 static void setDefault(LocaleCategory category, Locale newLocale) { 1046 implementationMissing(false); 1047 // if (category is null) 1048 // throw new NullPointerException("Category cannot be NULL"); 1049 // if (newLocale is null) 1050 // throw new NullPointerException("Can't set default locale to NULL"); 1051 1052 // SecurityManager sm = System.getSecurityManager(); 1053 // if (sm !is null) sm.checkPermission(new PropertyPermission 1054 // ("user.language", "write")); 1055 // switch (category) { 1056 // case DISPLAY: 1057 // defaultDisplayLocale = newLocale; 1058 // break; 1059 // case FORMAT: 1060 // defaultFormatLocale = newLocale; 1061 // break; 1062 // default: 1063 // assert false: "Unknown Category"; 1064 // } 1065 } 1066 1067 // /** 1068 // * Returns an array of all installed locales. 1069 // * The returned array represents the union of locales supported 1070 // * by the Java runtime environment and by installed 1071 // * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} 1072 // * implementations. It must contain at least a <code>Locale</code> 1073 // * instance equal to {@link java.util.Locale#US Locale.US}. 1074 // * 1075 // * @return An array of installed locales. 1076 // */ 1077 // static Locale[] getAvailableLocales() { 1078 // return LocaleServiceProviderPool.getAllAvailableLocales(); 1079 // } 1080 1081 // /** 1082 // * Returns a list of all 2-letter country codes defined in ISO 3166. 1083 // * Can be used to create Locales. 1084 // * This method is equivalent to {@link #getISOCountries(Locale.IsoCountryCode type)} 1085 // * with {@code type} {@link IsoCountryCode#PART1_ALPHA2}. 1086 // * <p> 1087 // * <b>Note:</b> The <code>Locale</code> class also supports other codes for 1088 // * country (region), such as 3-letter numeric UN M.49 area codes. 1089 // * Therefore, the list returned by this method does not contain ALL valid 1090 // * codes that can be used to create Locales. 1091 // * <p> 1092 // * Note that this method does not return obsolete 2-letter country codes. 1093 // * ISO3166-3 codes which designate country codes for those obsolete codes, 1094 // * can be retrieved from {@link #getISOCountries(Locale.IsoCountryCode type)} with 1095 // * {@code type} {@link IsoCountryCode#PART3}. 1096 // * @return An array of ISO 3166 two-letter country codes. 1097 // */ 1098 // static string[] getISOCountries() { 1099 // if (isoCountries is null) { 1100 // isoCountries = getISO2Table(LocaleISOData.isoCountryTable); 1101 // } 1102 // string[] result = new string[isoCountries.length]; 1103 // System.arraycopy(isoCountries, 0, result, 0, isoCountries.length); 1104 // return result; 1105 // } 1106 1107 // /** 1108 // * Returns a {@code Set} of ISO3166 country codes for the specified type. 1109 // * 1110 // * @param type {@link Locale.IsoCountryCode} specified ISO code type. 1111 // * @see java.util.Locale.IsoCountryCode 1112 // * @throws NullPointerException if type is null 1113 // * @return a {@code Set} of ISO country codes for the specified type. 1114 // */ 1115 // static Set!(string) getISOCountries(IsoCountryCode type) { 1116 // assert(type); 1117 // return IsoCountryCode.retrieveISOCountryCodes(type); 1118 // } 1119 1120 // /** 1121 // * Returns a list of all 2-letter language codes defined in ISO 639. 1122 // * Can be used to create Locales. 1123 // * <p> 1124 // * <b>Note:</b> 1125 // * <ul> 1126 // * <li>ISO 639 is not a stable standard— some languages' codes have changed. 1127 // * The list this function returns includes both the new and the old codes for the 1128 // * languages whose codes have changed. 1129 // * <li>The <code>Locale</code> class also supports language codes up to 1130 // * 8 characters in length. Therefore, the list returned by this method does 1131 // * not contain ALL valid codes that can be used to create Locales. 1132 // * </ul> 1133 // * 1134 // * @return An array of ISO 639 two-letter language codes. 1135 // */ 1136 // static string[] getISOLanguages() { 1137 // if (isoLanguages is null) { 1138 // isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable); 1139 // } 1140 // string[] result = new string[isoLanguages.length]; 1141 // System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length); 1142 // return result; 1143 // } 1144 1145 // private static string[] getISO2Table(string table) { 1146 // int len = table.length() / 5; 1147 // string[] isoTable = new string[len]; 1148 // for (int i = 0, j = 0; i < len; i++, j += 5) { 1149 // isoTable[i] = table.substring(j, j + 2); 1150 // } 1151 // return isoTable; 1152 // } 1153 1154 /** 1155 * Returns the language code of this Locale. 1156 * 1157 * <p><b>Note:</b> ISO 639 is not a stable standard— some languages' codes have changed. 1158 * Locale's constructor recognizes both the new and the old codes for the languages 1159 * whose codes have changed, but this function always returns the old code. If you 1160 * want to check for a specific language whose code has changed, don't do 1161 * <pre> 1162 * if (locale.getLanguage().equals("he")) // BAD! 1163 * ... 1164 * </pre> 1165 * Instead, do 1166 * <pre> 1167 * if (locale.getLanguage().equals(new Locale("he").getLanguage())) 1168 * ... 1169 * </pre> 1170 * @return The language code, or the empty string if none is defined. 1171 * @see #getDisplayLanguage 1172 */ 1173 string getLanguage() { 1174 return language; 1175 } 1176 1177 /** 1178 * Returns the script for this locale, which should 1179 * either be the empty string or an ISO 15924 4-letter script 1180 * code. The first letter is uppercase and the rest are 1181 * lowercase, for example, 'Latn', 'Cyrl'. 1182 * 1183 * @return The script code, or the empty string if none is defined. 1184 * @see #getDisplayScript 1185 */ 1186 string getScript() { 1187 return script; 1188 } 1189 1190 /** 1191 * Returns the country/region code for this locale, which should 1192 * either be the empty string, an uppercase ISO 3166 2-letter code, 1193 * or a UN M.49 3-digit code. 1194 * 1195 * @return The country/region code, or the empty string if none is defined. 1196 * @see #getDisplayCountry 1197 */ 1198 string getCountry() { 1199 return region; 1200 } 1201 1202 /** 1203 * Returns the variant code for this locale. 1204 * 1205 * @return The variant code, or the empty string if none is defined. 1206 * @see #getDisplayVariant 1207 */ 1208 string getVariant() { 1209 return variant; 1210 } 1211 1212 /** 1213 * Returns {@code true} if this {@code Locale} has any <a href="#def_extensions"> 1214 * extensions</a>. 1215 * 1216 * @return {@code true} if this {@code Locale} has any extensions 1217 */ 1218 bool hasExtensions() { 1219 return false; 1220 } 1221 1222 // /** 1223 // * Returns a copy of this {@code Locale} with no <a href="#def_extensions"> 1224 // * extensions</a>. If this {@code Locale} has no extensions, this {@code Locale} 1225 // * is returned. 1226 // * 1227 // * @return a copy of this {@code Locale} with no extensions, or {@code this} 1228 // * if {@code this} has no extensions 1229 // */ 1230 // Locale stripExtensions() { 1231 // return hasExtensions() ? Locale.getInstance(baseLocale, null) : this; 1232 // } 1233 1234 // /** 1235 // * Returns the extension (or private use) value associated with 1236 // * the specified key, or null if there is no extension 1237 // * associated with the key. To be well-formed, the key must be one 1238 // * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so 1239 // * for example 'z' and 'Z' represent the same extension. 1240 // * 1241 // * @param key the extension key 1242 // * @return The extension, or null if this locale defines no 1243 // * extension for the specified key. 1244 // * @throws IllegalArgumentException if key is not well-formed 1245 // * @see #PRIVATE_USE_EXTENSION 1246 // * @see #UNICODE_LOCALE_EXTENSION 1247 // */ 1248 // string getExtension(char key) { 1249 // if (!LocaleExtensions.isValidKey(key)) { 1250 // throw new IllegalArgumentException("Ill-formed extension key: " ~ key); 1251 // } 1252 // return hasExtensions() ? localeExtensions.getExtensionValue(key) : null; 1253 // } 1254 1255 // /** 1256 // * Returns the set of extension keys associated with this locale, or the 1257 // * empty set if it has no extensions. The returned set is unmodifiable. 1258 // * The keys will all be lower-case. 1259 // * 1260 // * @return The set of extension keys, or the empty set if this locale has 1261 // * no extensions. 1262 // */ 1263 // Set<Character> getExtensionKeys() { 1264 // if (!hasExtensions()) { 1265 // return Collections.emptySet(); 1266 // } 1267 // return localeExtensions.getKeys(); 1268 // } 1269 1270 // /** 1271 // * Returns the set of unicode locale attributes associated with 1272 // * this locale, or the empty set if it has no attributes. The 1273 // * returned set is unmodifiable. 1274 // * 1275 // * @return The set of attributes. 1276 // */ 1277 // Set!(string) getUnicodeLocaleAttributes() { 1278 // if (!hasExtensions()) { 1279 // return Collections.emptySet(); 1280 // } 1281 // return localeExtensions.getUnicodeLocaleAttributes(); 1282 // } 1283 1284 // /** 1285 // * Returns the Unicode locale type associated with the specified Unicode locale key 1286 // * for this locale. Returns the empty string for keys that are defined with no type. 1287 // * Returns null if the key is not defined. Keys are case-insensitive. The key must 1288 // * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is 1289 // * thrown. 1290 // * 1291 // * @param key the Unicode locale key 1292 // * @return The Unicode locale type associated with the key, or null if the 1293 // * locale does not define the key. 1294 // * @throws IllegalArgumentException if the key is not well-formed 1295 // * @throws NullPointerException if <code>key</code> is null 1296 // */ 1297 string getUnicodeLocaleType(string key) { 1298 /* if (!isUnicodeExtensionKey(key)) { 1299 throw new IllegalArgumentException("Ill-formed Unicode locale key: " ~ key); 1300 } 1301 return hasExtensions() ? localeExtensions.getUnicodeLocaleType(key) : null; */ 1302 return "iso"; 1303 } 1304 1305 // /** 1306 // * Returns the set of Unicode locale keys defined by this locale, or the empty set if 1307 // * this locale has none. The returned set is immutable. Keys are all lower case. 1308 // * 1309 // * @return The set of Unicode locale keys, or the empty set if this locale has 1310 // * no Unicode locale keywords. 1311 // */ 1312 // Set!(string) getUnicodeLocaleKeys() { 1313 // if (localeExtensions is null) { 1314 // return Collections.emptySet(); 1315 // } 1316 // return localeExtensions.getUnicodeLocaleKeys(); 1317 // } 1318 1319 // /** 1320 // * Package locale method returning the Locale's BaseLocale, 1321 // * used by ResourceBundle 1322 // * @return base locale of this Locale 1323 // */ 1324 // BaseLocale getBaseLocale() { 1325 // return baseLocale; 1326 // } 1327 1328 // /** 1329 // * Package private method returning the Locale's LocaleExtensions, 1330 // * used by ResourceBundle. 1331 // * @return locale extensions of this Locale, 1332 // * or {@code null} if no extensions are defined 1333 // */ 1334 // LocaleExtensions getLocaleExtensions() { 1335 // return localeExtensions; 1336 // } 1337 1338 /** 1339 * Returns a string representation of this <code>Locale</code> 1340 * object, consisting of language, country, variant, script, 1341 * and extensions as below: 1342 * <blockquote> 1343 * language ~ "_" ~ country ~ "_" ~ (variant ~ "_#" | "#") + script ~ "_" ~ extensions 1344 * </blockquote> 1345 * 1346 * Language is always lower case, country is always upper case, script is always title 1347 * case, and extensions are always lower case. Extensions and private use subtags 1348 * will be in canonical order as explained in {@link #toLanguageTag}. 1349 * 1350 * <p>When the locale has neither script nor extensions, the result is the same as in 1351 * Java 6 and prior. 1352 * 1353 * <p>If both the language and country fields are missing, this function will return 1354 * the empty string, even if the variant, script, or extensions field is present (you 1355 * can't have a locale with just a variant, the variant must accompany a well-formed 1356 * language or country code). 1357 * 1358 * <p>If script or extensions are present and variant is missing, no underscore is 1359 * added before the "#". 1360 * 1361 * <p>This behavior is designed to support debugging and to be compatible with 1362 * previous uses of <code>toString</code> that expected language, country, and variant 1363 * fields only. To represent a Locale as a string for interchange purposes, use 1364 * {@link #toLanguageTag}. 1365 * 1366 * <p>Examples: <ul> 1367 * <li>{@code en}</li> 1368 * <li>{@code de_DE}</li> 1369 * <li>{@code _GB}</li> 1370 * <li>{@code en_US_WIN}</li> 1371 * <li>{@code de__POSIX}</li> 1372 * <li>{@code zh_CN_#Hans}</li> 1373 * <li>{@code zh_TW_#Hant_x-java}</li> 1374 * <li>{@code th_TH_TH_#u-nu-thai}</li></ul> 1375 * 1376 * @return A string representation of the Locale, for debugging. 1377 * @see #getDisplayName 1378 * @see #toLanguageTag 1379 */ 1380 override string toString() { 1381 1382 bool l = (language.length != 0); 1383 bool s = (script.length != 0); 1384 bool r = (region.length != 0); 1385 bool v = (variant.length != 0); 1386 // bool e = (localeExtensions !is null && localeExtensions.getID().length() != 0); 1387 1388 StringBuilder result = new StringBuilder(language); 1389 if (r || (l && (v || s ))) { 1390 result.append('_') 1391 .append(region); // This may just append '_' 1392 } 1393 if (v && (l || r)) { 1394 result.append('_') 1395 .append(variant); 1396 } 1397 1398 if (s && (l || r)) { 1399 result.append("_#") 1400 .append(script); 1401 } 1402 1403 return result.toString(); 1404 } 1405 1406 // /** 1407 // * Returns a well-formed IETF BCP 47 language tag representing 1408 // * this locale. 1409 // * 1410 // * <p>If this <code>Locale</code> has a language, country, or 1411 // * variant that does not satisfy the IETF BCP 47 language tag 1412 // * syntax requirements, this method handles these fields as 1413 // * described below: 1414 // * 1415 // * <p><b>Language:</b> If language is empty, or not <a 1416 // * href="#def_language" >well-formed</a> (for example "a" or 1417 // * "e2"), it will be emitted as "und" (Undetermined). 1418 // * 1419 // * <p><b>Country:</b> If country is not <a 1420 // * href="#def_region">well-formed</a> (for example "12" or "USA"), 1421 // * it will be omitted. 1422 // * 1423 // * <p><b>Variant:</b> If variant <b>is</b> <a 1424 // * href="#def_variant">well-formed</a>, each sub-segment 1425 // * (delimited by '-' or '_') is emitted as a subtag. Otherwise: 1426 // * <ul> 1427 // * 1428 // * <li>if all sub-segments match <code>[0-9a-zA-Z]{1,8}</code> 1429 // * (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first 1430 // * ill-formed sub-segment and all following will be appended to 1431 // * the private use subtag. The first appended subtag will be 1432 // * "lvariant", followed by the sub-segments in order, separated by 1433 // * hyphen. For example, "x-lvariant-WIN", 1434 // * "Oracle-x-lvariant-JDK-Standard-Edition". 1435 // * 1436 // * <li>if any sub-segment does not match 1437 // * <code>[0-9a-zA-Z]{1,8}</code>, the variant will be truncated 1438 // * and the problematic sub-segment and all following sub-segments 1439 // * will be omitted. If the remainder is non-empty, it will be 1440 // * emitted as a private use subtag as above (even if the remainder 1441 // * turns out to be well-formed). For example, 1442 // * "Solaris_isjustthecoolestthing" is emitted as 1443 // * "x-lvariant-Solaris", not as "solaris".</li></ul> 1444 // * 1445 // * <p><b>Special Conversions:</b> Java supports some old locale 1446 // * representations, including deprecated ISO language codes, 1447 // * for compatibility. This method performs the following 1448 // * conversions: 1449 // * <ul> 1450 // * 1451 // * <li>Deprecated ISO language codes "iw", "ji", and "in" are 1452 // * converted to "he", "yi", and "id", respectively. 1453 // * 1454 // * <li>A locale with language "no", country "NO", and variant 1455 // * "NY", representing Norwegian Nynorsk (Norway), is converted 1456 // * to a language tag "nn-NO".</li></ul> 1457 // * 1458 // * <p><b>Note:</b> Although the language tag created by this 1459 // * method is well-formed (satisfies the syntax requirements 1460 // * defined by the IETF BCP 47 specification), it is not 1461 // * necessarily a valid BCP 47 language tag. For example, 1462 // * <pre> 1463 // * new Locale("xx", "YY").toLanguageTag();</pre> 1464 // * 1465 // * will return "xx-YY", but the language subtag "xx" and the 1466 // * region subtag "YY" are invalid because they are not registered 1467 // * in the IANA Language Subtag Registry. 1468 // * 1469 // * @return a BCP47 language tag representing the locale 1470 // * @see #forLanguageTag(string) 1471 // */ 1472 // string toLanguageTag() { 1473 // if (languageTag !is null) { 1474 // return languageTag; 1475 // } 1476 1477 // LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions); 1478 // StringBuilder buf = new StringBuilder(); 1479 1480 // string subtag = tag.getLanguage(); 1481 // if (subtag.length() > 0) { 1482 // buf.append(LanguageTag.canonicalizeLanguage(subtag)); 1483 // } 1484 1485 // subtag = tag.getScript(); 1486 // if (subtag.length() > 0) { 1487 // buf.append(LanguageTag.SEP); 1488 // buf.append(LanguageTag.canonicalizeScript(subtag)); 1489 // } 1490 1491 // subtag = tag.getRegion(); 1492 // if (subtag.length() > 0) { 1493 // buf.append(LanguageTag.SEP); 1494 // buf.append(LanguageTag.canonicalizeRegion(subtag)); 1495 // } 1496 1497 // List<string>subtags = tag.getVariants(); 1498 // for (string s : subtags) { 1499 // buf.append(LanguageTag.SEP); 1500 // // preserve casing 1501 // buf.append(s); 1502 // } 1503 1504 // subtags = tag.getExtensions(); 1505 // for (string s : subtags) { 1506 // buf.append(LanguageTag.SEP); 1507 // buf.append(LanguageTag.canonicalizeExtension(s)); 1508 // } 1509 1510 // subtag = tag.getPrivateuse(); 1511 // if (subtag.length() > 0) { 1512 // if (buf.length() > 0) { 1513 // buf.append(LanguageTag.SEP); 1514 // } 1515 // buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP); 1516 // // preserve casing 1517 // buf.append(subtag); 1518 // } 1519 1520 // string langTag = buf.toString(); 1521 // synchronized (this) { 1522 // if (languageTag is null) { 1523 // languageTag = langTag; 1524 // } 1525 // } 1526 // return languageTag; 1527 // } 1528 1529 // /** 1530 // * Returns a locale for the specified IETF BCP 47 language tag string. 1531 // * 1532 // * <p>If the specified language tag contains any ill-formed subtags, 1533 // * the first such subtag and all following subtags are ignored. Compare 1534 // * to {@link Locale.Builder#setLanguageTag} which throws an exception 1535 // * in this case. 1536 // * 1537 // * <p>The following <b>conversions</b> are performed:<ul> 1538 // * 1539 // * <li>The language code "und" is mapped to language "". 1540 // * 1541 // * <li>The language codes "he", "yi", and "id" are mapped to "iw", 1542 // * "ji", and "in" respectively. (This is the same canonicalization 1543 // * that's done in Locale's constructors.) 1544 // * 1545 // * <li>The portion of a private use subtag prefixed by "lvariant", 1546 // * if any, is removed and appended to the variant field in the 1547 // * result locale (without case normalization). If it is then 1548 // * empty, the private use subtag is discarded: 1549 // * 1550 // * <pre> 1551 // * Locale loc; 1552 // * loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX"); 1553 // * loc.getVariant(); // returns "POSIX" 1554 // * loc.getExtension('x'); // returns null 1555 // * 1556 // * loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def"); 1557 // * loc.getVariant(); // returns "POSIX_Abc_Def" 1558 // * loc.getExtension('x'); // returns "urp" 1559 // * </pre> 1560 // * 1561 // * <li>When the languageTag argument contains an extlang subtag, 1562 // * the first such subtag is used as the language, and the primary 1563 // * language subtag and other extlang subtags are ignored: 1564 // * 1565 // * <pre> 1566 // * Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao" 1567 // * Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US" 1568 // * </pre> 1569 // * 1570 // * <li>Case is normalized except for variant tags, which are left 1571 // * unchanged. Language is normalized to lower case, script to 1572 // * title case, country to upper case, and extensions to lower 1573 // * case. 1574 // * 1575 // * <li>If, after processing, the locale would exactly match either 1576 // * ja_JP_JP or th_TH_TH with no extensions, the appropriate 1577 // * extensions are added as though the constructor had been called: 1578 // * 1579 // * <pre> 1580 // * Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag(); 1581 // * // returns "ja-JP-u-ca-japanese-x-lvariant-JP" 1582 // * Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag(); 1583 // * // returns "th-TH-u-nu-thai-x-lvariant-TH" 1584 // * </pre></ul> 1585 // * 1586 // * <p>This implements the 'Language-Tag' production of BCP47, and 1587 // * so supports grandfathered (regular and irregular) as well as 1588 // * private use language tags. Stand alone private use tags are 1589 // * represented as empty language and extension 'x-whatever', 1590 // * and grandfathered tags are converted to their canonical replacements 1591 // * where they exist. 1592 // * 1593 // * <p>Grandfathered tags with canonical replacements are as follows: 1594 // * 1595 // * <table class="striped"> 1596 // * <caption style="display:none">Grandfathered tags with canonical replacements</caption> 1597 // * <thead style="text-align:center"> 1598 // * <tr><th scope="col" style="padding: 0 2px">grandfathered tag</th><th scope="col" style="padding: 0 2px">modern replacement</th></tr> 1599 // * </thead> 1600 // * <tbody style="text-align:center"> 1601 // * <tr><th scope="row">art-lojban</th><td>jbo</td></tr> 1602 // * <tr><th scope="row">i-ami</th><td>ami</td></tr> 1603 // * <tr><th scope="row">i-bnn</th><td>bnn</td></tr> 1604 // * <tr><th scope="row">i-hak</th><td>hak</td></tr> 1605 // * <tr><th scope="row">i-klingon</th><td>tlh</td></tr> 1606 // * <tr><th scope="row">i-lux</th><td>lb</td></tr> 1607 // * <tr><th scope="row">i-navajo</th><td>nv</td></tr> 1608 // * <tr><th scope="row">i-pwn</th><td>pwn</td></tr> 1609 // * <tr><th scope="row">i-tao</th><td>tao</td></tr> 1610 // * <tr><th scope="row">i-tay</th><td>tay</td></tr> 1611 // * <tr><th scope="row">i-tsu</th><td>tsu</td></tr> 1612 // * <tr><th scope="row">no-bok</th><td>nb</td></tr> 1613 // * <tr><th scope="row">no-nyn</th><td>nn</td></tr> 1614 // * <tr><th scope="row">sgn-BE-FR</th><td>sfb</td></tr> 1615 // * <tr><th scope="row">sgn-BE-NL</th><td>vgt</td></tr> 1616 // * <tr><th scope="row">sgn-CH-DE</th><td>sgg</td></tr> 1617 // * <tr><th scope="row">zh-guoyu</th><td>cmn</td></tr> 1618 // * <tr><th scope="row">zh-hakka</th><td>hak</td></tr> 1619 // * <tr><th scope="row">zh-min-nan</th><td>nan</td></tr> 1620 // * <tr><th scope="row">zh-xiang</th><td>hsn</td></tr> 1621 // * </tbody> 1622 // * </table> 1623 // * 1624 // * <p>Grandfathered tags with no modern replacement will be 1625 // * converted as follows: 1626 // * 1627 // * <table class="striped"> 1628 // * <caption style="display:none">Grandfathered tags with no modern replacement</caption> 1629 // * <thead style="text-align:center"> 1630 // * <tr><th scope="col" style="padding: 0 2px">grandfathered tag</th><th scope="col" style="padding: 0 2px">converts to</th></tr> 1631 // * </thead> 1632 // * <tbody style="text-align:center"> 1633 // * <tr><th scope="row">cel-gaulish</th><td>xtg-x-cel-gaulish</td></tr> 1634 // * <tr><th scope="row">en-GB-oed</th><td>en-GB-x-oed</td></tr> 1635 // * <tr><th scope="row">i-default</th><td>en-x-i-default</td></tr> 1636 // * <tr><th scope="row">i-enochian</th><td>und-x-i-enochian</td></tr> 1637 // * <tr><th scope="row">i-mingo</th><td>see-x-i-mingo</td></tr> 1638 // * <tr><th scope="row">zh-min</th><td>nan-x-zh-min</td></tr> 1639 // * </tbody> 1640 // * </table> 1641 // * 1642 // * <p>For a list of all grandfathered tags, see the 1643 // * IANA Language Subtag Registry (search for "Type: grandfathered"). 1644 // * 1645 // * <p><b>Note</b>: there is no guarantee that <code>toLanguageTag</code> 1646 // * and <code>forLanguageTag</code> will round-trip. 1647 // * 1648 // * @param languageTag the language tag 1649 // * @return The locale that best represents the language tag. 1650 // * @throws NullPointerException if <code>languageTag</code> is <code>null</code> 1651 // * @see #toLanguageTag() 1652 // * @see java.util.Locale.Builder#setLanguageTag(string) 1653 // */ 1654 // static Locale forLanguageTag(string languageTag) { 1655 // LanguageTag tag = LanguageTag.parse(languageTag, null); 1656 // InternalLocaleBuilder bldr = new InternalLocaleBuilder(); 1657 // bldr.setLanguageTag(tag); 1658 // BaseLocale base = bldr.getBaseLocale(); 1659 // LocaleExtensions exts = bldr.getLocaleExtensions(); 1660 // if (exts is null && base.getVariant().length() > 0) { 1661 // exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(), 1662 // base.getRegion(), base.getVariant()); 1663 // } 1664 // return getInstance(base, exts); 1665 // } 1666 1667 // /** 1668 // * Returns a three-letter abbreviation of this locale's language. 1669 // * If the language matches an ISO 639-1 two-letter code, the 1670 // * corresponding ISO 639-2/T three-letter lowercase code is 1671 // * returned. The ISO 639-2 language codes can be found on-line, 1672 // * see "Codes for the Representation of Names of Languages Part 2: 1673 // * Alpha-3 Code". If the locale specifies a three-letter 1674 // * language, the language is returned as is. If the locale does 1675 // * not specify a language the empty string is returned. 1676 // * 1677 // * @return A three-letter abbreviation of this locale's language. 1678 // * @exception MissingResourceException Throws MissingResourceException if 1679 // * three-letter language abbreviation is not available for this locale. 1680 // */ 1681 // string getISO3Language() throws MissingResourceException { 1682 // string lang = language; 1683 // if (lang.length() == 3) { 1684 // return lang; 1685 // } 1686 1687 // string language3 = getISO3Code(lang, LocaleISOData.isoLanguageTable); 1688 // if (language3 is null) { 1689 // throw new MissingResourceException("Couldn't find 3-letter language code for " 1690 // + lang, "FormatData_" ~ toString(), "ShortLanguage"); 1691 // } 1692 // return language3; 1693 // } 1694 1695 // /** 1696 // * Returns a three-letter abbreviation for this locale's country. 1697 // * If the country matches an ISO 3166-1 alpha-2 code, the 1698 // * corresponding ISO 3166-1 alpha-3 uppercase code is returned. 1699 // * If the locale doesn't specify a country, this will be the empty 1700 // * string. 1701 // * 1702 // * <p>The ISO 3166-1 codes can be found on-line. 1703 // * 1704 // * @return A three-letter abbreviation of this locale's country. 1705 // * @exception MissingResourceException Throws MissingResourceException if the 1706 // * three-letter country abbreviation is not available for this locale. 1707 // */ 1708 // string getISO3Country() throws MissingResourceException { 1709 // string country3 = getISO3Code(region, LocaleISOData.isoCountryTable); 1710 // if (country3 is null) { 1711 // throw new MissingResourceException("Couldn't find 3-letter country code for " 1712 // + region, "FormatData_" ~ toString(), "ShortCountry"); 1713 // } 1714 // return country3; 1715 // } 1716 1717 // private static string getISO3Code(string iso2Code, string table) { 1718 // int codeLength = iso2Code.length(); 1719 // if (codeLength == 0) { 1720 // return ""; 1721 // } 1722 1723 // int tableLength = table.length(); 1724 // int index = tableLength; 1725 // if (codeLength == 2) { 1726 // char c1 = iso2Code[0]; 1727 // char c2 = iso2Code[1]; 1728 // for (index = 0; index < tableLength; index += 5) { 1729 // if (table[index] == c1 1730 // && table[index + 1] == c2) { 1731 // break; 1732 // } 1733 // } 1734 // } 1735 // return index < tableLength ? table.substring(index + 2, index + 5) : null; 1736 // } 1737 1738 // /** 1739 // * Returns a name for the locale's language that is appropriate for display to the 1740 // * user. 1741 // * If possible, the name returned will be localized for the default 1742 // * {@link LocaleCategory#DISPLAY DISPLAY} locale. 1743 // * For example, if the locale is fr_FR and the default 1744 // * {@link LocaleCategory#DISPLAY DISPLAY} locale 1745 // * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and 1746 // * the default {@link LocaleCategory#DISPLAY DISPLAY} locale is fr_FR, 1747 // * getDisplayLanguage() will return "anglais". 1748 // * If the name returned cannot be localized for the default 1749 // * {@link LocaleCategory#DISPLAY DISPLAY} locale, 1750 // * (say, we don't have a Japanese name for Croatian), 1751 // * this function falls back on the English name, and uses the ISO code as a last-resort 1752 // * value. If the locale doesn't specify a language, this function returns the empty string. 1753 // * 1754 // * @return The name of the display language. 1755 // */ 1756 // final string getDisplayLanguage() { 1757 // return getDisplayLanguage(getDefault(Category.DISPLAY)); 1758 // } 1759 1760 // /** 1761 // * Returns a name for the locale's language that is appropriate for display to the 1762 // * user. 1763 // * If possible, the name returned will be localized according to inLocale. 1764 // * For example, if the locale is fr_FR and inLocale 1765 // * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and 1766 // * inLocale is fr_FR, getDisplayLanguage() will return "anglais". 1767 // * If the name returned cannot be localized according to inLocale, 1768 // * (say, we don't have a Japanese name for Croatian), 1769 // * this function falls back on the English name, and finally 1770 // * on the ISO code as a last-resort value. If the locale doesn't specify a language, 1771 // * this function returns the empty string. 1772 // * 1773 // * @param inLocale The locale for which to retrieve the display language. 1774 // * @return The name of the display language appropriate to the given locale. 1775 // * @exception NullPointerException if <code>inLocale</code> is <code>null</code> 1776 // */ 1777 // string getDisplayLanguage(Locale inLocale) { 1778 // return getDisplaystring(language, null, inLocale, DISPLAY_LANGUAGE); 1779 // } 1780 1781 // /** 1782 // * Returns a name for the locale's script that is appropriate for display to 1783 // * the user. If possible, the name will be localized for the default 1784 // * {@link LocaleCategory#DISPLAY DISPLAY} locale. Returns 1785 // * the empty string if this locale doesn't specify a script code. 1786 // * 1787 // * @return the display name of the script code for the current default 1788 // * {@link LocaleCategory#DISPLAY DISPLAY} locale 1789 // */ 1790 // string getDisplayScript() { 1791 // return getDisplayScript(getDefault(Category.DISPLAY)); 1792 // } 1793 1794 // /** 1795 // * Returns a name for the locale's script that is appropriate 1796 // * for display to the user. If possible, the name will be 1797 // * localized for the given locale. Returns the empty string if 1798 // * this locale doesn't specify a script code. 1799 // * 1800 // * @param inLocale The locale for which to retrieve the display script. 1801 // * @return the display name of the script code for the current default 1802 // * {@link LocaleCategory#DISPLAY DISPLAY} locale 1803 // * @throws NullPointerException if <code>inLocale</code> is <code>null</code> 1804 // */ 1805 // string getDisplayScript(Locale inLocale) { 1806 // return getDisplaystring(script, null, inLocale, DISPLAY_SCRIPT); 1807 // } 1808 1809 // /** 1810 // * Returns a name for the locale's country that is appropriate for display to the 1811 // * user. 1812 // * If possible, the name returned will be localized for the default 1813 // * {@link LocaleCategory#DISPLAY DISPLAY} locale. 1814 // * For example, if the locale is fr_FR and the default 1815 // * {@link LocaleCategory#DISPLAY DISPLAY} locale 1816 // * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and 1817 // * the default {@link LocaleCategory#DISPLAY DISPLAY} locale is fr_FR, 1818 // * getDisplayCountry() will return "Etats-Unis". 1819 // * If the name returned cannot be localized for the default 1820 // * {@link LocaleCategory#DISPLAY DISPLAY} locale, 1821 // * (say, we don't have a Japanese name for Croatia), 1822 // * this function falls back on the English name, and uses the ISO code as a last-resort 1823 // * value. If the locale doesn't specify a country, this function returns the empty string. 1824 // * 1825 // * @return The name of the country appropriate to the locale. 1826 // */ 1827 // final string getDisplayCountry() { 1828 // return getDisplayCountry(getDefault(Category.DISPLAY)); 1829 // } 1830 1831 // /** 1832 // * Returns a name for the locale's country that is appropriate for display to the 1833 // * user. 1834 // * If possible, the name returned will be localized according to inLocale. 1835 // * For example, if the locale is fr_FR and inLocale 1836 // * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and 1837 // * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis". 1838 // * If the name returned cannot be localized according to inLocale. 1839 // * (say, we don't have a Japanese name for Croatia), 1840 // * this function falls back on the English name, and finally 1841 // * on the ISO code as a last-resort value. If the locale doesn't specify a country, 1842 // * this function returns the empty string. 1843 // * 1844 // * @param inLocale The locale for which to retrieve the display country. 1845 // * @return The name of the country appropriate to the given locale. 1846 // * @exception NullPointerException if <code>inLocale</code> is <code>null</code> 1847 // */ 1848 // string getDisplayCountry(Locale inLocale) { 1849 // return getDisplaystring(region, null, inLocale, DISPLAY_COUNTRY); 1850 // } 1851 1852 // private string getDisplaystring(string code, string cat, Locale inLocale, int type) { 1853 // assert(inLocale); 1854 // assert(code); 1855 1856 // if (code.isEmpty()) { 1857 // return ""; 1858 // } 1859 1860 // LocaleServiceProviderPool pool = 1861 // LocaleServiceProviderPool.getPool(LocaleNameProvider.class); 1862 // string rbKey = (type == DISPLAY_VARIANT ? "%%"+code : code); 1863 // string result = pool.getLocalizedObject( 1864 // LocaleNameGetter.INSTANCE, 1865 // inLocale, rbKey, type, code, cat); 1866 // return result !is null ? result : code; 1867 // } 1868 1869 // /** 1870 // * Returns a name for the locale's variant code that is appropriate for display to the 1871 // * user. If possible, the name will be localized for the default 1872 // * {@link LocaleCategory#DISPLAY DISPLAY} locale. If the locale 1873 // * doesn't specify a variant code, this function returns the empty string. 1874 // * 1875 // * @return The name of the display variant code appropriate to the locale. 1876 // */ 1877 // final string getDisplayVariant() { 1878 // return getDisplayVariant(getDefault(Category.DISPLAY)); 1879 // } 1880 1881 // /** 1882 // * Returns a name for the locale's variant code that is appropriate for display to the 1883 // * user. If possible, the name will be localized for inLocale. If the locale 1884 // * doesn't specify a variant code, this function returns the empty string. 1885 // * 1886 // * @param inLocale The locale for which to retrieve the display variant code. 1887 // * @return The name of the display variant code appropriate to the given locale. 1888 // * @exception NullPointerException if <code>inLocale</code> is <code>null</code> 1889 // */ 1890 // string getDisplayVariant(Locale inLocale) { 1891 // if (variant.length() == 0) 1892 // return ""; 1893 1894 // LocaleResources lr = LocaleProviderAdapter 1895 // .getResourceBundleBased() 1896 // .getLocaleResources(inLocale); 1897 1898 // string names[] = getDisplayVariantArray(inLocale); 1899 1900 // // Get the localized patterns for formatting a list, and use 1901 // // them to format the list. 1902 // return formatList(names, 1903 // lr.getLocaleName("ListCompositionPattern")); 1904 // } 1905 1906 // /** 1907 // * Returns a name for the locale that is appropriate for display to the 1908 // * user. This will be the values returned by getDisplayLanguage(), 1909 // * getDisplayScript(), getDisplayCountry(), getDisplayVariant() and 1910 // * optional <a href="./Locale.html#def_locale_extension">Unicode extensions</a> 1911 // * assembled into a single string. The non-empty values are used in order, with 1912 // * the second and subsequent names in parentheses. For example: 1913 // * <blockquote> 1914 // * language (script, country, variant(, extension)*)<br> 1915 // * language (country(, extension)*)<br> 1916 // * language (variant(, extension)*)<br> 1917 // * script (country(, extension)*)<br> 1918 // * country (extension)*<br> 1919 // * </blockquote> 1920 // * depending on which fields are specified in the locale. The field 1921 // * separator in the above parentheses, denoted as a comma character, may 1922 // * be localized depending on the locale. If the language, script, country, 1923 // * and variant fields are all empty, this function returns the empty string. 1924 // * 1925 // * @return The name of the locale appropriate to display. 1926 // */ 1927 // final string getDisplayName() { 1928 // return getDisplayName(getDefault(Category.DISPLAY)); 1929 // } 1930 1931 // /** 1932 // * Returns a name for the locale that is appropriate for display 1933 // * to the user. This will be the values returned by 1934 // * getDisplayLanguage(), getDisplayScript(),getDisplayCountry() 1935 // * getDisplayVariant(), and optional <a href="./Locale.html#def_locale_extension"> 1936 // * Unicode extensions</a> assembled into a single string. The non-empty 1937 // * values are used in order, with the second and subsequent names in 1938 // * parentheses. For example: 1939 // * <blockquote> 1940 // * language (script, country, variant(, extension)*)<br> 1941 // * language (country(, extension)*)<br> 1942 // * language (variant(, extension)*)<br> 1943 // * script (country(, extension)*)<br> 1944 // * country (extension)*<br> 1945 // * </blockquote> 1946 // * depending on which fields are specified in the locale. The field 1947 // * separator in the above parentheses, denoted as a comma character, may 1948 // * be localized depending on the locale. If the language, script, country, 1949 // * and variant fields are all empty, this function returns the empty string. 1950 // * 1951 // * @param inLocale The locale for which to retrieve the display name. 1952 // * @return The name of the locale appropriate to display. 1953 // * @throws NullPointerException if <code>inLocale</code> is <code>null</code> 1954 // */ 1955 // string getDisplayName(Locale inLocale) { 1956 // LocaleResources lr = LocaleProviderAdapter 1957 // .getResourceBundleBased() 1958 // .getLocaleResources(inLocale); 1959 1960 // string languageName = getDisplayLanguage(inLocale); 1961 // string scriptName = getDisplayScript(inLocale); 1962 // string countryName = getDisplayCountry(inLocale); 1963 // string[] variantNames = getDisplayVariantArray(inLocale); 1964 1965 // // Get the localized patterns for formatting a display name. 1966 // string displayNamePattern = lr.getLocaleName("DisplayNamePattern"); 1967 // string listCompositionPattern = lr.getLocaleName("ListCompositionPattern"); 1968 1969 // // The display name consists of a main name, followed by qualifiers. 1970 // // Typically, the format is "MainName (Qualifier, Qualifier)" but this 1971 // // depends on what pattern is stored in the display locale. 1972 // string mainName = null; 1973 // string[] qualifierNames = null; 1974 1975 // // The main name is the language, or if there is no language, the script, 1976 // // then if no script, the country. If there is no language/script/country 1977 // // (an anomalous situation) then the display name is simply the variant's 1978 // // display name. 1979 // if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) { 1980 // if (variantNames.length == 0) { 1981 // return ""; 1982 // } else { 1983 // return formatList(variantNames, listCompositionPattern); 1984 // } 1985 // } 1986 // ArrayList<string> names = new ArrayList<>(4); 1987 // if (languageName.length() != 0) { 1988 // names.add(languageName); 1989 // } 1990 // if (scriptName.length() != 0) { 1991 // names.add(scriptName); 1992 // } 1993 // if (countryName.length() != 0) { 1994 // names.add(countryName); 1995 // } 1996 // if (variantNames.length != 0) { 1997 // names.addAll(Arrays.asList(variantNames)); 1998 // } 1999 2000 // // add Unicode extensions 2001 // if (localeExtensions !is null) { 2002 // localeExtensions.getUnicodeLocaleAttributes().stream() 2003 // .map(key -> getDisplaystring(key, null, inLocale, DISPLAY_UEXT_KEY)) 2004 // .forEach(names::add); 2005 // localeExtensions.getUnicodeLocaleKeys().stream() 2006 // .map(key -> getDisplayKeyTypeExtensionstring(key, lr, inLocale)) 2007 // .forEach(names::add); 2008 // } 2009 2010 // // The first one in the main name 2011 // mainName = names.get(0); 2012 2013 // // Others are qualifiers 2014 // int numNames = names.size(); 2015 // qualifierNames = (numNames > 1) ? 2016 // names.subList(1, numNames).toArray(new string[numNames - 1]) : new string[0]; 2017 2018 // // Create an array whose first element is the number of remaining 2019 // // elements. This serves as a selector into a ChoiceFormat pattern from 2020 // // the resource. The second and third elements are the main name and 2021 // // the qualifier; if there are no qualifiers, the third element is 2022 // // unused by the format pattern. 2023 // Object[] displayNames = { 2024 // qualifierNames.length != 0 ? 2 : 1, 2025 // mainName, 2026 // // We could also just call formatList() and have it handle the empty 2027 // // list case, but this is more efficient, and we want it to be 2028 // // efficient since all the language-only locales will not have any 2029 // // qualifiers. 2030 // qualifierNames.length != 0 ? formatList(qualifierNames, listCompositionPattern) : null 2031 // }; 2032 2033 // if (displayNamePattern !is null) { 2034 // return new MessageFormat(displayNamePattern).format(displayNames); 2035 // } 2036 // else { 2037 // // If we cannot get the message format pattern, then we use a simple 2038 // // hard-coded pattern. This should not occur in practice unless the 2039 // // installation is missing some core files (FormatData etc.). 2040 // StringBuilder result = new StringBuilder(); 2041 // result.append((string)displayNames[1]); 2042 // if (displayNames.length > 2) { 2043 // result.append(" ("); 2044 // result.append((string)displayNames[2]); 2045 // result.append(')'); 2046 // } 2047 // return result.toString(); 2048 // } 2049 // } 2050 2051 // /** 2052 // * Overrides Cloneable. 2053 // */ 2054 // override 2055 // Object clone() 2056 // { 2057 // try { 2058 // Locale that = (Locale)super.clone(); 2059 // return that; 2060 // } catch (CloneNotSupportedException e) { 2061 // throw new InternalError(e); 2062 // } 2063 // } 2064 2065 // /** 2066 // * Override hashCode. 2067 // * Since Locales are often used in hashtables, caches the value 2068 // * for speed. 2069 // */ 2070 // override 2071 // int hashCode() { 2072 // int hc = hashCodeValue; 2073 // if (hc == 0) { 2074 // hc = baseLocale.hashCode(); 2075 // if (localeExtensions !is null) { 2076 // hc ^= localeExtensions.hashCode(); 2077 // } 2078 // hashCodeValue = hc; 2079 // } 2080 // return hc; 2081 // } 2082 2083 // // Overrides 2084 2085 // /** 2086 // * Returns true if this Locale is equal to another object. A Locale is 2087 // * deemed equal to another Locale with identical language, script, country, 2088 // * variant and extensions, and unequal to all other objects. 2089 // * 2090 // * @return true if this Locale is equal to the specified object. 2091 // */ 2092 // override 2093 // bool equals(Object obj) { 2094 // if (this is obj) // quick check 2095 // return true; 2096 // if (!(obj instanceof Locale)) 2097 // return false; 2098 // BaseLocale otherBase = ((Locale)obj).baseLocale; 2099 // if (!baseLocale.equals(otherBase)) { 2100 // return false; 2101 // } 2102 // if (localeExtensions is null) { 2103 // return ((Locale)obj).localeExtensions is null; 2104 // } 2105 // return localeExtensions.equals(((Locale)obj).localeExtensions); 2106 // } 2107 2108 // // ================= privates ===================================== 2109 2110 // private BaseLocale baseLocale; 2111 // private LocaleExtensions localeExtensions; 2112 2113 /** 2114 * Calculated hashcode 2115 */ 2116 private int hashCodeValue; 2117 2118 // private __gshared Locale _defaultLocale; 2119 private __gshared Locale defaultDisplayLocale; 2120 private __gshared Locale defaultFormatLocale; 2121 2122 private string languageTag; 2123 2124 // /** 2125 // * Return an array of the display names of the variant. 2126 // * @param bundle the ResourceBundle to use to get the display names 2127 // * @return an array of display names, possible of zero length. 2128 // */ 2129 // private string[] getDisplayVariantArray(Locale inLocale) { 2130 // // Split the variant name into tokens separated by '_'. 2131 // stringTokenizer tokenizer = new stringTokenizer(variant, "_"); 2132 // string[] names = new string[tokenizer.countTokens()]; 2133 2134 // // For each variant token, lookup the display name. If 2135 // // not found, use the variant name itself. 2136 // for (int i=0; i<names.length; ++i) { 2137 // names[i] = getDisplaystring(tokenizer.nextToken(), null, 2138 // inLocale, DISPLAY_VARIANT); 2139 // } 2140 2141 // return names; 2142 // } 2143 2144 // private string getDisplayKeyTypeExtensionstring(string key, LocaleResources lr, Locale inLocale) { 2145 // string type = localeExtensions.getUnicodeLocaleType(key); 2146 // string ret = getDisplaystring(type, key, inLocale, DISPLAY_UEXT_TYPE); 2147 2148 // if (ret is null || ret.equals(type)) { 2149 // // no localization for this type. try combining key/type separately 2150 // string displayType = type; 2151 // switch (key) { 2152 // case "cu": 2153 // displayType = lr.getCurrencyName(type.toLowerCase(Locale.ROOT)); 2154 // break; 2155 // case "rg": 2156 // if (type !is null && 2157 // // UN M.49 code should not be allowed here 2158 // type.matches("^[a-zA-Z]{2}[zZ]{4}$")) { 2159 // displayType = lr.getLocaleName(type.substring(0, 2).toUpperCase(Locale.ROOT)); 2160 // } 2161 // break; 2162 // case "tz": 2163 // displayType = TimeZoneNameUtility.convertLDMLShortID(type) 2164 // .map(id -> TimeZoneNameUtility.retrieveGenericDisplayName(id, TimeZone.LONG, inLocale)) 2165 // .orElse(type); 2166 // break; 2167 // } 2168 // ret = MessageFormat.format(lr.getLocaleName("ListKeyTypePattern"), 2169 // getDisplaystring(key, null, inLocale, DISPLAY_UEXT_KEY), 2170 // Optional.ofNullable(displayType).orElse(type)); 2171 // } 2172 2173 // return ret; 2174 // } 2175 2176 // /** 2177 // * Format a list using given pattern strings. 2178 // * If either of the patterns is null, then a the list is 2179 // * formatted by concatenation with the delimiter ','. 2180 // * @param stringList the list of strings to be formatted. 2181 // * and formatting them into a list. 2182 // * @param pattern should take 2 arguments for reduction 2183 // * @return a string representing the list. 2184 // */ 2185 // private static string formatList(string[] stringList, string pattern) { 2186 // // If we have no list patterns, compose the list in a simple, 2187 // // non-localized way. 2188 // if (pattern is null) { 2189 // return Arrays.stream(stringList).collect(Collectors.joining(",")); 2190 // } 2191 2192 // switch (stringList.length) { 2193 // case 0: 2194 // return ""; 2195 // case 1: 2196 // return stringList[0]; 2197 // default: 2198 // return Arrays.stream(stringList).reduce("", 2199 // (s1, s2) -> { 2200 // if (s1.equals("")) { 2201 // return s2; 2202 // } 2203 // if (s2.equals("")) { 2204 // return s1; 2205 // } 2206 // return MessageFormat.format(pattern, s1, s2); 2207 // }); 2208 // } 2209 // } 2210 2211 // // Duplicate of sun.util.locale.UnicodeLocaleExtension.isKey in order to 2212 // // avoid its class loading. 2213 // private static bool isUnicodeExtensionKey(string s) { 2214 // // 2alphanum 2215 // return (s.length() == 2) && LocaleUtils.isAlphaNumericstring(s); 2216 // } 2217 2218 // /** 2219 // * @serialField language string 2220 // * language subtag in lower case. 2221 // * (See <a href="java.base/java/util/Locale.html#getLanguage()">getLanguage()</a>) 2222 // * @serialField country string 2223 // * country subtag in upper case. 2224 // * (See <a href="java.base/java/util/Locale.html#getCountry()">getCountry()</a>) 2225 // * @serialField variant string 2226 // * variant subtags separated by LOWLINE characters. 2227 // * (See <a href="java.base/java/util/Locale.html#getVariant()">getVariant()</a>) 2228 // * @serialField hashcode int 2229 // * deprecated, for forward compatibility only 2230 // * @serialField script string 2231 // * script subtag in title case 2232 // * (See <a href="java.base/java/util/Locale.html#getScript()">getScript()</a>) 2233 // * @serialField extensions string 2234 // * canonical representation of extensions, that is, 2235 // * BCP47 extensions in alphabetical order followed by 2236 // * BCP47 private use subtags, all in lower case letters 2237 // * separated by HYPHEN-MINUS characters. 2238 // * (See <a href="java.base/java/util/Locale.html#getExtensionKeys()">getExtensionKeys()</a>, 2239 // * <a href="java.base/java/util/Locale.html#getExtension(char)">getExtension(char)</a>) 2240 // */ 2241 // private static final ObjectStreamField[] serialPersistentFields = { 2242 // new ObjectStreamField("language", string.class), 2243 // new ObjectStreamField("country", string.class), 2244 // new ObjectStreamField("variant", string.class), 2245 // new ObjectStreamField("hashcode", int.class), 2246 // new ObjectStreamField("script", string.class), 2247 // new ObjectStreamField("extensions", string.class), 2248 // }; 2249 2250 // /** 2251 // * Serializes this <code>Locale</code> to the specified <code>ObjectOutputStream</code>. 2252 // * @param out the <code>ObjectOutputStream</code> to write 2253 // * @throws IOException 2254 // */ 2255 // private void writeObject(ObjectOutputStream out) throws IOException { 2256 // ObjectOutputStream.PutField fields = out.putFields(); 2257 // fields.put("language", language); 2258 // fields.put("script", script); 2259 // fields.put("country", region); 2260 // fields.put("variant", variant); 2261 // fields.put("extensions", localeExtensions is null ? "" : localeExtensions.getID()); 2262 // fields.put("hashcode", -1); // place holder just for backward support 2263 // out.writeFields(); 2264 // } 2265 2266 // /** 2267 // * Deserializes this <code>Locale</code>. 2268 // * @param in the <code>ObjectInputStream</code> to read 2269 // * @throws IOException 2270 // * @throws ClassNotFoundException 2271 // * @throws IllformedLocaleException 2272 // */ 2273 // private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 2274 // ObjectInputStream.GetField fields = in.readFields(); 2275 // string language = (string)fields.get("language", ""); 2276 // string script = (string)fields.get("script", ""); 2277 // string country = (string)fields.get("country", ""); 2278 // string variant = (string)fields.get("variant", ""); 2279 // string extStr = (string)fields.get("extensions", ""); 2280 // baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant); 2281 // if (extStr.length() > 0) { 2282 // try { 2283 // InternalLocaleBuilder bldr = new InternalLocaleBuilder(); 2284 // bldr.setExtensions(extStr); 2285 // localeExtensions = bldr.getLocaleExtensions(); 2286 // } catch (LocaleSyntaxException e) { 2287 // throw new IllformedLocaleException(e.getMessage()); 2288 // } 2289 // } else { 2290 // localeExtensions = null; 2291 // } 2292 // } 2293 2294 // /** 2295 // * Returns a cached <code>Locale</code> instance equivalent to 2296 // * the deserialized <code>Locale</code>. When serialized 2297 // * language, country and variant fields read from the object data stream 2298 // * are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions 2299 // * fields are empty, this method supplies <code>UNICODE_LOCALE_EXTENSION</code> 2300 // * "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script 2301 // * type is "thai"). See <a href="Locale.html#special_cases_constructor">Special Cases</a> 2302 // * for more information. 2303 // * 2304 // * @return an instance of <code>Locale</code> equivalent to 2305 // * the deserialized <code>Locale</code>. 2306 // * @throws java.io.ObjectStreamException 2307 // */ 2308 // private Object readResolve() throws java.io.ObjectStreamException { 2309 // return getInstance(language, script, 2310 // region, variant, localeExtensions); 2311 // } 2312 2313 // private static string[] isoLanguages; 2314 2315 // private static string[] isoCountries; 2316 2317 private static string convertOldISOCodes(string language) { 2318 // we accept both the old and the new ISO codes for the languages whose ISO 2319 // codes have changed, but we always store the OLD code, for backward compatibility 2320 language = language.toLower(); 2321 if (language == "he") { 2322 return "iw"; 2323 } else if (language == "yi") { 2324 return "ji"; 2325 } else if (language == "id") { 2326 return "in"; 2327 } else { 2328 return language; 2329 } 2330 } 2331 2332 // private static LocaleExtensions getCompatibilityExtensions(string language, 2333 // string script, 2334 // string country, 2335 // string variant) { 2336 // LocaleExtensions extensions = null; 2337 // // Special cases for backward compatibility support 2338 // if (LocaleUtils.caseIgnoreMatch(language, "ja") 2339 // && script.length() == 0 2340 // && LocaleUtils.caseIgnoreMatch(country, "jp") 2341 // && "JP".equals(variant)) { 2342 // // ja_JP_JP -> u-ca-japanese (calendar = japanese) 2343 // extensions = LocaleExtensions.CALENDAR_JAPANESE; 2344 // } else if (LocaleUtils.caseIgnoreMatch(language, "th") 2345 // && script.length() == 0 2346 // && LocaleUtils.caseIgnoreMatch(country, "th") 2347 // && "TH".equals(variant)) { 2348 // // th_TH_TH -> u-nu-thai (numbersystem = thai) 2349 // extensions = LocaleExtensions.NUMBER_THAI; 2350 // } 2351 // return extensions; 2352 // } 2353 2354 // /** 2355 // * Obtains a localized locale names from a LocaleNameProvider 2356 // * implementation. 2357 // */ 2358 // private static class LocaleNameGetter 2359 // implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, string> { 2360 // private static final LocaleNameGetter INSTANCE = new LocaleNameGetter(); 2361 2362 // override 2363 // string getObject(LocaleNameProvider localeNameProvider, 2364 // Locale locale, 2365 // string key, 2366 // Object... params) { 2367 // assert params.length == 3; 2368 // int type = (Integer)params[0]; 2369 // string code = (string)params[1]; 2370 // string cat = (string)params[2]; 2371 2372 // switch(type) { 2373 // case DISPLAY_LANGUAGE: 2374 // return localeNameProvider.getDisplayLanguage(code, locale); 2375 // case DISPLAY_COUNTRY: 2376 // return localeNameProvider.getDisplayCountry(code, locale); 2377 // case DISPLAY_VARIANT: 2378 // return localeNameProvider.getDisplayVariant(code, locale); 2379 // case DISPLAY_SCRIPT: 2380 // return localeNameProvider.getDisplayScript(code, locale); 2381 // case DISPLAY_UEXT_KEY: 2382 // return localeNameProvider.getDisplayUnicodeExtensionKey(code, locale); 2383 // case DISPLAY_UEXT_TYPE: 2384 // return localeNameProvider.getDisplayUnicodeExtensionType(code, cat, locale); 2385 // default: 2386 // assert false; // shouldn't happen 2387 // } 2388 2389 // return null; 2390 // } 2391 // } 2392 2393 2394 // /** 2395 // * <code>Builder</code> is used to build instances of <code>Locale</code> 2396 // * from values configured by the setters. Unlike the <code>Locale</code> 2397 // * constructors, the <code>Builder</code> checks if a value configured by a 2398 // * setter satisfies the syntax requirements defined by the <code>Locale</code> 2399 // * class. A <code>Locale</code> object created by a <code>Builder</code> is 2400 // * well-formed and can be transformed to a well-formed IETF BCP 47 language tag 2401 // * without losing information. 2402 // * 2403 // * <p><b>Note:</b> The <code>Locale</code> class does not provide any 2404 // * syntactic restrictions on variant, while BCP 47 requires each variant 2405 // * subtag to be 5 to 8 alphanumerics or a single numeric followed by 3 2406 // * alphanumerics. The method <code>setVariant</code> throws 2407 // * <code>IllformedLocaleException</code> for a variant that does not satisfy 2408 // * this restriction. If it is necessary to support such a variant, use a 2409 // * Locale constructor. However, keep in mind that a <code>Locale</code> 2410 // * object created this way might lose the variant information when 2411 // * transformed to a BCP 47 language tag. 2412 // * 2413 // * <p>The following example shows how to create a <code>Locale</code> object 2414 // * with the <code>Builder</code>. 2415 // * <blockquote> 2416 // * <pre> 2417 // * Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build(); 2418 // * </pre> 2419 // * </blockquote> 2420 // * 2421 // * <p>Builders can be reused; <code>clear()</code> resets all 2422 // * fields to their default values. 2423 // * 2424 // * @see Locale#forLanguageTag 2425 // */ 2426 // static final class Builder { 2427 // private final InternalLocaleBuilder localeBuilder; 2428 2429 // /** 2430 // * Constructs an empty Builder. The default value of all 2431 // * fields, extensions, and private use information is the 2432 // * empty string. 2433 // */ 2434 // Builder() { 2435 // localeBuilder = new InternalLocaleBuilder(); 2436 // } 2437 2438 // /** 2439 // * Resets the <code>Builder</code> to match the provided 2440 // * <code>locale</code>. Existing state is discarded. 2441 // * 2442 // * <p>All fields of the locale must be well-formed, see {@link Locale}. 2443 // * 2444 // * <p>Locales with any ill-formed fields cause 2445 // * <code>IllformedLocaleException</code> to be thrown, except for the 2446 // * following three cases which are accepted for compatibility 2447 // * reasons:<ul> 2448 // * <li>Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese" 2449 // * <li>Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai" 2450 // * <li>Locale("no", "NO", "NY") is treated as "nn-NO"</ul> 2451 // * 2452 // * @param locale the locale 2453 // * @return This builder. 2454 // * @throws IllformedLocaleException if <code>locale</code> has 2455 // * any ill-formed fields. 2456 // * @throws NullPointerException if <code>locale</code> is null. 2457 // */ 2458 // Builder setLocale(Locale locale) { 2459 // try { 2460 // localeBuilder.setLocale(locale.baseLocale, locale.localeExtensions); 2461 // } catch (LocaleSyntaxException e) { 2462 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2463 // } 2464 // return this; 2465 // } 2466 2467 // /** 2468 // * Resets the Builder to match the provided IETF BCP 47 2469 // * language tag. Discards the existing state. Null and the 2470 // * empty string cause the builder to be reset, like {@link 2471 // * #clear}. Grandfathered tags (see {@link 2472 // * Locale#forLanguageTag}) are converted to their canonical 2473 // * form before being processed. Otherwise, the language tag 2474 // * must be well-formed (see {@link Locale}) or an exception is 2475 // * thrown (unlike <code>Locale.forLanguageTag</code>, which 2476 // * just discards ill-formed and following portions of the 2477 // * tag). 2478 // * 2479 // * @param languageTag the language tag 2480 // * @return This builder. 2481 // * @throws IllformedLocaleException if <code>languageTag</code> is ill-formed 2482 // * @see Locale#forLanguageTag(string) 2483 // */ 2484 // Builder setLanguageTag(string languageTag) { 2485 // ParseStatus sts = new ParseStatus(); 2486 // LanguageTag tag = LanguageTag.parse(languageTag, sts); 2487 // if (sts.isError()) { 2488 // throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex()); 2489 // } 2490 // localeBuilder.setLanguageTag(tag); 2491 // return this; 2492 // } 2493 2494 // /** 2495 // * Sets the language. If <code>language</code> is the empty string or 2496 // * null, the language in this <code>Builder</code> is removed. Otherwise, 2497 // * the language must be <a href="./Locale.html#def_language">well-formed</a> 2498 // * or an exception is thrown. 2499 // * 2500 // * <p>The typical language value is a two or three-letter language 2501 // * code as defined in ISO639. 2502 // * 2503 // * @param language the language 2504 // * @return This builder. 2505 // * @throws IllformedLocaleException if <code>language</code> is ill-formed 2506 // */ 2507 // Builder setLanguage(string language) { 2508 // try { 2509 // localeBuilder.setLanguage(language); 2510 // } catch (LocaleSyntaxException e) { 2511 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2512 // } 2513 // return this; 2514 // } 2515 2516 // /** 2517 // * Sets the script. If <code>script</code> is null or the empty string, 2518 // * the script in this <code>Builder</code> is removed. 2519 // * Otherwise, the script must be <a href="./Locale.html#def_script">well-formed</a> or an 2520 // * exception is thrown. 2521 // * 2522 // * <p>The typical script value is a four-letter script code as defined by ISO 15924. 2523 // * 2524 // * @param script the script 2525 // * @return This builder. 2526 // * @throws IllformedLocaleException if <code>script</code> is ill-formed 2527 // */ 2528 // Builder setScript(string script) { 2529 // try { 2530 // localeBuilder.setScript(script); 2531 // } catch (LocaleSyntaxException e) { 2532 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2533 // } 2534 // return this; 2535 // } 2536 2537 // /** 2538 // * Sets the region. If region is null or the empty string, the region 2539 // * in this <code>Builder</code> is removed. Otherwise, 2540 // * the region must be <a href="./Locale.html#def_region">well-formed</a> or an 2541 // * exception is thrown. 2542 // * 2543 // * <p>The typical region value is a two-letter ISO 3166 code or a 2544 // * three-digit UN M.49 area code. 2545 // * 2546 // * <p>The country value in the <code>Locale</code> created by the 2547 // * <code>Builder</code> is always normalized to upper case. 2548 // * 2549 // * @param region the region 2550 // * @return This builder. 2551 // * @throws IllformedLocaleException if <code>region</code> is ill-formed 2552 // */ 2553 // Builder setRegion(string region) { 2554 // try { 2555 // localeBuilder.setRegion(region); 2556 // } catch (LocaleSyntaxException e) { 2557 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2558 // } 2559 // return this; 2560 // } 2561 2562 // /** 2563 // * Sets the variant. If variant is null or the empty string, the 2564 // * variant in this <code>Builder</code> is removed. Otherwise, it 2565 // * must consist of one or more <a href="./Locale.html#def_variant">well-formed</a> 2566 // * subtags, or an exception is thrown. 2567 // * 2568 // * <p><b>Note:</b> This method checks if <code>variant</code> 2569 // * satisfies the IETF BCP 47 variant subtag's syntax requirements, 2570 // * and normalizes the value to lowercase letters. However, 2571 // * the <code>Locale</code> class does not impose any syntactic 2572 // * restriction on variant, and the variant value in 2573 // * <code>Locale</code> is case sensitive. To set such a variant, 2574 // * use a Locale constructor. 2575 // * 2576 // * @param variant the variant 2577 // * @return This builder. 2578 // * @throws IllformedLocaleException if <code>variant</code> is ill-formed 2579 // */ 2580 // Builder setVariant(string variant) { 2581 // try { 2582 // localeBuilder.setVariant(variant); 2583 // } catch (LocaleSyntaxException e) { 2584 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2585 // } 2586 // return this; 2587 // } 2588 2589 // /** 2590 // * Sets the extension for the given key. If the value is null or the 2591 // * empty string, the extension is removed. Otherwise, the extension 2592 // * must be <a href="./Locale.html#def_extensions">well-formed</a> or an exception 2593 // * is thrown. 2594 // * 2595 // * <p><b>Note:</b> The key {@link Locale#UNICODE_LOCALE_EXTENSION 2596 // * UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension. 2597 // * Setting a value for this key replaces any existing Unicode locale key/type 2598 // * pairs with those defined in the extension. 2599 // * 2600 // * <p><b>Note:</b> The key {@link Locale#PRIVATE_USE_EXTENSION 2601 // * PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be 2602 // * well-formed, the value for this key needs only to have subtags of one to 2603 // * eight alphanumeric characters, not two to eight as in the general case. 2604 // * 2605 // * @param key the extension key 2606 // * @param value the extension value 2607 // * @return This builder. 2608 // * @throws IllformedLocaleException if <code>key</code> is illegal 2609 // * or <code>value</code> is ill-formed 2610 // * @see #setUnicodeLocaleKeyword(string, string) 2611 // */ 2612 // Builder setExtension(char key, string value) { 2613 // try { 2614 // localeBuilder.setExtension(key, value); 2615 // } catch (LocaleSyntaxException e) { 2616 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2617 // } 2618 // return this; 2619 // } 2620 2621 // /** 2622 // * Sets the Unicode locale keyword type for the given key. If the type 2623 // * is null, the Unicode keyword is removed. Otherwise, the key must be 2624 // * non-null and both key and type must be <a 2625 // * href="./Locale.html#def_locale_extension">well-formed</a> or an exception 2626 // * is thrown. 2627 // * 2628 // * <p>Keys and types are converted to lower case. 2629 // * 2630 // * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension} 2631 // * replaces all Unicode locale keywords with those defined in the 2632 // * extension. 2633 // * 2634 // * @param key the Unicode locale key 2635 // * @param type the Unicode locale type 2636 // * @return This builder. 2637 // * @throws IllformedLocaleException if <code>key</code> or <code>type</code> 2638 // * is ill-formed 2639 // * @throws NullPointerException if <code>key</code> is null 2640 // * @see #setExtension(char, string) 2641 // */ 2642 // Builder setUnicodeLocaleKeyword(string key, string type) { 2643 // try { 2644 // localeBuilder.setUnicodeLocaleKeyword(key, type); 2645 // } catch (LocaleSyntaxException e) { 2646 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2647 // } 2648 // return this; 2649 // } 2650 2651 // /** 2652 // * Adds a unicode locale attribute, if not already present, otherwise 2653 // * has no effect. The attribute must not be null and must be <a 2654 // * href="./Locale.html#def_locale_extension">well-formed</a> or an exception 2655 // * is thrown. 2656 // * 2657 // * @param attribute the attribute 2658 // * @return This builder. 2659 // * @throws NullPointerException if <code>attribute</code> is null 2660 // * @throws IllformedLocaleException if <code>attribute</code> is ill-formed 2661 // * @see #setExtension(char, string) 2662 // */ 2663 // Builder addUnicodeLocaleAttribute(string attribute) { 2664 // try { 2665 // localeBuilder.addUnicodeLocaleAttribute(attribute); 2666 // } catch (LocaleSyntaxException e) { 2667 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2668 // } 2669 // return this; 2670 // } 2671 2672 // /** 2673 // * Removes a unicode locale attribute, if present, otherwise has no 2674 // * effect. The attribute must not be null and must be <a 2675 // * href="./Locale.html#def_locale_extension">well-formed</a> or an exception 2676 // * is thrown. 2677 // * 2678 // * <p>Attribute comparison for removal is case-insensitive. 2679 // * 2680 // * @param attribute the attribute 2681 // * @return This builder. 2682 // * @throws NullPointerException if <code>attribute</code> is null 2683 // * @throws IllformedLocaleException if <code>attribute</code> is ill-formed 2684 // * @see #setExtension(char, string) 2685 // */ 2686 // Builder removeUnicodeLocaleAttribute(string attribute) { 2687 // assert(attribute); 2688 // try { 2689 // localeBuilder.removeUnicodeLocaleAttribute(attribute); 2690 // } catch (LocaleSyntaxException e) { 2691 // throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); 2692 // } 2693 // return this; 2694 // } 2695 2696 // /** 2697 // * Resets the builder to its initial, empty state. 2698 // * 2699 // * @return This builder. 2700 // */ 2701 // Builder clear() { 2702 // localeBuilder.clear(); 2703 // return this; 2704 // } 2705 2706 // /** 2707 // * Resets the extensions to their initial, empty state. 2708 // * Language, script, region and variant are unchanged. 2709 // * 2710 // * @return This builder. 2711 // * @see #setExtension(char, string) 2712 // */ 2713 // Builder clearExtensions() { 2714 // localeBuilder.clearExtensions(); 2715 // return this; 2716 // } 2717 2718 // /** 2719 // * Returns an instance of <code>Locale</code> created from the fields set 2720 // * on this builder. 2721 // * 2722 // * <p>This applies the conversions listed in {@link Locale#forLanguageTag} 2723 // * when constructing a Locale. (Grandfathered tags are handled in 2724 // * {@link #setLanguageTag}.) 2725 // * 2726 // * @return A Locale. 2727 // */ 2728 // Locale build() { 2729 // BaseLocale baseloc = localeBuilder.getBaseLocale(); 2730 // LocaleExtensions extensions = localeBuilder.getLocaleExtensions(); 2731 // if (extensions is null && baseloc.getVariant().length() > 0) { 2732 // extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(), 2733 // baseloc.getRegion(), baseloc.getVariant()); 2734 // } 2735 // return Locale.getInstance(baseloc, extensions); 2736 // } 2737 // } 2738 2739 // /** 2740 // * This enum provides constants to select a filtering mode for locale 2741 // * matching. Refer to <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 2742 // * Matching of Language Tags</a> for details. 2743 // * 2744 // * <p>As an example, think of two Language Priority Lists each of which 2745 // * includes only one language range and a set of following language tags: 2746 // * 2747 // * <pre> 2748 // * de (German) 2749 // * de-DE (German, Germany) 2750 // * de-Deva (German, in Devanagari script) 2751 // * de-Deva-DE (German, in Devanagari script, Germany) 2752 // * de-DE-1996 (German, Germany, orthography of 1996) 2753 // * de-Latn-DE (German, in Latin script, Germany) 2754 // * de-Latn-DE-1996 (German, in Latin script, Germany, orthography of 1996) 2755 // * </pre> 2756 // * 2757 // * The filtering method will behave as follows: 2758 // * 2759 // * <table class="striped"> 2760 // * <caption>Filtering method behavior</caption> 2761 // * <thead> 2762 // * <tr> 2763 // * <th scope="col">Filtering Mode</th> 2764 // * <th scope="col">Language Priority List: {@code "de-DE"}</th> 2765 // * <th scope="col">Language Priority List: {@code "de-*-DE"}</th> 2766 // * </tr> 2767 // * </thead> 2768 // * <tbody> 2769 // * <tr> 2770 // * <th scope="row" style="vertical-align:top"> 2771 // * {@link FilteringMode#AUTOSELECT_FILTERING AUTOSELECT_FILTERING} 2772 // * </th> 2773 // * <td style="vertical-align:top"> 2774 // * Performs <em>basic</em> filtering and returns {@code "de-DE"} and 2775 // * {@code "de-DE-1996"}. 2776 // * </td> 2777 // * <td style="vertical-align:top"> 2778 // * Performs <em>extended</em> filtering and returns {@code "de-DE"}, 2779 // * {@code "de-Deva-DE"}, {@code "de-DE-1996"}, {@code "de-Latn-DE"}, and 2780 // * {@code "de-Latn-DE-1996"}. 2781 // * </td> 2782 // * </tr> 2783 // * <tr> 2784 // * <th scope="row" style="vertical-align:top"> 2785 // * {@link FilteringMode#EXTENDED_FILTERING EXTENDED_FILTERING} 2786 // * </th> 2787 // * <td style="vertical-align:top"> 2788 // * Performs <em>extended</em> filtering and returns {@code "de-DE"}, 2789 // * {@code "de-Deva-DE"}, {@code "de-DE-1996"}, {@code "de-Latn-DE"}, and 2790 // * {@code "de-Latn-DE-1996"}. 2791 // * </td> 2792 // * <td style="vertical-align:top">Same as above.</td> 2793 // * </tr> 2794 // * <tr> 2795 // * <th scope="row" style="vertical-align:top"> 2796 // * {@link FilteringMode#IGNORE_EXTENDED_RANGES IGNORE_EXTENDED_RANGES} 2797 // * </th> 2798 // * <td style="vertical-align:top"> 2799 // * Performs <em>basic</em> filtering and returns {@code "de-DE"} and 2800 // * {@code "de-DE-1996"}. 2801 // * </td> 2802 // * <td style="vertical-align:top"> 2803 // * Performs <em>basic</em> filtering and returns {@code null} because 2804 // * nothing matches. 2805 // * </td> 2806 // * </tr> 2807 // * <tr> 2808 // * <th scope="row" style="vertical-align:top"> 2809 // * {@link FilteringMode#MAP_EXTENDED_RANGES MAP_EXTENDED_RANGES} 2810 // * </th> 2811 // * <td style="vertical-align:top">Same as above.</td> 2812 // * <td style="vertical-align:top"> 2813 // * Performs <em>basic</em> filtering and returns {@code "de-DE"} and 2814 // * {@code "de-DE-1996"} because {@code "de-*-DE"} is mapped to 2815 // * {@code "de-DE"}. 2816 // * </td> 2817 // * </tr> 2818 // * <tr> 2819 // * <th scope="row" style="vertical-align:top"> 2820 // * {@link FilteringMode#REJECT_EXTENDED_RANGES REJECT_EXTENDED_RANGES} 2821 // * </th> 2822 // * <td style="vertical-align:top">Same as above.</td> 2823 // * <td style="vertical-align:top"> 2824 // * Throws {@link IllegalArgumentException} because {@code "de-*-DE"} is 2825 // * not a valid basic language range. 2826 // * </td> 2827 // * </tr> 2828 // * </tbody> 2829 // * </table> 2830 // * 2831 // * @see #filter(List, Collection, FilteringMode) 2832 // * @see #filterTags(List, Collection, FilteringMode) 2833 // * 2834 // */ 2835 // static enum FilteringMode { 2836 // /** 2837 // * Specifies automatic filtering mode based on the given Language 2838 // * Priority List consisting of language ranges. If all of the ranges 2839 // * are basic, basic filtering is selected. Otherwise, extended 2840 // * filtering is selected. 2841 // */ 2842 // AUTOSELECT_FILTERING, 2843 2844 // /** 2845 // * Specifies extended filtering. 2846 // */ 2847 // EXTENDED_FILTERING, 2848 2849 // /** 2850 // * Specifies basic filtering: Note that any extended language ranges 2851 // * included in the given Language Priority List are ignored. 2852 // */ 2853 // IGNORE_EXTENDED_RANGES, 2854 2855 // /** 2856 // * Specifies basic filtering: If any extended language ranges are 2857 // * included in the given Language Priority List, they are mapped to the 2858 // * basic language range. Specifically, a language range starting with a 2859 // * subtag {@code "*"} is treated as a language range {@code "*"}. For 2860 // * example, {@code "*-US"} is treated as {@code "*"}. If {@code "*"} is 2861 // * not the first subtag, {@code "*"} and extra {@code "-"} are removed. 2862 // * For example, {@code "ja-*-JP"} is mapped to {@code "ja-JP"}. 2863 // */ 2864 // MAP_EXTENDED_RANGES, 2865 2866 // /** 2867 // * Specifies basic filtering: If any extended language ranges are 2868 // * included in the given Language Priority List, the list is rejected 2869 // * and the filtering method throws {@link IllegalArgumentException}. 2870 // */ 2871 // REJECT_EXTENDED_RANGES 2872 // }; 2873 2874 // /** 2875 // * This class expresses a <em>Language Range</em> defined in 2876 // * <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 Matching of 2877 // * Language Tags</a>. A language range is an identifier which is used to 2878 // * select language tag(s) meeting specific requirements by using the 2879 // * mechanisms described in <a href="Locale.html#LocaleMatching">Locale 2880 // * Matching</a>. A list which represents a user's preferences and consists 2881 // * of language ranges is called a <em>Language Priority List</em>. 2882 // * 2883 // * <p>There are two types of language ranges: basic and extended. In RFC 2884 // * 4647, the syntax of language ranges is expressed in 2885 // * <a href="http://tools.ietf.org/html/rfc4234">ABNF</a> as follows: 2886 // * <blockquote> 2887 // * <pre> 2888 // * basic-language-range = (1*8ALPHA *("-" 1*8alphanum)) / "*" 2889 // * extended-language-range = (1*8ALPHA / "*") 2890 // * *("-" (1*8alphanum / "*")) 2891 // * alphanum = ALPHA / DIGIT 2892 // * </pre> 2893 // * </blockquote> 2894 // * For example, {@code "en"} (English), {@code "ja-JP"} (Japanese, Japan), 2895 // * {@code "*"} (special language range which matches any language tag) are 2896 // * basic language ranges, whereas {@code "*-CH"} (any languages, 2897 // * Switzerland), {@code "es-*"} (Spanish, any regions), and 2898 // * {@code "zh-Hant-*"} (Traditional Chinese, any regions) are extended 2899 // * language ranges. 2900 // * 2901 // * @see #filter 2902 // * @see #filterTags 2903 // * @see #lookup 2904 // * @see #lookupTag 2905 // * 2906 // */ 2907 // static final class LanguageRange { 2908 2909 // /** 2910 // * A constant holding the maximum value of weight, 1.0, which indicates 2911 // * that the language range is a good fit for the user. 2912 // */ 2913 // static final double MAX_WEIGHT = 1.0; 2914 2915 // /** 2916 // * A constant holding the minimum value of weight, 0.0, which indicates 2917 // * that the language range is not a good fit for the user. 2918 // */ 2919 // static final double MIN_WEIGHT = 0.0; 2920 2921 // private final string range; 2922 // private final double weight; 2923 2924 // private int hash; 2925 2926 // /** 2927 // * Constructs a {@code LanguageRange} using the given {@code range}. 2928 // * Note that no validation is done against the IANA Language Subtag 2929 // * Registry at time of construction. 2930 // * 2931 // * <p>This is equivalent to {@code LanguageRange(range, MAX_WEIGHT)}. 2932 // * 2933 // * @param range a language range 2934 // * @throws NullPointerException if the given {@code range} is 2935 // * {@code null} 2936 // * @throws IllegalArgumentException if the given {@code range} does not 2937 // * comply with the syntax of the language range mentioned in RFC 4647 2938 // */ 2939 // LanguageRange(string range) { 2940 // this(range, MAX_WEIGHT); 2941 // } 2942 2943 // /** 2944 // * Constructs a {@code LanguageRange} using the given {@code range} and 2945 // * {@code weight}. Note that no validation is done against the IANA 2946 // * Language Subtag Registry at time of construction. 2947 // * 2948 // * @param range a language range 2949 // * @param weight a weight value between {@code MIN_WEIGHT} and 2950 // * {@code MAX_WEIGHT} 2951 // * @throws NullPointerException if the given {@code range} is 2952 // * {@code null} 2953 // * @throws IllegalArgumentException if the given {@code range} does not 2954 // * comply with the syntax of the language range mentioned in RFC 4647 2955 // * or if the given {@code weight} is less than {@code MIN_WEIGHT} 2956 // * or greater than {@code MAX_WEIGHT} 2957 // */ 2958 // LanguageRange(string range, double weight) { 2959 // if (range is null) { 2960 // throw new NullPointerException(); 2961 // } 2962 // if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) { 2963 // throw new IllegalArgumentException("weight=" ~ weight); 2964 // } 2965 2966 // range = range.toLowerCase(Locale.ROOT); 2967 2968 // // Do syntax check. 2969 // bool isIllFormed = false; 2970 // string[] subtags = range.split("-"); 2971 // if (isSubtagIllFormed(subtags[0], true) 2972 // || range.endsWith("-")) { 2973 // isIllFormed = true; 2974 // } else { 2975 // for (int i = 1; i < subtags.length; i++) { 2976 // if (isSubtagIllFormed(subtags[i], false)) { 2977 // isIllFormed = true; 2978 // break; 2979 // } 2980 // } 2981 // } 2982 // if (isIllFormed) { 2983 // throw new IllegalArgumentException("range=" ~ range); 2984 // } 2985 2986 // this.range = range; 2987 // this.weight = weight; 2988 // } 2989 2990 // private static bool isSubtagIllFormed(string subtag, 2991 // bool isFirstSubtag) { 2992 // if (subtag.equals("") || subtag.length() > 8) { 2993 // return true; 2994 // } else if (subtag.equals("*")) { 2995 // return false; 2996 // } 2997 // char[] charArray = subtag.toCharArray(); 2998 // if (isFirstSubtag) { // ALPHA 2999 // for (char c : charArray) { 3000 // if (c < 'a' || c > 'z') { 3001 // return true; 3002 // } 3003 // } 3004 // } else { // ALPHA / DIGIT 3005 // for (char c : charArray) { 3006 // if (c < '0' || (c > '9' && c < 'a') || c > 'z') { 3007 // return true; 3008 // } 3009 // } 3010 // } 3011 // return false; 3012 // } 3013 3014 // /** 3015 // * Returns the language range of this {@code LanguageRange}. 3016 // * 3017 // * @return the language range. 3018 // */ 3019 // string getRange() { 3020 // return range; 3021 // } 3022 3023 // /** 3024 // * Returns the weight of this {@code LanguageRange}. 3025 // * 3026 // * @return the weight value. 3027 // */ 3028 // double getWeight() { 3029 // return weight; 3030 // } 3031 3032 // /** 3033 // * Parses the given {@code ranges} to generate a Language Priority List. 3034 // * 3035 // * <p>This method performs a syntactic check for each language range in 3036 // * the given {@code ranges} but doesn't do validation using the IANA 3037 // * Language Subtag Registry. 3038 // * 3039 // * <p>The {@code ranges} to be given can take one of the following 3040 // * forms: 3041 // * 3042 // * <pre> 3043 // * "Accept-Language: ja,en;q=0.4" (weighted list with Accept-Language prefix) 3044 // * "ja,en;q=0.4" (weighted list) 3045 // * "ja,en" (prioritized list) 3046 // * </pre> 3047 // * 3048 // * In a weighted list, each language range is given a weight value. 3049 // * The weight value is identical to the "quality value" in 3050 // * <a href="http://tools.ietf.org/html/rfc2616">RFC 2616</a>, and it 3051 // * expresses how much the user prefers the language. A weight value is 3052 // * specified after a corresponding language range followed by 3053 // * {@code ";q="}, and the default weight value is {@code MAX_WEIGHT} 3054 // * when it is omitted. 3055 // * 3056 // * <p>Unlike a weighted list, language ranges in a prioritized list 3057 // * are sorted in the descending order based on its priority. The first 3058 // * language range has the highest priority and meets the user's 3059 // * preference most. 3060 // * 3061 // * <p>In either case, language ranges are sorted in descending order in 3062 // * the Language Priority List based on priority or weight. If a 3063 // * language range appears in the given {@code ranges} more than once, 3064 // * only the first one is included on the Language Priority List. 3065 // * 3066 // * <p>The returned list consists of language ranges from the given 3067 // * {@code ranges} and their equivalents found in the IANA Language 3068 // * Subtag Registry. For example, if the given {@code ranges} is 3069 // * {@code "Accept-Language: iw,en-us;q=0.7,en;q=0.3"}, the elements in 3070 // * the list to be returned are: 3071 // * 3072 // * <pre> 3073 // * <b>Range</b> <b>Weight</b> 3074 // * "iw" (older tag for Hebrew) 1.0 3075 // * "he" (new preferred code for Hebrew) 1.0 3076 // * "en-us" (English, United States) 0.7 3077 // * "en" (English) 0.3 3078 // * </pre> 3079 // * 3080 // * Two language ranges, {@code "iw"} and {@code "he"}, have the same 3081 // * highest priority in the list. By adding {@code "he"} to the user's 3082 // * Language Priority List, locale-matching method can find Hebrew as a 3083 // * matching locale (or language tag) even if the application or system 3084 // * offers only {@code "he"} as a supported locale (or language tag). 3085 // * 3086 // * @param ranges a list of comma-separated language ranges or a list of 3087 // * language ranges in the form of the "Accept-Language" header 3088 // * defined in <a href="http://tools.ietf.org/html/rfc2616">RFC 3089 // * 2616</a> 3090 // * @return a Language Priority List consisting of language ranges 3091 // * included in the given {@code ranges} and their equivalent 3092 // * language ranges if available. The list is modifiable. 3093 // * @throws NullPointerException if {@code ranges} is null 3094 // * @throws IllegalArgumentException if a language range or a weight 3095 // * found in the given {@code ranges} is ill-formed 3096 // */ 3097 // static List<LanguageRange> parse(string ranges) { 3098 // return LocaleMatcher.parse(ranges); 3099 // } 3100 3101 // /** 3102 // * Parses the given {@code ranges} to generate a Language Priority 3103 // * List, and then customizes the list using the given {@code map}. 3104 // * This method is equivalent to 3105 // * {@code mapEquivalents(parse(ranges), map)}. 3106 // * 3107 // * @param ranges a list of comma-separated language ranges or a list 3108 // * of language ranges in the form of the "Accept-Language" header 3109 // * defined in <a href="http://tools.ietf.org/html/rfc2616">RFC 3110 // * 2616</a> 3111 // * @param map a map containing information to customize language ranges 3112 // * @return a Language Priority List with customization. The list is 3113 // * modifiable. 3114 // * @throws NullPointerException if {@code ranges} is null 3115 // * @throws IllegalArgumentException if a language range or a weight 3116 // * found in the given {@code ranges} is ill-formed 3117 // * @see #parse(string) 3118 // * @see #mapEquivalents 3119 // */ 3120 // static List<LanguageRange> parse(string ranges, 3121 // Map<string, List<string>> map) { 3122 // return mapEquivalents(parse(ranges), map); 3123 // } 3124 3125 // /** 3126 // * Generates a new customized Language Priority List using the given 3127 // * {@code priorityList} and {@code map}. If the given {@code map} is 3128 // * empty, this method returns a copy of the given {@code priorityList}. 3129 // * 3130 // * <p>In the map, a key represents a language range whereas a value is 3131 // * a list of equivalents of it. {@code '*'} cannot be used in the map. 3132 // * Each equivalent language range has the same weight value as its 3133 // * original language range. 3134 // * 3135 // * <pre> 3136 // * An example of map: 3137 // * <b>Key</b> <b>Value</b> 3138 // * "zh" (Chinese) "zh", 3139 // * "zh-Hans"(Simplified Chinese) 3140 // * "zh-HK" (Chinese, Hong Kong) "zh-HK" 3141 // * "zh-TW" (Chinese, Taiwan) "zh-TW" 3142 // * </pre> 3143 // * 3144 // * The customization is performed after modification using the IANA 3145 // * Language Subtag Registry. 3146 // * 3147 // * <p>For example, if a user's Language Priority List consists of five 3148 // * language ranges ({@code "zh"}, {@code "zh-CN"}, {@code "en"}, 3149 // * {@code "zh-TW"}, and {@code "zh-HK"}), the newly generated Language 3150 // * Priority List which is customized using the above map example will 3151 // * consists of {@code "zh"}, {@code "zh-Hans"}, {@code "zh-CN"}, 3152 // * {@code "zh-Hans-CN"}, {@code "en"}, {@code "zh-TW"}, and 3153 // * {@code "zh-HK"}. 3154 // * 3155 // * <p>{@code "zh-HK"} and {@code "zh-TW"} aren't converted to 3156 // * {@code "zh-Hans-HK"} nor {@code "zh-Hans-TW"} even if they are 3157 // * included in the Language Priority List. In this example, mapping 3158 // * is used to clearly distinguish Simplified Chinese and Traditional 3159 // * Chinese. 3160 // * 3161 // * <p>If the {@code "zh"}-to-{@code "zh"} mapping isn't included in the 3162 // * map, a simple replacement will be performed and the customized list 3163 // * won't include {@code "zh"} and {@code "zh-CN"}. 3164 // * 3165 // * @param priorityList user's Language Priority List 3166 // * @param map a map containing information to customize language ranges 3167 // * @return a new Language Priority List with customization. The list is 3168 // * modifiable. 3169 // * @throws NullPointerException if {@code priorityList} is {@code null} 3170 // * @see #parse(string, Map) 3171 // */ 3172 // static List<LanguageRange> mapEquivalents( 3173 // List<LanguageRange>priorityList, 3174 // Map<string, List<string>> map) { 3175 // return LocaleMatcher.mapEquivalents(priorityList, map); 3176 // } 3177 3178 // /** 3179 // * Returns a hash code value for the object. 3180 // * 3181 // * @return a hash code value for this object. 3182 // */ 3183 // override 3184 // int hashCode() { 3185 // int h = hash; 3186 // if (h == 0) { 3187 // h = 17; 3188 // h = 37*h + range.hashCode(); 3189 // long bitsWeight = Double.doubleToLongBits(weight); 3190 // h = 37*h + (int)(bitsWeight ^ (bitsWeight >>> 32)); 3191 // if (h != 0) { 3192 // hash = h; 3193 // } 3194 // } 3195 // return h; 3196 // } 3197 3198 // /** 3199 // * Compares this object to the specified object. The result is true if 3200 // * and only if the argument is not {@code null} and is a 3201 // * {@code LanguageRange} object that contains the same {@code range} 3202 // * and {@code weight} values as this object. 3203 // * 3204 // * @param obj the object to compare with 3205 // * @return {@code true} if this object's {@code range} and 3206 // * {@code weight} are the same as the {@code obj}'s; {@code false} 3207 // * otherwise. 3208 // */ 3209 // override 3210 // bool equals(Object obj) { 3211 // if (this is obj) { 3212 // return true; 3213 // } 3214 // if (!(obj instanceof LanguageRange)) { 3215 // return false; 3216 // } 3217 // LanguageRange other = (LanguageRange)obj; 3218 // return hash == other.hash 3219 // && range.equals(other.range) 3220 // && weight == other.weight; 3221 // } 3222 3223 // /** 3224 // * Returns an informative string representation of this {@code LanguageRange} 3225 // * object, consisting of language range and weight if the range is 3226 // * weighted and the weight is less than the max weight. 3227 // * 3228 // * @return a string representation of this {@code LanguageRange} object. 3229 // */ 3230 // override 3231 // string toString() { 3232 // return (weight == MAX_WEIGHT) ? range : range ~ ";q=" ~ weight; 3233 // } 3234 // } 3235 3236 // /** 3237 // * Returns a list of matching {@code Locale} instances using the filtering 3238 // * mechanism defined in RFC 4647. 3239 // * 3240 // * This filter operation on the given {@code locales} ensures that only 3241 // * unique matching locale(s) are returned. 3242 // * 3243 // * @param priorityList user's Language Priority List in which each language 3244 // * tag is sorted in descending order based on priority or weight 3245 // * @param locales {@code Locale} instances used for matching 3246 // * @param mode filtering mode 3247 // * @return a list of {@code Locale} instances for matching language tags 3248 // * sorted in descending order based on priority or weight, or an empty 3249 // * list if nothing matches. The list is modifiable. 3250 // * @throws NullPointerException if {@code priorityList} or {@code locales} 3251 // * is {@code null} 3252 // * @throws IllegalArgumentException if one or more extended language ranges 3253 // * are included in the given list when 3254 // * {@link FilteringMode#REJECT_EXTENDED_RANGES} is specified 3255 // * 3256 // */ 3257 // static List<Locale> filter(List<LanguageRange> priorityList, 3258 // Collection<Locale> locales, 3259 // FilteringMode mode) { 3260 // return LocaleMatcher.filter(priorityList, locales, mode); 3261 // } 3262 3263 // /** 3264 // * Returns a list of matching {@code Locale} instances using the filtering 3265 // * mechanism defined in RFC 4647. This is equivalent to 3266 // * {@link #filter(List, Collection, FilteringMode)} when {@code mode} is 3267 // * {@link FilteringMode#AUTOSELECT_FILTERING}. 3268 // * 3269 // * This filter operation on the given {@code locales} ensures that only 3270 // * unique matching locale(s) are returned. 3271 // * 3272 // * @param priorityList user's Language Priority List in which each language 3273 // * tag is sorted in descending order based on priority or weight 3274 // * @param locales {@code Locale} instances used for matching 3275 // * @return a list of {@code Locale} instances for matching language tags 3276 // * sorted in descending order based on priority or weight, or an empty 3277 // * list if nothing matches. The list is modifiable. 3278 // * @throws NullPointerException if {@code priorityList} or {@code locales} 3279 // * is {@code null} 3280 // * 3281 // */ 3282 // static List<Locale> filter(List<LanguageRange> priorityList, 3283 // Collection<Locale> locales) { 3284 // return filter(priorityList, locales, FilteringMode.AUTOSELECT_FILTERING); 3285 // } 3286 3287 // /** 3288 // * Returns a list of matching languages tags using the basic filtering 3289 // * mechanism defined in RFC 4647. 3290 // * 3291 // * This filter operation on the given {@code tags} ensures that only 3292 // * unique matching tag(s) are returned with preserved case. In case of 3293 // * duplicate matching tags with the case difference, the first matching 3294 // * tag with preserved case is returned. 3295 // * For example, "de-ch" is returned out of the duplicate matching tags 3296 // * "de-ch" and "de-CH", if "de-ch" is checked first for matching in the 3297 // * given {@code tags}. Note that if the given {@code tags} is an unordered 3298 // * {@code Collection}, the returned matching tag out of duplicate tags is 3299 // * subject to change, depending on the implementation of the 3300 // * {@code Collection}. 3301 // * 3302 // * @param priorityList user's Language Priority List in which each language 3303 // * tag is sorted in descending order based on priority or weight 3304 // * @param tags language tags 3305 // * @param mode filtering mode 3306 // * @return a list of matching language tags sorted in descending order 3307 // * based on priority or weight, or an empty list if nothing matches. 3308 // * The list is modifiable. 3309 // * @throws NullPointerException if {@code priorityList} or {@code tags} is 3310 // * {@code null} 3311 // * @throws IllegalArgumentException if one or more extended language ranges 3312 // * are included in the given list when 3313 // * {@link FilteringMode#REJECT_EXTENDED_RANGES} is specified 3314 // * 3315 // */ 3316 // static List<string> filterTags(List<LanguageRange> priorityList, 3317 // Collection<string> tags, 3318 // FilteringMode mode) { 3319 // return LocaleMatcher.filterTags(priorityList, tags, mode); 3320 // } 3321 3322 // /** 3323 // * Returns a list of matching languages tags using the basic filtering 3324 // * mechanism defined in RFC 4647. This is equivalent to 3325 // * {@link #filterTags(List, Collection, FilteringMode)} when {@code mode} 3326 // * is {@link FilteringMode#AUTOSELECT_FILTERING}. 3327 // * 3328 // * This filter operation on the given {@code tags} ensures that only 3329 // * unique matching tag(s) are returned with preserved case. In case of 3330 // * duplicate matching tags with the case difference, the first matching 3331 // * tag with preserved case is returned. 3332 // * For example, "de-ch" is returned out of the duplicate matching tags 3333 // * "de-ch" and "de-CH", if "de-ch" is checked first for matching in the 3334 // * given {@code tags}. Note that if the given {@code tags} is an unordered 3335 // * {@code Collection}, the returned matching tag out of duplicate tags is 3336 // * subject to change, depending on the implementation of the 3337 // * {@code Collection}. 3338 // * 3339 // * @param priorityList user's Language Priority List in which each language 3340 // * tag is sorted in descending order based on priority or weight 3341 // * @param tags language tags 3342 // * @return a list of matching language tags sorted in descending order 3343 // * based on priority or weight, or an empty list if nothing matches. 3344 // * The list is modifiable. 3345 // * @throws NullPointerException if {@code priorityList} or {@code tags} is 3346 // * {@code null} 3347 // * 3348 // */ 3349 // static List<string> filterTags(List<LanguageRange> priorityList, 3350 // Collection<string> tags) { 3351 // return filterTags(priorityList, tags, FilteringMode.AUTOSELECT_FILTERING); 3352 // } 3353 3354 // /** 3355 // * Returns a {@code Locale} instance for the best-matching language 3356 // * tag using the lookup mechanism defined in RFC 4647. 3357 // * 3358 // * @param priorityList user's Language Priority List in which each language 3359 // * tag is sorted in descending order based on priority or weight 3360 // * @param locales {@code Locale} instances used for matching 3361 // * @return the best matching <code>Locale</code> instance chosen based on 3362 // * priority or weight, or {@code null} if nothing matches. 3363 // * @throws NullPointerException if {@code priorityList} or {@code tags} is 3364 // * {@code null} 3365 // * 3366 // */ 3367 // static Locale lookup(List<LanguageRange> priorityList, 3368 // Collection<Locale> locales) { 3369 // return LocaleMatcher.lookup(priorityList, locales); 3370 // } 3371 3372 // /** 3373 // * Returns the best-matching language tag using the lookup mechanism 3374 // * defined in RFC 4647. 3375 // * 3376 // * This lookup operation on the given {@code tags} ensures that the 3377 // * first matching tag with preserved case is returned. 3378 // * 3379 // * @param priorityList user's Language Priority List in which each language 3380 // * tag is sorted in descending order based on priority or weight 3381 // * @param tags language tangs used for matching 3382 // * @return the best matching language tag chosen based on priority or 3383 // * weight, or {@code null} if nothing matches. 3384 // * @throws NullPointerException if {@code priorityList} or {@code tags} is 3385 // * {@code null} 3386 // * 3387 // */ 3388 // static string lookupTag(List<LanguageRange> priorityList, 3389 // Collection<string> tags) { 3390 // return LocaleMatcher.lookupTag(priorityList, tags); 3391 // } 3392 3393 } 3394 3395 /** 3396 * Enum for locale categories. These locale categories are used to get/set 3397 * the default locale for the specific functionality represented by the 3398 * category. 3399 * 3400 * @see #getDefault(LocaleCategory) 3401 * @see #setDefault(LocaleCategory, Locale) 3402 */ 3403 struct LocaleCategory { 3404 3405 /** 3406 * Category used to represent the default locale for 3407 * displaying user interfaces. 3408 */ 3409 enum LocaleCategory DISPLAY = LocaleCategory("user.language.display", 3410 "user.script.display", 3411 "user.country.display", 3412 "user.variant.display", 3413 "user.extensions.display"); 3414 3415 /** 3416 * Category used to represent the default locale for 3417 * formatting dates, numbers, and/or currencies. 3418 */ 3419 enum LocaleCategory FORMAT = LocaleCategory("user.language.format", 3420 "user.script.format", 3421 "user.country.format", 3422 "user.variant.format", 3423 "user.extensions.format"); 3424 3425 this(string languageKey, string scriptKey, string countryKey, 3426 string variantKey, string extensionsKey) { 3427 this.languageKey = languageKey; 3428 this.scriptKey = scriptKey; 3429 this.countryKey = countryKey; 3430 this.variantKey = variantKey; 3431 this.extensionsKey = extensionsKey; 3432 } 3433 3434 string languageKey; 3435 string scriptKey; 3436 string countryKey; 3437 string variantKey; 3438 string extensionsKey; 3439 3440 // bool opEquals(const LocaleCategory h) nothrow { 3441 // return this.languageKey == h.languageKey && 3442 // // this.scriptKey == h.scriptKey && 3443 // this.countryKey == h.countryKey; 3444 // } 3445 3446 // bool opEquals(ref const LocaleCategory h) nothrow { 3447 // return this.languageKey == h.languageKey && 3448 // // this.scriptKey == h.scriptKey && 3449 // this.countryKey == h.countryKey; 3450 // } 3451 }