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

The following examples show how to use com.ibm.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: DateTimeFormatConstructor.java    From es6draft with MIT License 6 votes vote down vote up
private List<String> getCalendarInfo() {
    String[] values = Calendar.getKeywordValuesForLocale("calendar", locale, false);
    ArrayList<String> result = new ArrayList<>(values.length);
    for (int i = 0; i < values.length; ++i) {
        String calendarName = values[i];
        // Ignore "unknown" calendar entry in result set
        if ("unknown".equals(calendarName)) {
            continue;
        }
        CalendarAlgorithm algorithm = CalendarAlgorithm.forName(calendarName);
        result.add(algorithm.getName());
        if (algorithm.getAlias() != null) {
            result.add(algorithm.getAlias());
        }
    }
    return result;
}
 
Example 2
Source File: DateTimePatternGenerator.java    From fitnotifications 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 3
Source File: DateTimeFormatConstructor.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public String defaultValue(ExtensionKey extensionKey) {
    switch (extensionKey) {
    case ca:
        String[] values = Calendar.getKeywordValuesForLocale("calendar", locale, false);
        return CalendarAlgorithm.forName(values[0]).getName();
    case hc:
        return null;
    case nu:
        return NumberingSystem.getInstance(locale).getName();
    default:
        throw new IllegalArgumentException(extensionKey.name());
    }
}
 
Example 4
Source File: DateIntervalInfo.java    From fitnotifications 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}
    }
}