8204603: Short week days, NaN value and timezone name are inconsistent between CLDR and Java in zh_CN, zh_TW locales.
Summary: handled languageALiases for supported locales of CLDR.
Reviewed-by: naoto
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/sun/util/cldr/CLDRCalendarNameProviderImpl.java Wed Jul 04 11:55:11 2018 +0530
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.util.cldr;
+
+import static sun.util.locale.provider.LocaleProviderAdapter.Type;
+
+import java.util.Locale;
+import java.util.Set;
+import sun.util.locale.provider.AvailableLanguageTags;
+import sun.util.locale.provider.CalendarNameProviderImpl;
+import sun.util.locale.provider.LocaleProviderAdapter;
+
+
+public class CLDRCalendarNameProviderImpl extends CalendarNameProviderImpl implements AvailableLanguageTags{
+
+ public CLDRCalendarNameProviderImpl(Type type, Set<String> langtags) {
+ super(type, langtags);
+ }
+
+ @Override
+ public boolean isSupportedLocale(Locale locale) {
+ if (Locale.ROOT.equals(locale)) {
+ return true;
+ }
+ String calendarType = null;
+ if (locale.hasExtensions()) {
+ calendarType = locale.getUnicodeLocaleType("ca");
+ locale = locale.stripExtensions();
+ }
+ if (calendarType != null) {
+ switch (calendarType) {
+ case "buddhist":
+ case "japanese":
+ case "gregory":
+ case "islamic":
+ case "roc":
+ break;
+ default:
+ // Unknown calendar type
+ return false;
+ }
+ }
+ return LocaleProviderAdapter.forType(Type.CLDR).isSupportedProviderLocale(locale, langtags);
+ }
+}
--- a/src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java Tue Jul 03 15:08:00 2018 -0400
+++ b/src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java Wed Jul 04 11:55:11 2018 +0530
@@ -45,6 +45,7 @@
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.spi.CalendarDataProvider;
+import java.util.spi.CalendarNameProvider;
import java.util.spi.TimeZoneNameProvider;
import sun.util.locale.provider.JRELocaleProviderAdapter;
import sun.util.locale.provider.LocaleDataMetaInfo;
@@ -133,6 +134,24 @@
}
@Override
+ public CalendarNameProvider getCalendarNameProvider() {
+ if (calendarNameProvider == null) {
+ CalendarNameProvider provider = AccessController.doPrivileged(
+ (PrivilegedAction<CalendarNameProvider>) ()
+ -> new CLDRCalendarNameProviderImpl(
+ getAdapterType(),
+ getLanguageTagSet("FormatData")));
+
+ synchronized (this) {
+ if (calendarNameProvider == null) {
+ calendarNameProvider = provider;
+ }
+ }
+ }
+ return calendarNameProvider;
+ }
+
+ @Override
public CollatorProvider getCollatorProvider() {
return null;
}
@@ -166,7 +185,7 @@
return locs;
}
- private Locale applyAliases(Locale loc) {
+ private static Locale applyAliases(Locale loc) {
if (langAliasesMap.isEmpty()) {
langAliasesMap = baseMetaInfo.getLanguageAliasMap();
}
@@ -264,19 +283,18 @@
}
/**
- * This method returns equivalent CLDR supported locale for zh-HK,
- * no, no-NO locales so that COMPAT locales do not precede
- * those locales during ResourceBundle search path.
+ * This method returns equivalent CLDR supported locale
+ * for no, no-NO locales so that COMPAT locales do not precede
+ * those locales during ResourceBundle search path, also if an alias exists for a locale,
+ * it returns equivalent locale, e.g for zh_HK it returns zh_Hant-HK.
*/
private static Locale getEquivalentLoc(Locale locale) {
switch (locale.toString()) {
- case "zh_HK":
- return Locale.forLanguageTag("zh-Hant-HK");
case "no":
case "no_NO":
return Locale.forLanguageTag("nb");
}
- return locale;
+ return applyAliases(locale);
}
@Override
--- a/src/java.base/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java Tue Jul 03 15:08:00 2018 -0400
+++ b/src/java.base/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java Wed Jul 04 11:55:11 2018 +0530
@@ -42,8 +42,8 @@
* @author Naoto Sato
*/
public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
- private final LocaleProviderAdapter.Type type;
- private final Set<String> langtags;
+ protected final LocaleProviderAdapter.Type type;
+ protected final Set<String> langtags;
public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
this.type = type;
@@ -248,11 +248,8 @@
if (langtags.contains(locale.toLanguageTag())) {
return true;
}
- if (type == LocaleProviderAdapter.Type.JRE) {
- String oldname = locale.toString().replace('_', '-');
- return langtags.contains(oldname);
- }
- return false;
+ String oldname = locale.toString().replace('_', '-');
+ return langtags.contains(oldname);
}
@Override
--- a/src/java.base/share/classes/sun/util/locale/provider/JRELocaleProviderAdapter.java Tue Jul 03 15:08:00 2018 -0400
+++ b/src/java.base/share/classes/sun/util/locale/provider/JRELocaleProviderAdapter.java Wed Jul 04 11:55:11 2018 +0530
@@ -131,7 +131,7 @@
private volatile LocaleNameProvider localeNameProvider;
protected volatile TimeZoneNameProvider timeZoneNameProvider;
protected volatile CalendarDataProvider calendarDataProvider;
- private volatile CalendarNameProvider calendarNameProvider;
+ protected volatile CalendarNameProvider calendarNameProvider;
private volatile CalendarProvider calendarProvider;
private volatile JavaTimeDateTimePatternProvider javaTimeDateTimePatternProvider;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/sun/util/resources/cldr/Bug8204603.java Wed Jul 04 11:55:11 2018 +0530
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /*
+ * @test
+ * @bug 8204603
+ * @summary Test that correct data is retrieved for zh_CN and zh_TW locales
+ * and CLDR provider supports all locales for which aliases exist.
+ * @modules java.base/sun.util.locale.provider
+ * jdk.localedata
+ * @run main Bug8204603
+ */
+
+import java.text.DateFormatSymbols;
+import java.text.DecimalFormatSymbols;
+import java.util.Calendar;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import sun.util.locale.provider.LocaleProviderAdapter;
+
+/**
+ * This test is dependent on a particular version of CLDR data.
+ */
+public class Bug8204603 {
+
+ /**
+ * List of all locales for which CLDR provides alias Mappings. e.g alias of
+ * zh-HK is zh-Hant-HK
+ */
+ private static final List<Locale> ALIAS_LOCALES
+ = List.of(Locale.forLanguageTag("az-AZ"), Locale.forLanguageTag("bs-BA"),
+ Locale.forLanguageTag("ha-Latn-GH"), Locale.forLanguageTag("ha-Latn-NE"),
+ Locale.forLanguageTag("ha-Latn-NG"), Locale.forLanguageTag("i-lux"),
+ Locale.forLanguageTag("kk-Cyrl-KZ"), Locale.forLanguageTag("ks-Arab-IN"),
+ Locale.forLanguageTag("ky-Cyrl-KG"), Locale.forLanguageTag("lb"),
+ Locale.forLanguageTag("lb"), Locale.forLanguageTag("mn-Cyrl-MN"),
+ Locale.forLanguageTag("mo"), Locale.forLanguageTag("ms-Latn-BN"),
+ Locale.forLanguageTag("ms-Latn-MY"), Locale.forLanguageTag("ms-Latn-SG"),
+ Locale.forLanguageTag("pa-IN"), Locale.forLanguageTag("pa-PK"),
+ Locale.forLanguageTag("scc"), Locale.forLanguageTag("scr"),
+ Locale.forLanguageTag("sh"), Locale.forLanguageTag("shi-MA"),
+ Locale.forLanguageTag("sr-BA"), Locale.forLanguageTag("sr-RS"),
+ Locale.forLanguageTag("sr-XK"), Locale.forLanguageTag("tl"),
+ Locale.forLanguageTag("tzm-Latn-MA"), Locale.forLanguageTag("ug-Arab-CN"),
+ Locale.forLanguageTag("uz-AF"), Locale.forLanguageTag("uz-UZ"),
+ Locale.forLanguageTag("vai-LR"), Locale.forLanguageTag("vai-LR"),
+ Locale.forLanguageTag("yue-CN"), Locale.forLanguageTag("yue-HK"),
+ Locale.forLanguageTag("zh-CN"), Locale.forLanguageTag("zh-HK"),
+ Locale.forLanguageTag("zh-MO"), Locale.forLanguageTag("zh-SG"),
+ Locale.forLanguageTag("zh-TW"));
+
+ private static final Map<Locale, String> CALENDAR_DATA_MAP = Map.of(
+ Locale.forLanguageTag("zh-CN"), "\u5468\u65E5",
+ Locale.forLanguageTag("zh-TW"), "\u9031\u65E5");
+ private static final Map<Locale, String> NAN_DATA_MAP = Map.of(
+ Locale.forLanguageTag("zh-CN"), "NaN",
+ Locale.forLanguageTag("zh-TW"), "\u975E\u6578\u503C");
+
+ public static void main(String[] args) {
+ testCldrSupportedLocales();
+ CALENDAR_DATA_MAP.forEach((k, v) -> testCalendarData(k, v));
+ NAN_DATA_MAP.forEach((k, v) -> testNanData(k, v));
+ }
+
+ /**
+ * tests that CLDR provider should return true for alias locales.
+ *
+ */
+ private static void testCldrSupportedLocales() {
+ LocaleProviderAdapter cldr = LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.CLDR);
+ Set<Locale> availableLocs = Set.of(cldr.getAvailableLocales());
+ Set<String> langtags = new HashSet<>();
+ availableLocs.forEach(loc -> langtags.add(loc.toLanguageTag()));
+ ALIAS_LOCALES.stream().filter(loc -> !cldr.isSupportedProviderLocale(loc, langtags)).findAny()
+ .ifPresent(l -> {
+ throw new RuntimeException("Locale " + l
+ + " is not supported by CLDR locale provider");
+ });
+ }
+
+ private static void testCalendarData(Locale loc, String expected) {
+ DateFormatSymbols dfs = DateFormatSymbols.getInstance(loc);
+ String[] shortDays = dfs.getShortWeekdays();
+ String actual = shortDays[Calendar.SUNDAY];
+ if (!actual.equals(expected)) {
+ throw new RuntimeException("Calendar data mismatch for locale: "
+ + loc + ", expected is: " + expected + ", actual is: " + actual);
+ }
+ }
+
+ private static void testNanData(Locale loc, String expected) {
+ DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(loc);
+ String actual = dfs.getNaN();
+ if (!actual.equals(expected)) {
+ throw new RuntimeException("NaN mismatch for locale: "
+ + loc + ", expected is: " + expected + ", actual is: " + actual);
+ }
+ }
+}