Skip to content

Commit e64f079

Browse files
committedOct 23, 2024
8342582: user.region for formatting number no longer works for 21.0.5
Reviewed-by: jlu, rriggs
1 parent 426da4b commit e64f079

File tree

3 files changed

+98
-27
lines changed

3 files changed

+98
-27
lines changed
 

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

+5-22
Original file line numberDiff line numberDiff line change
@@ -1126,28 +1126,11 @@ private static synchronized Locale getFormatLocale() {
11261126
}
11271127

11281128
private static Locale initDefault() {
1129-
String language, region, script, country, variant;
1130-
language = StaticProperty.USER_LANGUAGE;
1131-
// for compatibility, check for old user.region property
1132-
region = StaticProperty.USER_REGION;
1133-
if (!region.isEmpty()) {
1134-
// region can be of form country, country_variant, or _variant
1135-
int i = region.indexOf('_');
1136-
if (i >= 0) {
1137-
country = region.substring(0, i);
1138-
variant = region.substring(i + 1);
1139-
} else {
1140-
country = region;
1141-
variant = "";
1142-
}
1143-
script = "";
1144-
} else {
1145-
script = StaticProperty.USER_SCRIPT;
1146-
country = StaticProperty.USER_COUNTRY;
1147-
variant = StaticProperty.USER_VARIANT;
1148-
}
1149-
1150-
return getInstance(language, script, country, variant,
1129+
return getInstance(
1130+
StaticProperty.USER_LANGUAGE,
1131+
StaticProperty.USER_SCRIPT,
1132+
StaticProperty.USER_COUNTRY,
1133+
StaticProperty.USER_VARIANT,
11511134
getDefaultExtensions(StaticProperty.USER_EXTENSIONS)
11521135
.orElse(null));
11531136
}

‎src/java.base/share/classes/jdk/internal/util/StaticProperty.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -98,19 +98,33 @@ private StaticProperty() {}
9898
USER_LANGUAGE = getProperty(props, "user.language", "en");
9999
USER_LANGUAGE_DISPLAY = getProperty(props, "user.language.display", USER_LANGUAGE);
100100
USER_LANGUAGE_FORMAT = getProperty(props, "user.language.format", USER_LANGUAGE);
101-
USER_SCRIPT = getProperty(props, "user.script", "");
101+
// for compatibility, check for old user.region property
102+
USER_REGION = getProperty(props, "user.region", "");
103+
if (!USER_REGION.isEmpty()) {
104+
// region can be of form country, country_variant, or _variant
105+
int i = USER_REGION.indexOf('_');
106+
if (i >= 0) {
107+
USER_COUNTRY = USER_REGION.substring(0, i);
108+
USER_VARIANT = USER_REGION.substring(i + 1);
109+
} else {
110+
USER_COUNTRY = USER_REGION;
111+
USER_VARIANT = "";
112+
}
113+
USER_SCRIPT = "";
114+
} else {
115+
USER_SCRIPT = getProperty(props, "user.script", "");
116+
USER_COUNTRY = getProperty(props, "user.country", "");
117+
USER_VARIANT = getProperty(props, "user.variant", "");
118+
}
102119
USER_SCRIPT_DISPLAY = getProperty(props, "user.script.display", USER_SCRIPT);
103120
USER_SCRIPT_FORMAT = getProperty(props, "user.script.format", USER_SCRIPT);
104-
USER_COUNTRY = getProperty(props, "user.country", "");
105121
USER_COUNTRY_DISPLAY = getProperty(props, "user.country.display", USER_COUNTRY);
106122
USER_COUNTRY_FORMAT = getProperty(props, "user.country.format", USER_COUNTRY);
107-
USER_VARIANT = getProperty(props, "user.variant", "");
108123
USER_VARIANT_DISPLAY = getProperty(props, "user.variant.display", USER_VARIANT);
109124
USER_VARIANT_FORMAT = getProperty(props, "user.variant.format", USER_VARIANT);
110125
USER_EXTENSIONS = getProperty(props, "user.extensions", "");
111126
USER_EXTENSIONS_DISPLAY = getProperty(props, "user.extensions.display", USER_EXTENSIONS);
112127
USER_EXTENSIONS_FORMAT = getProperty(props, "user.extensions.format", USER_EXTENSIONS);
113-
USER_REGION = getProperty(props, "user.region", "");
114128
}
115129

116130
private static String getProperty(Properties props, String key) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8342582
27+
* @summary Test if "user.region" system property successfully overrides
28+
* other locale related system properties at startup
29+
* @modules jdk.localedata
30+
* @run junit/othervm
31+
* -Duser.region=DE
32+
* -Duser.language=en
33+
* -Duser.script=Latn
34+
* -Duser.country=US
35+
* -Duser.variant=FOO UserRegionTest
36+
* @run junit/othervm
37+
* -Duser.region=DE_POSIX
38+
* -Duser.language=en
39+
* -Duser.script=Latn
40+
* -Duser.country=US
41+
* -Duser.variant=FOO UserRegionTest
42+
* @run junit/othervm
43+
* -Duser.region=_POSIX
44+
* -Duser.language=en
45+
* -Duser.script=Latn
46+
* -Duser.country=US
47+
* -Duser.variant=FOO UserRegionTest
48+
*/
49+
50+
import java.util.Locale;
51+
52+
import org.junit.jupiter.api.Test;
53+
import static org.junit.jupiter.api.Assertions.assertEquals;
54+
55+
public class UserRegionTest {
56+
@Test
57+
public void testDefaultLocale() {
58+
var region = System.getProperty("user.region").split("_");
59+
var expected = Locale.of(System.getProperty("user.language"),
60+
region[0], region.length > 1 ? region[1] : "");
61+
assertEquals(expected, Locale.getDefault());
62+
assertEquals(expected, Locale.getDefault(Locale.Category.FORMAT));
63+
assertEquals(expected, Locale.getDefault(Locale.Category.DISPLAY));
64+
}
65+
66+
@Test
67+
public void testNumberFormat() {
68+
if (System.getProperty("user.region").startsWith("DE")) {
69+
assertEquals("0,50000", String.format("%.5f", 0.5f));
70+
} else {
71+
assertEquals("0.50000", String.format("%.5f", 0.5f));
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)
Please sign in to comment.