Java Code Examples for android.icu.util.Calendar#getKeywordValuesForLocale()

The following examples show how to use android.icu.util.Calendar#getKeywordValuesForLocale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateTimePatternGenerator.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private String getCalendarTypeToUse(ULocale uLocale) {
    // Get the correct calendar type
    // TODO: C++ and Java are inconsistent (see #9952).
    String calendarTypeToUse = uLocale.getKeywordValue("calendar");
    if ( calendarTypeToUse == null ) {
        String[] preferredCalendarTypes = Calendar.getKeywordValuesForLocale("calendar", uLocale, true);
        calendarTypeToUse = preferredCalendarTypes[0]; // the most preferred calendar
    }
    if ( calendarTypeToUse == null ) {
        calendarTypeToUse = "gregorian"; // fallback
    }
    return calendarTypeToUse;
}
 
Example 2
Source File: DateIntervalInfo.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void setup(ULocale locale) {
    int DEFAULT_HASH_SIZE = 19;
    fIntervalPatterns = new HashMap<String, Map<String, PatternInfo>>(DEFAULT_HASH_SIZE);
    // initialize to guard if there is no interval date format defined in
    // resource files
    fFallbackIntervalPattern = "{0} \u2013 {1}";

    try {
        // Get the correct calendar type
        String calendarTypeToUse = locale.getKeywordValue("calendar");
        if ( calendarTypeToUse == null ) {
            String[] preferredCalendarTypes =
                    Calendar.getKeywordValuesForLocale("calendar", locale, true);
            calendarTypeToUse = preferredCalendarTypes[0]; // the most preferred calendar
        }
        if ( calendarTypeToUse == null ) {
            calendarTypeToUse = "gregorian"; // fallback
        }

        // Instantiate the sink to process the data and the resource bundle
        DateIntervalSink sink = new DateIntervalSink(this);
        ICUResourceBundle resource =
                (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);

        // Get the fallback pattern
        String fallbackPattern = resource.getStringWithFallback(CALENDAR_KEY + "/" + calendarTypeToUse
                + "/" + INTERVAL_FORMATS_KEY + "/" + FALLBACK_STRING);
        setFallbackIntervalPattern(fallbackPattern);

        // Already loaded calendar types
        Set<String> loadedCalendarTypes = new HashSet<String>();

        while (calendarTypeToUse != null) {
            // Throw an exception when a loop is detected
            if (loadedCalendarTypes.contains(calendarTypeToUse)) {
                throw new ICUException("Loop in calendar type fallback: " + calendarTypeToUse);
            }

            // Register the calendar type to avoid loops
            loadedCalendarTypes.add(calendarTypeToUse);

            // Get all resources for this calendar type
            String pathToIntervalFormats = CALENDAR_KEY + "/" + calendarTypeToUse;
            resource.getAllItemsWithFallback(pathToIntervalFormats, sink);

            // Get next calendar type to load if there was an alias pointing at it
            calendarTypeToUse = sink.getAndResetNextCalendarType();
        }
    } catch ( MissingResourceException e) {
        // Will fallback to {data0} - {date1}
    }
}
 
Example 3
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestGetKeywordValuesForLocale(){

    // Android patch: Force default Gregorian calendar.
    final String[][] PREFERRED = {
        {"root",        "gregorian"},
        {"und",         "gregorian"},
        {"en_US",       "gregorian"},
        {"en_029",      "gregorian"},
        {"th_TH",       "gregorian", "buddhist"},
        {"und_TH",      "gregorian", "buddhist"},
        {"en_TH",       "gregorian", "buddhist"},
        {"he_IL",       "gregorian", "hebrew", "islamic", "islamic-civil", "islamic-tbla"},
        {"ar_EG",       "gregorian", "coptic", "islamic", "islamic-civil", "islamic-tbla"},
        {"ja",          "gregorian", "japanese"},
        {"ps_Guru_IN",  "gregorian", "indian"},
        {"th@calendar=gregorian",   "gregorian", "buddhist"},
        {"en@calendar=islamic",     "gregorian"},
        {"zh_TW",       "gregorian", "roc", "chinese"},
        {"ar_IR",       "gregorian", "persian", "islamic", "islamic-civil", "islamic-tbla"},
        {"th@rg=SAZZZZ", "gregorian", "islamic-umalqura", "islamic", "islamic-rgsa"},
    };
    // Android patch end.

    String[] ALL = Calendar.getKeywordValuesForLocale("calendar", ULocale.getDefault(), false);
    HashSet ALLSET = new HashSet();
    for (int i = 0; i < ALL.length; i++) {
        ALLSET.add(ALL[i]);
    }

    for (int i = 0; i < PREFERRED.length; i++) {
        ULocale loc = new ULocale(PREFERRED[i][0]);
        String[] expected = new String[PREFERRED[i].length - 1];
        System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length);

        String[] pref = Calendar.getKeywordValuesForLocale("calendar", loc, true);
        boolean matchPref = false;
        if (pref.length == expected.length) {
            matchPref = true;
            for (int j = 0; j < pref.length; j++) {
                if (!pref[j].equals(expected[j])) {
                    matchPref = false;
                }
            }
        }
        if (!matchPref) {
            errln("FAIL: Preferred values for locale " + loc 
                    + " got:" + Arrays.toString(pref) + " expected:" + Arrays.toString(expected));
        }

        String[] all = Calendar.getKeywordValuesForLocale("calendar", loc, false);
        boolean matchAll = false;
        if (all.length == ALLSET.size()) {
            matchAll = true;
            for (int j = 0; j < all.length; j++) {
                if (!ALLSET.contains(all[j])) {
                    matchAll = false;
                    break;
                }
            }
        }
        if (!matchAll) {
            errln("FAIL: All values for locale " + loc
                    + " got:" + Arrays.toString(all)); 
        }
    }
}