33 * of a License Agreement between Taligent and Sun. This technology
34 * is protected by multiple US and International patents.
35 *
36 * This notice and attribution to Taligent may not be removed.
37 * Taligent is a registered trademark of Taligent, Inc.
38 *
39 */
40
41 package java.util;
42
43 import java.io.IOException;
44 import java.io.ObjectInputStream;
45 import java.io.ObjectOutputStream;
46 import java.io.ObjectStreamField;
47 import java.io.Serializable;
48 import java.security.AccessController;
49 import java.text.MessageFormat;
50 import java.util.spi.LocaleNameProvider;
51
52 import sun.security.action.GetPropertyAction;
53 import sun.util.locale.provider.LocaleServiceProviderPool;
54 import sun.util.locale.BaseLocale;
55 import sun.util.locale.InternalLocaleBuilder;
56 import sun.util.locale.LanguageTag;
57 import sun.util.locale.LocaleExtensions;
58 import sun.util.locale.LocaleMatcher;
59 import sun.util.locale.LocaleObjectCache;
60 import sun.util.locale.LocaleSyntaxException;
61 import sun.util.locale.LocaleUtils;
62 import sun.util.locale.ParseStatus;
63 import sun.util.locale.provider.LocaleProviderAdapter;
64 import sun.util.resources.OpenListResourceBundle;
65
66 /**
67 * A <code>Locale</code> object represents a specific geographical, political,
68 * or cultural region. An operation that requires a <code>Locale</code> to perform
69 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
70 * to tailor information for the user. For example, displaying a number
71 * is a locale-sensitive operation— the number should be formatted
72 * according to the customs and conventions of the user's native country,
73 * region, or culture.
74 *
75 * <p> The {@code Locale} class implements IETF BCP 47 which is composed of
76 * <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 "Matching of Language
77 * Tags"</a> and <a href="http://tools.ietf.org/html/rfc5646">RFC 5646 "Tags
78 * for Identifying Languages"</a> with support for the LDML (UTS#35, "Unicode
79 * Locale Data Markup Language") BCP 47-compatible extensions for locale data
80 * exchange.
81 *
82 * <p> A <code>Locale</code> object logically consists of the fields
83 * described below.
84 *
1762 /**
1763 * Returns a name for the locale's variant code that is appropriate for display to the
1764 * user. If possible, the name will be localized for the default locale. If the locale
1765 * doesn't specify a variant code, this function returns the empty string.
1766 */
1767 public final String getDisplayVariant() {
1768 return getDisplayVariant(getDefault(Category.DISPLAY));
1769 }
1770
1771 /**
1772 * Returns a name for the locale's variant code that is appropriate for display to the
1773 * user. If possible, the name will be localized for inLocale. If the locale
1774 * doesn't specify a variant code, this function returns the empty string.
1775 *
1776 * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
1777 */
1778 public String getDisplayVariant(Locale inLocale) {
1779 if (baseLocale.getVariant().length() == 0)
1780 return "";
1781
1782 OpenListResourceBundle bundle = LocaleProviderAdapter.forJRE().getLocaleData().getLocaleNames(inLocale);
1783
1784 String names[] = getDisplayVariantArray(bundle, inLocale);
1785
1786 // Get the localized patterns for formatting a list, and use
1787 // them to format the list.
1788 String listPattern = null;
1789 String listCompositionPattern = null;
1790 try {
1791 listPattern = bundle.getString("ListPattern");
1792 listCompositionPattern = bundle.getString("ListCompositionPattern");
1793 } catch (MissingResourceException e) {
1794 }
1795 return formatList(names, listPattern, listCompositionPattern);
1796 }
1797
1798 /**
1799 * Returns a name for the locale that is appropriate for display to the
1800 * user. This will be the values returned by getDisplayLanguage(),
1801 * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
1802 * into a single string. The the non-empty values are used in order,
1803 * with the second and subsequent names in parentheses. For example:
1804 * <blockquote>
1805 * language (script, country, variant)<br>
1806 * language (country)<br>
1807 * language (variant)<br>
1808 * script (country)<br>
1809 * country<br>
1810 * </blockquote>
1811 * depending on which fields are specified in the locale. If the
1812 * language, sacript, country, and variant fields are all empty,
1813 * this function returns the empty string.
1814 */
1815 public final String getDisplayName() {
1816 return getDisplayName(getDefault(Category.DISPLAY));
1820 * Returns a name for the locale that is appropriate for display
1821 * to the user. This will be the values returned by
1822 * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(),
1823 * and getDisplayVariant() assembled into a single string.
1824 * The non-empty values are used in order,
1825 * with the second and subsequent names in parentheses. For example:
1826 * <blockquote>
1827 * language (script, country, variant)<br>
1828 * language (country)<br>
1829 * language (variant)<br>
1830 * script (country)<br>
1831 * country<br>
1832 * </blockquote>
1833 * depending on which fields are specified in the locale. If the
1834 * language, script, country, and variant fields are all empty,
1835 * this function returns the empty string.
1836 *
1837 * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
1838 */
1839 public String getDisplayName(Locale inLocale) {
1840 OpenListResourceBundle bundle = LocaleProviderAdapter.forJRE().getLocaleData().getLocaleNames(inLocale);
1841
1842 String languageName = getDisplayLanguage(inLocale);
1843 String scriptName = getDisplayScript(inLocale);
1844 String countryName = getDisplayCountry(inLocale);
1845 String[] variantNames = getDisplayVariantArray(bundle, inLocale);
1846
1847 // Get the localized patterns for formatting a display name.
1848 String displayNamePattern = null;
1849 String listPattern = null;
1850 String listCompositionPattern = null;
1851 try {
1852 displayNamePattern = bundle.getString("DisplayNamePattern");
1853 listPattern = bundle.getString("ListPattern");
1854 listCompositionPattern = bundle.getString("ListCompositionPattern");
1855 } catch (MissingResourceException e) {
1856 }
1857
1858 // The display name consists of a main name, followed by qualifiers.
1859 // Typically, the format is "MainName (Qualifier, Qualifier)" but this
1860 // depends on what pattern is stored in the display locale.
1861 String mainName = null;
1862 String[] qualifierNames = null;
1863
1864 // The main name is the language, or if there is no language, the script,
1865 // then if no script, the country. If there is no language/script/country
1866 // (an anomalous situation) then the display name is simply the variant's
1867 // display name.
1868 if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
1869 if (variantNames.length == 0) {
1870 return "";
1871 } else {
1872 return formatList(variantNames, listPattern, listCompositionPattern);
1873 }
1874 }
1875 ArrayList<String> names = new ArrayList<>(4);
1876 if (languageName.length() != 0) {
1988
1989 private transient BaseLocale baseLocale;
1990 private transient LocaleExtensions localeExtensions;
1991
1992 /**
1993 * Calculated hashcode
1994 */
1995 private transient volatile int hashCodeValue = 0;
1996
1997 private volatile static Locale defaultLocale = initDefault();
1998 private volatile static Locale defaultDisplayLocale = null;
1999 private volatile static Locale defaultFormatLocale = null;
2000
2001 private transient volatile String languageTag;
2002
2003 /**
2004 * Return an array of the display names of the variant.
2005 * @param bundle the ResourceBundle to use to get the display names
2006 * @return an array of display names, possible of zero length.
2007 */
2008 private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) {
2009 // Split the variant name into tokens separated by '_'.
2010 StringTokenizer tokenizer = new StringTokenizer(baseLocale.getVariant(), "_");
2011 String[] names = new String[tokenizer.countTokens()];
2012
2013 // For each variant token, lookup the display name. If
2014 // not found, use the variant name itself.
2015 for (int i=0; i<names.length; ++i) {
2016 names[i] = getDisplayString(tokenizer.nextToken(),
2017 inLocale, DISPLAY_VARIANT);
2018 }
2019
2020 return names;
2021 }
2022
2023 /**
2024 * Format a list using given pattern strings.
2025 * If either of the patterns is null, then a the list is
2026 * formatted by concatenation with the delimiter ','.
2027 * @param stringList the list of strings to be formatted.
2028 * @param listPattern should create a MessageFormat taking 0-3 arguments
|
33 * of a License Agreement between Taligent and Sun. This technology
34 * is protected by multiple US and International patents.
35 *
36 * This notice and attribution to Taligent may not be removed.
37 * Taligent is a registered trademark of Taligent, Inc.
38 *
39 */
40
41 package java.util;
42
43 import java.io.IOException;
44 import java.io.ObjectInputStream;
45 import java.io.ObjectOutputStream;
46 import java.io.ObjectStreamField;
47 import java.io.Serializable;
48 import java.security.AccessController;
49 import java.text.MessageFormat;
50 import java.util.spi.LocaleNameProvider;
51
52 import sun.security.action.GetPropertyAction;
53 import sun.util.locale.BaseLocale;
54 import sun.util.locale.InternalLocaleBuilder;
55 import sun.util.locale.LanguageTag;
56 import sun.util.locale.LocaleExtensions;
57 import sun.util.locale.LocaleMatcher;
58 import sun.util.locale.LocaleObjectCache;
59 import sun.util.locale.LocaleSyntaxException;
60 import sun.util.locale.LocaleUtils;
61 import sun.util.locale.ParseStatus;
62 import sun.util.locale.provider.LocaleProviderAdapter;
63 import sun.util.locale.provider.LocaleResources;
64 import sun.util.locale.provider.LocaleServiceProviderPool;
65 import sun.util.locale.provider.ResourceBundleBasedAdapter;
66
67 /**
68 * A <code>Locale</code> object represents a specific geographical, political,
69 * or cultural region. An operation that requires a <code>Locale</code> to perform
70 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
71 * to tailor information for the user. For example, displaying a number
72 * is a locale-sensitive operation— the number should be formatted
73 * according to the customs and conventions of the user's native country,
74 * region, or culture.
75 *
76 * <p> The {@code Locale} class implements IETF BCP 47 which is composed of
77 * <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 "Matching of Language
78 * Tags"</a> and <a href="http://tools.ietf.org/html/rfc5646">RFC 5646 "Tags
79 * for Identifying Languages"</a> with support for the LDML (UTS#35, "Unicode
80 * Locale Data Markup Language") BCP 47-compatible extensions for locale data
81 * exchange.
82 *
83 * <p> A <code>Locale</code> object logically consists of the fields
84 * described below.
85 *
1763 /**
1764 * Returns a name for the locale's variant code that is appropriate for display to the
1765 * user. If possible, the name will be localized for the default locale. If the locale
1766 * doesn't specify a variant code, this function returns the empty string.
1767 */
1768 public final String getDisplayVariant() {
1769 return getDisplayVariant(getDefault(Category.DISPLAY));
1770 }
1771
1772 /**
1773 * Returns a name for the locale's variant code that is appropriate for display to the
1774 * user. If possible, the name will be localized for inLocale. If the locale
1775 * doesn't specify a variant code, this function returns the empty string.
1776 *
1777 * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
1778 */
1779 public String getDisplayVariant(Locale inLocale) {
1780 if (baseLocale.getVariant().length() == 0)
1781 return "";
1782
1783 LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(inLocale);
1784
1785 String names[] = getDisplayVariantArray(inLocale);
1786
1787 // Get the localized patterns for formatting a list, and use
1788 // them to format the list.
1789 return formatList(names,
1790 lr.getLocaleName("ListPattern"),
1791 lr.getLocaleName("ListCompositionPattern"));
1792 }
1793
1794 /**
1795 * Returns a name for the locale that is appropriate for display to the
1796 * user. This will be the values returned by getDisplayLanguage(),
1797 * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
1798 * into a single string. The the non-empty values are used in order,
1799 * with the second and subsequent names in parentheses. For example:
1800 * <blockquote>
1801 * language (script, country, variant)<br>
1802 * language (country)<br>
1803 * language (variant)<br>
1804 * script (country)<br>
1805 * country<br>
1806 * </blockquote>
1807 * depending on which fields are specified in the locale. If the
1808 * language, sacript, country, and variant fields are all empty,
1809 * this function returns the empty string.
1810 */
1811 public final String getDisplayName() {
1812 return getDisplayName(getDefault(Category.DISPLAY));
1816 * Returns a name for the locale that is appropriate for display
1817 * to the user. This will be the values returned by
1818 * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(),
1819 * and getDisplayVariant() assembled into a single string.
1820 * The non-empty values are used in order,
1821 * with the second and subsequent names in parentheses. For example:
1822 * <blockquote>
1823 * language (script, country, variant)<br>
1824 * language (country)<br>
1825 * language (variant)<br>
1826 * script (country)<br>
1827 * country<br>
1828 * </blockquote>
1829 * depending on which fields are specified in the locale. If the
1830 * language, script, country, and variant fields are all empty,
1831 * this function returns the empty string.
1832 *
1833 * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
1834 */
1835 public String getDisplayName(Locale inLocale) {
1836 LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(inLocale);
1837
1838 String languageName = getDisplayLanguage(inLocale);
1839 String scriptName = getDisplayScript(inLocale);
1840 String countryName = getDisplayCountry(inLocale);
1841 String[] variantNames = getDisplayVariantArray(inLocale);
1842
1843 // Get the localized patterns for formatting a display name.
1844 String displayNamePattern = lr.getLocaleName("DisplayNamePattern");
1845 String listPattern = lr.getLocaleName("ListPattern");
1846 String listCompositionPattern = lr.getLocaleName("ListCompositionPattern");
1847
1848 // The display name consists of a main name, followed by qualifiers.
1849 // Typically, the format is "MainName (Qualifier, Qualifier)" but this
1850 // depends on what pattern is stored in the display locale.
1851 String mainName = null;
1852 String[] qualifierNames = null;
1853
1854 // The main name is the language, or if there is no language, the script,
1855 // then if no script, the country. If there is no language/script/country
1856 // (an anomalous situation) then the display name is simply the variant's
1857 // display name.
1858 if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
1859 if (variantNames.length == 0) {
1860 return "";
1861 } else {
1862 return formatList(variantNames, listPattern, listCompositionPattern);
1863 }
1864 }
1865 ArrayList<String> names = new ArrayList<>(4);
1866 if (languageName.length() != 0) {
1978
1979 private transient BaseLocale baseLocale;
1980 private transient LocaleExtensions localeExtensions;
1981
1982 /**
1983 * Calculated hashcode
1984 */
1985 private transient volatile int hashCodeValue = 0;
1986
1987 private volatile static Locale defaultLocale = initDefault();
1988 private volatile static Locale defaultDisplayLocale = null;
1989 private volatile static Locale defaultFormatLocale = null;
1990
1991 private transient volatile String languageTag;
1992
1993 /**
1994 * Return an array of the display names of the variant.
1995 * @param bundle the ResourceBundle to use to get the display names
1996 * @return an array of display names, possible of zero length.
1997 */
1998 private String[] getDisplayVariantArray(Locale inLocale) {
1999 // Split the variant name into tokens separated by '_'.
2000 StringTokenizer tokenizer = new StringTokenizer(baseLocale.getVariant(), "_");
2001 String[] names = new String[tokenizer.countTokens()];
2002
2003 // For each variant token, lookup the display name. If
2004 // not found, use the variant name itself.
2005 for (int i=0; i<names.length; ++i) {
2006 names[i] = getDisplayString(tokenizer.nextToken(),
2007 inLocale, DISPLAY_VARIANT);
2008 }
2009
2010 return names;
2011 }
2012
2013 /**
2014 * Format a list using given pattern strings.
2015 * If either of the patterns is null, then a the list is
2016 * formatted by concatenation with the delimiter ','.
2017 * @param stringList the list of strings to be formatted.
2018 * @param listPattern should create a MessageFormat taking 0-3 arguments
|