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