2 *******************************************************************************
3 * Copyright (C) 2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import java.util.MissingResourceException;
11 import com.ibm.icu.util.ULocale;
12 import com.ibm.icu.util.UResourceBundle;
17 * Date/time format service classes in com.ibm.icu.text packages
18 * sometimes need to access calendar internal APIs. But calendar
19 * classes are in com.ibm.icu.util package, so the package local
20 * cannot be used. This class is added in com.ibm.icu.impl
21 * package for sharing some calendar internal code for calendar
24 public class CalendarUtil {
26 private static ICUCache<String, String> CALTYPE_CACHE = new SimpleCache<String, String>();
28 private static final String CALKEY = "calendar";
29 private static final String DEFCAL = "gregorian";
32 * Returns a calendar type for the given locale.
33 * When the given locale has calendar keyword, the
34 * value of calendar keyword is returned. Otherwise,
35 * the default calendar type for the locale is returned.
36 * @param loc The locale
37 * @return Calendar type string, such as "gregorian"
39 public static String getCalendarType(ULocale loc) {
40 String calType = null;
42 calType = loc.getKeywordValue(CALKEY);
43 if (calType != null) {
47 String baseLoc = loc.getBaseName();
50 calType = CALTYPE_CACHE.get(baseLoc);
51 if (calType != null) {
55 // Canonicalize, so grandfathered variant will be transformed to keywords
56 ULocale canonical = ULocale.createCanonical(loc.toString());
57 calType = canonical.getKeywordValue("calendar");
59 if (calType == null) {
60 // When calendar keyword is not available, use the locale's
61 // region to get the default calendar type
62 String region = canonical.getCountry();
63 if (region.length() == 0) {
64 ULocale fullLoc = ULocale.addLikelySubtags(canonical);
65 region = fullLoc.getCountry();
68 // Read supplementalData to get the default calendar type for
69 // the locale's region
71 UResourceBundle rb = UResourceBundle.getBundleInstance(
72 ICUResourceBundle.ICU_BASE_NAME,
74 ICUResourceBundle.ICU_DATA_CLASS_LOADER);
75 UResourceBundle calPref = rb.get("calendarPreferenceData");
76 UResourceBundle order = null;
78 order = calPref.get(region);
79 } catch (MissingResourceException mre) {
80 // use "001" as fallback
81 order = calPref.get("001");
83 // the first calendar type is the default for the region
84 calType = order.getString(0);
85 } catch (MissingResourceException mre) {
89 if (calType == null) {
90 // Use "gregorian" as the last resort fallback.
95 // Cache the resolved value for the next time
96 CALTYPE_CACHE.put(baseLoc, calType);