Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 46b1b1a

Browse files
committed
8320919: Clarify Locale related system properties
Reviewed-by: iris Backport-of: 376051a9be95e0e4acf3c59d0eba3e9ef8727d79
1 parent da24559 commit 46b1b1a

File tree

1 file changed

+81
-10
lines changed

1 file changed

+81
-10
lines changed

src/java.base/share/classes/java/util/Locale.java

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.io.ObjectOutputStream;
4646
import java.io.ObjectStreamField;
4747
import java.io.Serializable;
48+
import java.text.DateFormat;
4849
import java.text.MessageFormat;
4950
import java.util.concurrent.ConcurrentHashMap;
5051
import java.util.spi.LocaleNameProvider;
@@ -257,6 +258,74 @@
257258
* locales. For example, {@code Locale.US} is the {@code Locale} object
258259
* for the United States.
259260
*
261+
* <h3><a id="default_locale">Default Locale</a></h3>
262+
*
263+
* <p>The default Locale is provided for any locale-sensitive methods if no
264+
* {@code Locale} is explicitly specified as an argument, such as
265+
* {@link DateFormat#getInstance()}. The default Locale is determined at startup
266+
* of the Java runtime and established in the following three phases:
267+
* <ol>
268+
* <li>The locale-related system properties listed below are established from the
269+
* host environment. Some system properties (except for {@code user.language}) may
270+
* not have values from the host environment.
271+
* <table class="striped">
272+
* <caption style="display:none">Shows property keys and associated values</caption>
273+
* <thead>
274+
* <tr><th scope="col">Locale-related System Properties Key</th>
275+
* <th scope="col">Description</th></tr>
276+
* </thead>
277+
* <tbody>
278+
* <tr><th scope="row">{@systemProperty user.language}</th>
279+
* <td>{@link ##def_language language} for the default Locale,
280+
* such as "en" (English)</td></tr>
281+
* <tr><th scope="row">{@systemProperty user.script}</th>
282+
* <td>{@link ##def_script script} for the default Locale,
283+
* such as "Latn" (Latin)</td></tr>
284+
* <tr><th scope="row">{@systemProperty user.country}</th>
285+
* <td>{@link ##def_region country} for the default Locale,
286+
* such as "US" (United States)</td></tr>
287+
* <tr><th scope="row">{@systemProperty user.variant}</th>
288+
* <td>{@link ##def_variant variant} for the default Locale,
289+
* such as "POSIX"</td></tr>
290+
* <tr><th scope="row">{@systemProperty user.extensions}</th>
291+
* <td>{@link ##def_extensions extensions} for the default Locale,
292+
* such as "u-ca-japanese" (Japanese Calendar)</td></tr>
293+
* </tbody>
294+
* </table>
295+
* </li>
296+
* <li>The values of these system properties can be overridden by values designated
297+
* at startup time. If the overriding value of the {@code user.extensions} property
298+
* is unparsable, it is ignored. The overriding values of other properties are not
299+
* checked for syntax or validity and are used directly in the default Locale.
300+
* (Typically, system property values can be provided using the {@code -D} command-line
301+
* option of a launcher. For example, specifying {@code -Duser.extensions=foobarbaz}
302+
* results in a default Locale with no extensions, while specifying
303+
* {@code -Duser.language=foobarbaz} results in a default Locale whose language is
304+
* "foobarbaz".)
305+
* </li>
306+
* <li>The default {@code Locale} instance is constructed from the values of these
307+
* system properties.
308+
* </li>
309+
* </ol>
310+
* <p>Altering the system property values with {@link System#setProperties(Properties)}/
311+
* {@link System#setProperty(String, String)} has no effect on the default Locale.
312+
* <p>Once the default Locale is established, applications can query the default
313+
* Locale with {@link #getDefault()} and change it with {@link #setDefault(Locale)}.
314+
* If the default Locale is changed with {@link #setDefault(Locale)}, the corresponding
315+
* system properties are not altered. It is not recommended that applications read
316+
* these system properties and parse or interpret them as their values may be out of date.
317+
*
318+
* <p>There are finer-grained default Locales specific for each {@link Locale.Category}.
319+
* These category specific default Locales can be queried by {@link #getDefault(Category)},
320+
* and set by {@link #setDefault(Category, Locale)}. Construction of these category
321+
* specific default Locales are determined by the corresponding system properties,
322+
* which consist of the base system properties as listed above, suffixed by either
323+
* {@code ".display"} or {@code ".format"} depending on the category. For example,
324+
* the value of the {@code user.language.display} system property will be used in the
325+
* {@code language} part of the default Locale for the {@link Locale.Category#DISPLAY}
326+
* category. In the absence of category specific system properties, the "category-less"
327+
* system properties are used, such as {@code user.language} in the previous example.
328+
*
260329
* <h3><a id="LocaleMatching">Locale Matching</a></h3>
261330
*
262331
* <p>If an application or a system is internationalized and provides localized
@@ -983,14 +1052,14 @@ public int hashCode() {
9831052
}
9841053

9851054
/**
986-
* Gets the current value of the default locale for this instance
987-
* of the Java Virtual Machine.
1055+
* Gets the current value of the {@link ##default_locale default locale} for
1056+
* this instance of the Java Virtual Machine.
9881057
* <p>
9891058
* The Java Virtual Machine sets the default locale during startup
9901059
* based on the host environment. It is used by many locale-sensitive
9911060
* methods if no locale is explicitly specified.
9921061
* It can be changed using the
993-
* {@link #setDefault(java.util.Locale) setDefault} method.
1062+
* {@link #setDefault(Locale)} method.
9941063
*
9951064
* @return the default locale for this instance of the Java Virtual Machine
9961065
*/
@@ -1000,13 +1069,13 @@ public static Locale getDefault() {
10001069
}
10011070

10021071
/**
1003-
* Gets the current value of the default locale for the specified Category
1004-
* for this instance of the Java Virtual Machine.
1072+
* Gets the current value of the {@link ##default_locale default locale} for
1073+
* the specified Category for this instance of the Java Virtual Machine.
10051074
* <p>
10061075
* The Java Virtual Machine sets the default locale during startup based
10071076
* on the host environment. It is used by many locale-sensitive methods
10081077
* if no locale is explicitly specified. It can be changed using the
1009-
* setDefault(Locale.Category, Locale) method.
1078+
* {@link #setDefault(Locale.Category, Locale)} method.
10101079
*
10111080
* @param category the specified category to get the default locale
10121081
* @throws NullPointerException if category is null
@@ -1114,8 +1183,9 @@ private static Optional<LocaleExtensions> getDefaultExtensions(String extensions
11141183
}
11151184

11161185
/**
1117-
* Sets the default locale for this instance of the Java Virtual Machine.
1118-
* This does not affect the host locale.
1186+
* Sets the {@link ##default_locale default locale} for
1187+
* this instance of the Java Virtual Machine. This does not affect the
1188+
* host locale.
11191189
* <p>
11201190
* If there is a security manager, its {@code checkPermission}
11211191
* method is called with a {@code PropertyPermission("user.language", "write")}
@@ -1148,8 +1218,9 @@ public static synchronized void setDefault(Locale newLocale) {
11481218
}
11491219

11501220
/**
1151-
* Sets the default locale for the specified Category for this instance
1152-
* of the Java Virtual Machine. This does not affect the host locale.
1221+
* Sets the {@link ##default_locale default locale} for the specified
1222+
* Category for this instance of the Java Virtual Machine. This does
1223+
* not affect the host locale.
11531224
* <p>
11541225
* If there is a security manager, its checkPermission method is called
11551226
* with a PropertyPermission("user.language", "write") permission before

0 commit comments

Comments
 (0)