1394 * @see #getDisplayName |
1394 * @see #getDisplayName |
1395 * @see #toLanguageTag |
1395 * @see #toLanguageTag |
1396 */ |
1396 */ |
1397 @Override |
1397 @Override |
1398 public final String toString() { |
1398 public final String toString() { |
1399 boolean l = (baseLocale.getLanguage().length() != 0); |
1399 boolean l = !baseLocale.getLanguage().isEmpty(); |
1400 boolean s = (baseLocale.getScript().length() != 0); |
1400 boolean s = !baseLocale.getScript().isEmpty(); |
1401 boolean r = (baseLocale.getRegion().length() != 0); |
1401 boolean r = !baseLocale.getRegion().isEmpty(); |
1402 boolean v = (baseLocale.getVariant().length() != 0); |
1402 boolean v = !baseLocale.getVariant().isEmpty(); |
1403 boolean e = (localeExtensions != null && localeExtensions.getID().length() != 0); |
1403 boolean e = localeExtensions != null && !localeExtensions.getID().isEmpty(); |
1404 |
1404 |
1405 StringBuilder result = new StringBuilder(baseLocale.getLanguage()); |
1405 StringBuilder result = new StringBuilder(baseLocale.getLanguage()); |
1406 if (r || (l && (v || s || e))) { |
1406 if (r || (l && (v || s || e))) { |
1407 result.append('_') |
1407 result.append('_') |
1408 .append(baseLocale.getRegion()); // This may just append '_' |
1408 .append(baseLocale.getRegion()); // This may just append '_' |
1502 |
1502 |
1503 LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions); |
1503 LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions); |
1504 StringBuilder buf = new StringBuilder(); |
1504 StringBuilder buf = new StringBuilder(); |
1505 |
1505 |
1506 String subtag = tag.getLanguage(); |
1506 String subtag = tag.getLanguage(); |
1507 if (subtag.length() > 0) { |
1507 if (!subtag.isEmpty()) { |
1508 buf.append(LanguageTag.canonicalizeLanguage(subtag)); |
1508 buf.append(LanguageTag.canonicalizeLanguage(subtag)); |
1509 } |
1509 } |
1510 |
1510 |
1511 subtag = tag.getScript(); |
1511 subtag = tag.getScript(); |
1512 if (subtag.length() > 0) { |
1512 if (!subtag.isEmpty()) { |
1513 buf.append(LanguageTag.SEP); |
1513 buf.append(LanguageTag.SEP); |
1514 buf.append(LanguageTag.canonicalizeScript(subtag)); |
1514 buf.append(LanguageTag.canonicalizeScript(subtag)); |
1515 } |
1515 } |
1516 |
1516 |
1517 subtag = tag.getRegion(); |
1517 subtag = tag.getRegion(); |
1518 if (subtag.length() > 0) { |
1518 if (!subtag.isEmpty()) { |
1519 buf.append(LanguageTag.SEP); |
1519 buf.append(LanguageTag.SEP); |
1520 buf.append(LanguageTag.canonicalizeRegion(subtag)); |
1520 buf.append(LanguageTag.canonicalizeRegion(subtag)); |
1521 } |
1521 } |
1522 |
1522 |
1523 List<String>subtags = tag.getVariants(); |
1523 List<String>subtags = tag.getVariants(); |
1532 buf.append(LanguageTag.SEP); |
1532 buf.append(LanguageTag.SEP); |
1533 buf.append(LanguageTag.canonicalizeExtension(s)); |
1533 buf.append(LanguageTag.canonicalizeExtension(s)); |
1534 } |
1534 } |
1535 |
1535 |
1536 subtag = tag.getPrivateuse(); |
1536 subtag = tag.getPrivateuse(); |
1537 if (subtag.length() > 0) { |
1537 if (!subtag.isEmpty()) { |
1538 if (buf.length() > 0) { |
1538 if (buf.length() > 0) { |
1539 buf.append(LanguageTag.SEP); |
1539 buf.append(LanguageTag.SEP); |
1540 } |
1540 } |
1541 buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP); |
1541 buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP); |
1542 // preserve casing |
1542 // preserve casing |
1682 LanguageTag tag = LanguageTag.parse(languageTag, null); |
1682 LanguageTag tag = LanguageTag.parse(languageTag, null); |
1683 InternalLocaleBuilder bldr = new InternalLocaleBuilder(); |
1683 InternalLocaleBuilder bldr = new InternalLocaleBuilder(); |
1684 bldr.setLanguageTag(tag); |
1684 bldr.setLanguageTag(tag); |
1685 BaseLocale base = bldr.getBaseLocale(); |
1685 BaseLocale base = bldr.getBaseLocale(); |
1686 LocaleExtensions exts = bldr.getLocaleExtensions(); |
1686 LocaleExtensions exts = bldr.getLocaleExtensions(); |
1687 if (exts == null && base.getVariant().length() > 0) { |
1687 if (exts == null && !base.getVariant().isEmpty()) { |
1688 exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(), |
1688 exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(), |
1689 base.getRegion(), base.getVariant()); |
1689 base.getRegion(), base.getVariant()); |
1690 } |
1690 } |
1691 return getInstance(base, exts); |
1691 return getInstance(base, exts); |
1692 } |
1692 } |
1915 * @param inLocale The locale for which to retrieve the display variant code. |
1915 * @param inLocale The locale for which to retrieve the display variant code. |
1916 * @return The name of the display variant code appropriate to the given locale. |
1916 * @return The name of the display variant code appropriate to the given locale. |
1917 * @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
1917 * @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
1918 */ |
1918 */ |
1919 public String getDisplayVariant(Locale inLocale) { |
1919 public String getDisplayVariant(Locale inLocale) { |
1920 if (baseLocale.getVariant().length() == 0) |
1920 if (baseLocale.getVariant().isEmpty()) |
1921 return ""; |
1921 return ""; |
1922 |
1922 |
1923 LocaleResources lr = LocaleProviderAdapter |
1923 LocaleResources lr = LocaleProviderAdapter |
1924 .getResourceBundleBased() |
1924 .getResourceBundleBased() |
1925 .getLocaleResources(inLocale); |
1925 .getLocaleResources(inLocale); |
1996 String listCompositionPattern = lr.getLocaleName("ListCompositionPattern"); |
1996 String listCompositionPattern = lr.getLocaleName("ListCompositionPattern"); |
1997 |
1997 |
1998 // The display name consists of a main name, followed by qualifiers. |
1998 // The display name consists of a main name, followed by qualifiers. |
1999 // Typically, the format is "MainName (Qualifier, Qualifier)" but this |
1999 // Typically, the format is "MainName (Qualifier, Qualifier)" but this |
2000 // depends on what pattern is stored in the display locale. |
2000 // depends on what pattern is stored in the display locale. |
2001 String mainName = null; |
2001 String mainName; |
2002 String[] qualifierNames = null; |
2002 String[] qualifierNames; |
2003 |
2003 |
2004 // The main name is the language, or if there is no language, the script, |
2004 // The main name is the language, or if there is no language, the script, |
2005 // then if no script, the country. If there is no language/script/country |
2005 // then if no script, the country. If there is no language/script/country |
2006 // (an anomalous situation) then the display name is simply the variant's |
2006 // (an anomalous situation) then the display name is simply the variant's |
2007 // display name. |
2007 // display name. |
2008 if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) { |
2008 if (languageName.isEmpty() && scriptName.isEmpty() && countryName.isEmpty()) { |
2009 if (variantNames.length == 0) { |
2009 if (variantNames.length == 0) { |
2010 return ""; |
2010 return ""; |
2011 } else { |
2011 } else { |
2012 return formatList(variantNames, listCompositionPattern); |
2012 return formatList(variantNames, listCompositionPattern); |
2013 } |
2013 } |
2014 } |
2014 } |
2015 ArrayList<String> names = new ArrayList<>(4); |
2015 ArrayList<String> names = new ArrayList<>(4); |
2016 if (languageName.length() != 0) { |
2016 if (!languageName.isEmpty()) { |
2017 names.add(languageName); |
2017 names.add(languageName); |
2018 } |
2018 } |
2019 if (scriptName.length() != 0) { |
2019 if (!scriptName.isEmpty()) { |
2020 names.add(scriptName); |
2020 names.add(scriptName); |
2021 } |
2021 } |
2022 if (countryName.length() != 0) { |
2022 if (!countryName.isEmpty()) { |
2023 names.add(countryName); |
2023 names.add(countryName); |
2024 } |
2024 } |
2025 if (variantNames.length != 0) { |
2025 if (variantNames.length != 0) { |
2026 names.addAll(Arrays.asList(variantNames)); |
2026 names.addAll(Arrays.asList(variantNames)); |
2027 } |
2027 } |
2307 String script = (String)fields.get("script", ""); |
2307 String script = (String)fields.get("script", ""); |
2308 String country = (String)fields.get("country", ""); |
2308 String country = (String)fields.get("country", ""); |
2309 String variant = (String)fields.get("variant", ""); |
2309 String variant = (String)fields.get("variant", ""); |
2310 String extStr = (String)fields.get("extensions", ""); |
2310 String extStr = (String)fields.get("extensions", ""); |
2311 baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant); |
2311 baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant); |
2312 if (extStr.length() > 0) { |
2312 if (!extStr.isEmpty()) { |
2313 try { |
2313 try { |
2314 InternalLocaleBuilder bldr = new InternalLocaleBuilder(); |
2314 InternalLocaleBuilder bldr = new InternalLocaleBuilder(); |
2315 bldr.setExtensions(extStr); |
2315 bldr.setExtensions(extStr); |
2316 localeExtensions = bldr.getLocaleExtensions(); |
2316 localeExtensions = bldr.getLocaleExtensions(); |
2317 } catch (LocaleSyntaxException e) { |
2317 } catch (LocaleSyntaxException e) { |
2365 String country, |
2365 String country, |
2366 String variant) { |
2366 String variant) { |
2367 LocaleExtensions extensions = null; |
2367 LocaleExtensions extensions = null; |
2368 // Special cases for backward compatibility support |
2368 // Special cases for backward compatibility support |
2369 if (LocaleUtils.caseIgnoreMatch(language, "ja") |
2369 if (LocaleUtils.caseIgnoreMatch(language, "ja") |
2370 && script.length() == 0 |
2370 && script.isEmpty() |
2371 && LocaleUtils.caseIgnoreMatch(country, "jp") |
2371 && LocaleUtils.caseIgnoreMatch(country, "jp") |
2372 && "JP".equals(variant)) { |
2372 && "JP".equals(variant)) { |
2373 // ja_JP_JP -> u-ca-japanese (calendar = japanese) |
2373 // ja_JP_JP -> u-ca-japanese (calendar = japanese) |
2374 extensions = LocaleExtensions.CALENDAR_JAPANESE; |
2374 extensions = LocaleExtensions.CALENDAR_JAPANESE; |
2375 } else if (LocaleUtils.caseIgnoreMatch(language, "th") |
2375 } else if (LocaleUtils.caseIgnoreMatch(language, "th") |
2376 && script.length() == 0 |
2376 && script.isEmpty() |
2377 && LocaleUtils.caseIgnoreMatch(country, "th") |
2377 && LocaleUtils.caseIgnoreMatch(country, "th") |
2378 && "TH".equals(variant)) { |
2378 && "TH".equals(variant)) { |
2379 // th_TH_TH -> u-nu-thai (numbersystem = thai) |
2379 // th_TH_TH -> u-nu-thai (numbersystem = thai) |
2380 extensions = LocaleExtensions.NUMBER_THAI; |
2380 extensions = LocaleExtensions.NUMBER_THAI; |
2381 } |
2381 } |
2804 * @return A Locale. |
2804 * @return A Locale. |
2805 */ |
2805 */ |
2806 public Locale build() { |
2806 public Locale build() { |
2807 BaseLocale baseloc = localeBuilder.getBaseLocale(); |
2807 BaseLocale baseloc = localeBuilder.getBaseLocale(); |
2808 LocaleExtensions extensions = localeBuilder.getLocaleExtensions(); |
2808 LocaleExtensions extensions = localeBuilder.getLocaleExtensions(); |
2809 if (extensions == null && baseloc.getVariant().length() > 0) { |
2809 if (extensions == null && !baseloc.getVariant().isEmpty()) { |
2810 extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(), |
2810 extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(), |
2811 baseloc.getRegion(), baseloc.getVariant()); |
2811 baseloc.getRegion(), baseloc.getVariant()); |
2812 } |
2812 } |
2813 return Locale.getInstance(baseloc, extensions); |
2813 return Locale.getInstance(baseloc, extensions); |
2814 } |
2814 } |