jdk/src/share/classes/java/util/Locale.java
author naoto
Tue, 31 Aug 2010 11:27:10 -0700
changeset 6489 9e7015635425
parent 5506 202f599c92aa
child 6501 684810d882b3
permissions -rw-r--r--
4700857: RFE: separating user locale and user interface locale Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.spi.LocaleNameProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.spi.LocaleServiceProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.util.LocaleServiceProviderPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.util.resources.LocaleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.util.resources.OpenListResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * A <code>Locale</code> object represents a specific geographical, political,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * or cultural region. An operation that requires a <code>Locale</code> to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to tailor information for the user. For example, displaying a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * is a locale-sensitive operation--the number should be formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * according to the customs/conventions of the user's native country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * region, or culture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Create a <code>Locale</code> object using the constructors in this class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Locale(String language)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Locale(String language, String country)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Locale(String language, String country, String variant)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The language argument is a valid <STRONG>ISO Language Code.</STRONG>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * These codes are the lower-case, two-letter codes as defined by ISO-639.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * You can find a full list of these codes at a number of sites, such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <BR><a href ="http://www.loc.gov/standards/iso639-2/php/English_list.php">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <code>http://www.loc.gov/standards/iso639-2/php/English_list.php</code></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * The country argument is a valid <STRONG>ISO Country Code.</STRONG> These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * codes are the upper-case, two-letter codes as defined by ISO-3166.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * You can find a full list of these codes at a number of sites, such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <BR><a href="http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html</code></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The variant argument is a vendor or browser-specific code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Where there are two variants, separate them with an underscore, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * put the most important one first. For example, a Traditional Spanish collation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * might construct a locale with parameters for language, country and variant as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * "es", "ES", "Traditional_WIN".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Because a <code>Locale</code> object is just an identifier for a region,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * no validity check is performed when you construct a <code>Locale</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * If you want to see whether particular resources are available for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <code>Locale</code> you construct, you must query those resources. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * example, ask the <code>NumberFormat</code> for the locales it supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * using its <code>getAvailableLocales</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <BR><STRONG>Note:</STRONG> When you ask for a resource for a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * locale, you get back the best available match, not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * precisely what you asked for. For more information, look at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * {@link ResourceBundle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * The <code>Locale</code> class provides a number of convenient constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * that you can use to create <code>Locale</code> objects for commonly used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * locales. For example, the following creates a <code>Locale</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * for the United States:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Locale.US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * Once you've created a <code>Locale</code> you can query it for information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * itself. Use <code>getCountry</code> to get the ISO Country Code and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <code>getLanguage</code> to get the ISO Language Code. You can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * use <code>getDisplayCountry</code> to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * name of the country suitable for displaying to the user. Similarly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * you can use <code>getDisplayLanguage</code> to get the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * the language suitable for displaying to the user. Interestingly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * and have two versions: one that uses the default locale and one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * that uses the locale specified as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * The Java Platform provides a number of classes that perform locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * operations. For example, the <code>NumberFormat</code> class formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * numbers, currency, or percentages in a locale-sensitive manner. Classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * such as <code>NumberFormat</code> have a number of convenience methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * for creating a default object of that type. For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code>NumberFormat</code> class provides these three convenience methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * for creating a default <code>NumberFormat</code> object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * NumberFormat.getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * NumberFormat.getCurrencyInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * NumberFormat.getPercentInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * These methods have two variants; one with an explicit locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * and one without; the latter using the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * NumberFormat.getInstance(myLocale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * NumberFormat.getCurrencyInstance(myLocale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * NumberFormat.getPercentInstance(myLocale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * A <code>Locale</code> is the mechanism for identifying the kind of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * (<code>NumberFormat</code>) that you would like to get. The locale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <STRONG>just</STRONG> a mechanism for identifying objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <STRONG>not</STRONG> a container for the objects themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @see         ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * @see         java.text.Format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * @see         java.text.NumberFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * @see         java.text.Collator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * @author      Mark Davis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @since       1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
public final class Locale implements Cloneable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // cache to store singleton Locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private final static ConcurrentHashMap<String, Locale> cache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        new ConcurrentHashMap<String, Locale>(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static public final Locale ENGLISH = createSingleton("en__", "en", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static public final Locale FRENCH = createSingleton("fr__", "fr", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static public final Locale GERMAN = createSingleton("de__", "de", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static public final Locale ITALIAN = createSingleton("it__", "it", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static public final Locale JAPANESE = createSingleton("ja__", "ja", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static public final Locale KOREAN = createSingleton("ko__", "ko", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static public final Locale CHINESE = createSingleton("zh__", "zh", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static public final Locale SIMPLIFIED_CHINESE = createSingleton("zh_CN_", "zh", "CN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    static public final Locale TRADITIONAL_CHINESE = createSingleton("zh_TW_", "zh", "TW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static public final Locale FRANCE = createSingleton("fr_FR_", "fr", "FR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    static public final Locale GERMANY = createSingleton("de_DE_", "de", "DE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static public final Locale ITALY = createSingleton("it_IT_", "it", "IT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static public final Locale JAPAN = createSingleton("ja_JP_", "ja", "JP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static public final Locale KOREA = createSingleton("ko_KR_", "ko", "KR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static public final Locale CHINA = SIMPLIFIED_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static public final Locale PRC = SIMPLIFIED_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    static public final Locale TAIWAN = TRADITIONAL_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static public final Locale UK = createSingleton("en_GB_", "en", "GB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static public final Locale US = createSingleton("en_US_", "en", "US");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static public final Locale CANADA = createSingleton("en_CA_", "en", "CA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    static public final Locale CANADA_FRENCH = createSingleton("fr_CA_", "fr", "CA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Useful constant for the root locale.  The root locale is the locale whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * language, country, and variant are empty ("") strings.  This is regarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * as the base locale of all locales, and is used as the language/country
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * neutral locale for the locale sensitive operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    static public final Locale ROOT = createSingleton("__", "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /** serialization ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    static final long serialVersionUID = 9149081749638150636L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Display types for retrieving localized names from the name providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static final int DISPLAY_LANGUAGE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static final int DISPLAY_COUNTRY  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private static final int DISPLAY_VARIANT  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Construct a locale from language, country, variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * NOTE:  ISO 639 is not a stable standard; some of the language codes it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * (specifically iw, ji, and in) have changed.  This constructor accepts both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * API on Locale will return only the OLD codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param country uppercase two-letter ISO-3166 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param variant vendor and browser specific code. See class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception NullPointerException thrown if any argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public Locale(String language, String country, String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        this.language = convertOldISOCodes(language);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        this.country = toUpperCase(country).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        this.variant = variant.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Construct a locale from language, country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * NOTE:  ISO 639 is not a stable standard; some of the language codes it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * (specifically iw, ji, and in) have changed.  This constructor accepts both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * API on Locale will return only the OLD codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param country uppercase two-letter ISO-3166 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception NullPointerException thrown if either argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public Locale(String language, String country) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        this(language, country, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Construct a locale from a language code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * NOTE:  ISO 639 is not a stable standard; some of the language codes it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * (specifically iw, ji, and in) have changed.  This constructor accepts both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * API on Locale will return only the OLD codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @exception NullPointerException thrown if argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public Locale(String language) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this(language, "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Constructs a <code>Locale</code> using <code>language</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * and <code>country</code>.  This constructor assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <code>language</code> and <code>contry</code> are interned and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * it is invoked by createSingleton only. (flag is just for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * avoiding the conflict with the public constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private Locale(String language, String country, boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        this.language = language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        this.country = country;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        this.variant = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Creates a <code>Locale</code> instance with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <code>language</code> and <code>counry</code> and puts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * instance under the given <code>key</code> in the cache. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * method must be called only when initializing the Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private static Locale createSingleton(String key, String language, String country) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Locale locale = new Locale(language, country, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        cache.put(key, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Returns a <code>Locale</code> constructed from the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <code>language</code>, <code>country</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <code>variant</code>. If the same <code>Locale</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * is available in the cache, then that instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * returned. Otherwise, a new <code>Locale</code> instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * created and cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param country uppercase two-letter ISO-3166 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param variant vendor and browser specific code. See class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @return the <code>Locale</code> instance requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @exception NullPointerException if any argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    static Locale getInstance(String language, String country, String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (language== null || country == null || variant == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        sb.append(language).append('_').append(country).append('_').append(variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        String key = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Locale locale = cache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            locale = new Locale(language, country, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            Locale l = cache.putIfAbsent(key, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                locale = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Gets the current value of the default locale for this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * It can be changed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * {@link #setDefault(java.util.Locale) setDefault} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return the default locale for this instance of the Java Virtual Machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public static Locale getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // do not synchronize this method - see 4071298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // it's OK if more than one default locale happens to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (defaultLocale == null) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   398
            initDefault();
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   399
        }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   400
        return defaultLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   401
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   402
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   403
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   404
     * Gets the current value of the default locale for the specified Category
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   405
     * for this instance of the Java Virtual Machine.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   406
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   407
     * The Java Virtual Machine sets the default locale during startup based
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   408
     * on the host environment. It is used by many locale-sensitive methods
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   409
     * if no locale is explicitly specified. It can be changed using the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   410
     * setDefault(Locale.Category, Locale) method.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   411
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   412
     * @param category - the specified category to get the default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   413
     * @throws NullPointerException - if category is null
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   414
     * @return the default locale for the specified Category for this instance
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   415
     *     of the Java Virtual Machine
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   416
     * @see #setDefault(Locale.Category, Locale)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   417
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   418
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   419
    public static Locale getDefault(Locale.Category category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   420
        // do not synchronize this method - see 4071298
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   421
        // it's OK if more than one default locale happens to be created
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   422
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   423
        case DISPLAY:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   424
            if (defaultDisplayLocale == null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   425
                initDefault(category);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   426
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   427
            return defaultDisplayLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   428
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   429
            if (defaultFormatLocale == null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   430
                initDefault(category);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   431
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   432
            return defaultFormatLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   433
        default:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   434
            assert false: "Unknown Category";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   435
        }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   436
        return getDefault();
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   437
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   438
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   439
    private static void initDefault() {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   440
        String language, region, country, variant;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   441
        language = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   442
            new GetPropertyAction("user.language", "en"));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   443
        // for compatibility, check for old user.region property
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   444
        region = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   445
            new GetPropertyAction("user.region"));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   446
        if (region != null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   447
            // region can be of form country, country_variant, or _variant
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   448
            int i = region.indexOf('_');
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   449
            if (i >= 0) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   450
                country = region.substring(0, i);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   451
                variant = region.substring(i + 1);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   452
            } else {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   453
                country = region;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   454
                variant = "";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   455
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   456
        } else {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   457
            country = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   458
                new GetPropertyAction("user.country", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   459
            variant = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   460
                new GetPropertyAction("user.variant", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   461
        }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   462
        defaultLocale = getInstance(language, country, variant);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   463
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   464
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   465
    private static void initDefault(Locale.Category category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   466
        String language, region, country, variant;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   467
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   468
        case DISPLAY:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            language = AccessController.doPrivileged(
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   470
                new GetPropertyAction("user.language.display", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   471
            if ("".equals(language)) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   472
                defaultDisplayLocale = getDefault();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                country = AccessController.doPrivileged(
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   475
                    new GetPropertyAction("user.country.display", ""));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                variant = AccessController.doPrivileged(
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   477
                    new GetPropertyAction("user.variant.display", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   478
                defaultDisplayLocale = getInstance(language, country, variant);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   480
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   481
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   482
            language = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   483
                new GetPropertyAction("user.language.format", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   484
            if ("".equals(language)) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   485
                defaultFormatLocale = getDefault();
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   486
            } else {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   487
                country = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   488
                    new GetPropertyAction("user.country.format", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   489
                variant = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   490
                    new GetPropertyAction("user.variant.format", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   491
                defaultFormatLocale = getInstance(language, country, variant);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   492
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   493
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Sets the default locale for this instance of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * This does not affect the host locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * If there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * method is called with a <code>PropertyPermission("user.language", "write")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * permission before the default locale is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Since changing the default locale may affect many different areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * of functionality, this method should only be used if the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * is prepared to reinitialize locale-sensitive code running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * within the same Java Virtual Machine.
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   513
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   514
     * By setting the default locale with this method, all of the default
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   515
     * locales for each Category are also set to the specified default locale.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *        <code>checkPermission</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @throws NullPointerException if <code>newLocale</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param newLocale the new default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @see java.util.PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public static synchronized void setDefault(Locale newLocale) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   526
        setDefault(Category.DISPLAY, newLocale);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   527
        setDefault(Category.FORMAT, newLocale);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   528
        defaultLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   529
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   530
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   531
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   532
     * Sets the default locale for the specified Category for this instance
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   533
     * of the Java Virtual Machine. This does not affect the host locale.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   534
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   535
     * If there is a security manager, its checkPermission method is called
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   536
     * with a PropertyPermission("user.language", "write") permission before
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   537
     * the default locale is changed.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   538
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   539
     * The Java Virtual Machine sets the default locale during startup based
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   540
     * on the host environment. It is used by many locale-sensitive methods
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   541
     * if no locale is explicitly specified.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   542
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   543
     * Since changing the default locale may affect many different areas of
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   544
     * functionality, this method should only be used if the caller is
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   545
     * prepared to reinitialize locale-sensitive code running within the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   546
     * same Java Virtual Machine.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   547
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   548
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   549
     * @param category - the specified category to set the default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   550
     * @param newLocale - the new default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   551
     * @throws SecurityException - if a security manager exists and its
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   552
     *     checkPermission method doesn't allow the operation.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   553
     * @throws NullPointerException - if category and/or newLocale is null
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   554
     * @see SecurityManager.checkPermission(java.security.Permission)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   555
     * @see PropertyPermission
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   556
     * @see #getDefault(Locale.Category)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   557
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   558
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   559
    public static synchronized void setDefault(Locale.Category category,
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   560
        Locale newLocale) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   561
        if (category == null)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   562
            throw new NullPointerException("Category cannot be NULL");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (newLocale == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            throw new NullPointerException("Can't set default locale to NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (sm != null) sm.checkPermission(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        ("user.language", "write"));
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   569
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   570
        case DISPLAY:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   571
            defaultDisplayLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   572
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   573
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   574
            defaultFormatLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   575
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   576
        default:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   577
            assert false: "Unknown Category";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   578
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Returns an array of all installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * The returned array represents the union of locales supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * by the Java runtime environment and by installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * implementations.  It must contain at least a <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * instance equal to {@link java.util.Locale#US Locale.US}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @return An array of installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public static Locale[] getAvailableLocales() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return LocaleServiceProviderPool.getAllAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Returns a list of all 2-letter country codes defined in ISO 3166.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Can be used to create Locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public static String[] getISOCountries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (isoCountries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        String[] result = new String[isoCountries.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Returns a list of all 2-letter language codes defined in ISO 639.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Can be used to create Locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * [NOTE:  ISO 639 is not a stable standard-- some languages' codes have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * The list this function returns includes both the new and the old codes for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * languages whose codes have changed.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public static String[] getISOLanguages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (isoLanguages == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        String[] result = new String[isoLanguages.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    private static final String[] getISO2Table(String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        int len = table.length() / 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        String[] isoTable = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        for (int i = 0, j = 0; i < len; i++, j += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            isoTable[i] = table.substring(j, j + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return isoTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Returns the language code for this locale, which will either be the empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * or a lowercase ISO 639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <p>NOTE:  ISO 639 is not a stable standard-- some languages' codes have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Locale's constructor recognizes both the new and the old codes for the languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * whose codes have changed, but this function always returns the old code.  If you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * want to check for a specific language whose code has changed, don't do <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * if (locale.getLanguage().equals("he"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *    ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * </pre>Instead, do<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * if (locale.getLanguage().equals(new Locale("he", "", "").getLanguage()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *    ...</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @see #getDisplayLanguage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public String getLanguage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        return language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Returns the country/region code for this locale, which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * either be the empty string or an uppercase ISO 3166 2-letter code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @see #getDisplayCountry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public String getCountry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return country;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Returns the variant code for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @see #getDisplayVariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public String getVariant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Getter for the programmatic name of the entire locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * with the language, country and variant separated by underbars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Language is always lower case, and country is always upper case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * If the language is missing, the string will begin with an underbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * If both the language and country fields are missing, this function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * will return the empty string, even if the variant field is filled in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * (you can't have a locale with just a variant-- the variant must accompany
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * a valid language or country code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see #getDisplayName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        boolean l = language.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        boolean c = country.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        boolean v = variant.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        StringBuilder result = new StringBuilder(language);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (c||(l&&v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            result.append('_').append(country); // This may just append '_'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (v&&(l||c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            result.append('_').append(variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Returns a three-letter abbreviation for this locale's language.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * doesn't specify a language, this will be the empty string.  Otherwise, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * be a lowercase ISO 639-2/T language code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * The ISO 639-2 language codes can be found on-line at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <a href="http://www.loc.gov/standards/iso639-2/englangn.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * <code>http://www.loc.gov/standards/iso639-2/englangn.html</code>.</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @exception MissingResourceException Throws MissingResourceException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * three-letter language abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public String getISO3Language() throws MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        String language3 = getISO3Code(language, LocaleISOData.isoLanguageTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (language3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            throw new MissingResourceException("Couldn't find 3-letter language code for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    + language, "FormatData_" + toString(), "ShortLanguage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return language3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Returns a three-letter abbreviation for this locale's country.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * doesn't specify a country, this will be the empty string.  Otherwise, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * be an uppercase ISO 3166 3-letter country code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * The ISO 3166-2 country codes can be found on-line at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <a href="http://www.davros.org/misc/iso3166.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <code>http://www.davros.org/misc/iso3166.txt</code>.</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @exception MissingResourceException Throws MissingResourceException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * three-letter country abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public String getISO3Country() throws MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        String country3 = getISO3Code(country, LocaleISOData.isoCountryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (country3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            throw new MissingResourceException("Couldn't find 3-letter country code for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    + country, "FormatData_" + toString(), "ShortCountry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return country3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private static final String getISO3Code(String iso2Code, String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        int codeLength = iso2Code.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if (codeLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        int tableLength = table.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        int index = tableLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (codeLength == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            char c1 = iso2Code.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            char c2 = iso2Code.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            for (index = 0; index < tableLength; index += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                if (table.charAt(index) == c1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    && table.charAt(index + 1) == c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        return index < tableLength ? table.substring(index + 2, index + 5) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * the default locale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * value.  If the locale doesn't specify a language, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public final String getDisplayLanguage() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   766
        return getDisplayLanguage(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * If the name returned cannot be localized according to inLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public String getDisplayLanguage(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        return getDisplayString(language, inLocale, DISPLAY_LANGUAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * value.  If the locale doesn't specify a country, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public final String getDisplayCountry() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   801
        return getDisplayCountry(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * If the name returned cannot be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    public String getDisplayCountry(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return getDisplayString(country, inLocale, DISPLAY_COUNTRY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private String getDisplayString(String code, Locale inLocale, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (code.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (inLocale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            // Check whether a provider can provide an implementation that's closer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            // to the requested locale than what the Java runtime itself can provide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            LocaleServiceProviderPool pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (pool.hasProviders()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                result = pool.getLocalizedObject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                    LocaleNameGetter.INSTANCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                    inLocale, bundle, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                    type, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                result = bundle.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // just fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * user.  If possible, the name will be localized for the default locale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public final String getDisplayVariant() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   868
        return getDisplayVariant(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * user.  If possible, the name will be localized for inLocale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public String getDisplayVariant(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if (variant.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        String names[] = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        // Get the localized patterns for formatting a list, and use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        // them to format the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return formatList(names, listPattern, listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Returns a name for the locale that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * user.  This will be the values returned by getDisplayLanguage(), getDisplayCountry(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * and getDisplayVariant() assembled into a single string.  The display name will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * one of the following forms:<p><blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * language (country, variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * language (country)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * language (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * country (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * language<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * country<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * variant<p></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * depending on which fields are specified in the locale.  If the language, country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * and variant fields are all empty, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public final String getDisplayName() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   914
        return getDisplayName(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Returns a name for the locale that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * user.  This will be the values returned by getDisplayLanguage(), getDisplayCountry(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * and getDisplayVariant() assembled into a single string.  The display name will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * one of the following forms:<p><blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * language (country, variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * language (country)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * language (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * country (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * language<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * country<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * variant<p></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * depending on which fields are specified in the locale.  If the language, country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * and variant fields are all empty, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    public String getDisplayName(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        String languageName = getDisplayLanguage(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        String countryName = getDisplayCountry(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        String[] variantNames = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        // Get the localized patterns for formatting a display name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        String displayNamePattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            displayNamePattern = bundle.getString("DisplayNamePattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        // The display name consists of a main name, followed by qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        // Typically, the format is "MainName (Qualifier, Qualifier)" but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        // depends on what pattern is stored in the display locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        String   mainName       = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        String[] qualifierNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // The main name is the language, or if there is no language, the country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        // If there is neither language nor country (an anomalous situation) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        // the display name is simply the variant's display name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (languageName.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            mainName = languageName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            if (countryName.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                qualifierNames = new String[variantNames.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                qualifierNames[0] = countryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            else qualifierNames = variantNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        else if (countryName.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            mainName = countryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            qualifierNames = variantNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            return formatList(variantNames, listPattern, listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        // Create an array whose first element is the number of remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // elements.  This serves as a selector into a ChoiceFormat pattern from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // the resource.  The second and third elements are the main name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // the qualifier; if there are no qualifiers, the third element is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // unused by the format pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        Object[] displayNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            new Integer(qualifierNames.length != 0 ? 2 : 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            mainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            // We could also just call formatList() and have it handle the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            // list case, but this is more efficient, and we want it to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            // efficient since all the language-only locales will not have any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            // qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (displayNamePattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            return new MessageFormat(displayNamePattern).format(displayNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            // If we cannot get the message format pattern, then we use a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            // hard-coded pattern.  This should not occur in practice unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            // installation is missing some core files (FormatData etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            result.append((String)displayNames[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            if (displayNames.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                result.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                result.append((String)displayNames[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * Overrides Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            Locale that = (Locale)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * Override hashCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Since Locales are often used in hashtables, caches the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * for speed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        int hc = hashCodeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (hc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            hc = (language.hashCode() << 8) ^ country.hashCode() ^ (variant.hashCode() << 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            hashCodeValue = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Returns true if this Locale is equal to another object.  A Locale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * deemed equal to another Locale with identical language, country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * and variant, and unequal to all other objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @return true if this Locale is equal to the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        if (!(obj instanceof Locale))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        Locale other = (Locale) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return language == other.language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            && country == other.country
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            && variant == other.variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    // ================= privates =====================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    // XXX instance and class variables. For now keep these separate, since it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    // faster to match. Later, make into single string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @see #getLanguage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    private final String language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * @see #getCountry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    private final String country;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @see #getVariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    private final String variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * Placeholder for the object's hash code.  Always -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    private volatile int hashcode = -1;        // lazy evaluate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * Calculated hashcode to fix 4518797.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    private transient volatile int hashCodeValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    private static Locale defaultLocale = null;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1094
    private static Locale defaultDisplayLocale = null;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1095
    private static Locale defaultFormatLocale = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * Return an array of the display names of the variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @param bundle the ResourceBundle to use to get the display names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @return an array of display names, possible of zero length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        // Split the variant name into tokens separated by '_'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        StringTokenizer tokenizer = new StringTokenizer(variant, "_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        String[] names = new String[tokenizer.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        // For each variant token, lookup the display name.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        // not found, use the variant name itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        for (int i=0; i<names.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            names[i] = getDisplayString(tokenizer.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                                inLocale, DISPLAY_VARIANT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Format a list using given pattern strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * If either of the patterns is null, then a the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * formatted by concatenation with the delimiter ','.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * @param stringList the list of strings to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @param listPattern should create a MessageFormat taking 0-3 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * and formatting them into a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @param listCompositionPattern should take 2 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * and is used by composeList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @return a string representing the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        // If we have no list patterns, compose the list in a simple,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        // non-localized way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (listPattern == null || listCompositionPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            for (int i=0; i<stringList.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                if (i>0) result.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                result.append(stringList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // Compose the list down to three elements if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        if (stringList.length > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            MessageFormat format = new MessageFormat(listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            stringList = composeList(format, stringList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        // Rebuild the argument list with the list length as the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        Object[] args = new Object[stringList.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        System.arraycopy(stringList, 0, args, 1, stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        args[0] = new Integer(stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        // Format it using the pattern in the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        MessageFormat format = new MessageFormat(listPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        return format.format(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * Given a list of strings, return a list shortened to three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Shorten it by applying the given format to the first two elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @param format a format which takes two arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @param list a list of strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @return if the list is three elements or shorter, the same list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * otherwise, a new list of three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    private static String[] composeList(MessageFormat format, String[] list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (list.length <= 3) return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // Use the given format to compose the first two elements into one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        String[] listItems = { list[0], list[1] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        String newItem = format.format(listItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // Form a new list one element shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        String[] newList = new String[list.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        System.arraycopy(list, 2, newList, 1, newList.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        newList[0] = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        // Recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        return composeList(format, newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Replace the deserialized Locale object with a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * created object. Newer language codes are replaced with older ISO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * codes. The country and variant codes are replaced with internalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * String copies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    private Object readResolve() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        return getInstance(language, country, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    private static volatile String[] isoLanguages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    private static volatile String[] isoCountries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Locale needs its own, locale insensitive version of toLowerCase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * avoid circularity problems between Locale and String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * The most straightforward algorithm is used. Look at optimizations later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    private String toLowerCase(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        char[] buf = new char[str.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        for (int i = 0; i < buf.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            buf[i] = Character.toLowerCase(str.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        return new String( buf );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Locale needs its own, locale insensitive version of toUpperCase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * avoid circularity problems between Locale and String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * The most straightforward algorithm is used. Look at optimizations later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    private String toUpperCase(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        char[] buf = new char[str.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        for (int i = 0; i < buf.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            buf[i] = Character.toUpperCase(str.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        return new String( buf );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    private String convertOldISOCodes(String language) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // we accept both the old and the new ISO codes for the languages whose ISO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        // codes have changed, but we always store the OLD code, for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        language = toLowerCase(language).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (language == "he") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            return "iw";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        } else if (language == "yi") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return "ji";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        } else if (language == "id") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            return "in";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            return language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * Obtains a localized locale names from a LocaleNameProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    private static class LocaleNameGetter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        private static final LocaleNameGetter INSTANCE = new LocaleNameGetter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        public String getObject(LocaleNameProvider localeNameProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                                Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                                String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                                Object... params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            assert params.length == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            int type = (Integer)params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            String code = (String)params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            case DISPLAY_LANGUAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                return localeNameProvider.getDisplayLanguage(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            case DISPLAY_COUNTRY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                return localeNameProvider.getDisplayCountry(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            case DISPLAY_VARIANT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                return localeNameProvider.getDisplayVariant(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                assert false; // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    }
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1266
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1267
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1268
     * Enum for locale categories.  These locale categories are used to get/set
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1269
     * the default locale for the specific functionality represented by the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1270
     * category.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1271
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1272
     * @see #getDefault(Locale.Category)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1273
     * @see #setDefault(Locale.Category, Locale)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1274
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1275
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1276
    public enum Category {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1277
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1278
        /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1279
         * Category used to represent the default locale for
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1280
         * displaying user interfaces.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1281
         */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1282
        DISPLAY,
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1283
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1284
        /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1285
         * Category used to represent the default locale for
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1286
         * formatting dates, numbers, and/or currencies.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1287
         */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1288
        FORMAT,
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1289
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
}