Java Code Examples for com.ibm.icu.util.ULocale#getKeywordValue()

The following examples show how to use com.ibm.icu.util.ULocale#getKeywordValue() . 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: CalendarUtil.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a calendar type for the given locale.
 * When the given locale has calendar keyword, the
 * value of calendar keyword is returned.  Otherwise,
 * the default calendar type for the locale is returned.
 * @param loc The locale
 * @return Calendar type string, such as "gregorian"
 */
public static String getCalendarType(ULocale loc) {
    String calType = loc.getKeywordValue(CALKEY);
    if (calType != null) {
        return calType;
    }

    // Canonicalize, so grandfathered variant will be transformed to keywords
    ULocale canonical = ULocale.createCanonical(loc.toString());
    calType = canonical.getKeywordValue(CALKEY);
    if (calType != null) {
        return calType;
    }

    // When calendar keyword is not available, use the locale's
    // region to get the default calendar type
    String region = ULocale.getRegionForSupplementalData(canonical, true);
    return CalendarPreferences.INSTANCE.getCalendarTypeForRegion(region);
}
 
Example 2
Source File: NumberingSystem.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the default numbering system for the specified ULocale.
 * @stable ICU 4.2
 */
public static NumberingSystem getInstance(ULocale locale) {
    // Check for @numbers
    boolean nsResolved = true;
    String numbersKeyword = locale.getKeywordValue("numbers");
    if (numbersKeyword != null ) {
        for ( String keyword : OTHER_NS_KEYWORDS ) {
            if ( numbersKeyword.equals(keyword)) {
                nsResolved = false;
                break;
            }
        }
    } else {
        numbersKeyword = "default";
        nsResolved = false;
    }

    if (nsResolved) {
        NumberingSystem ns = getInstanceByName(numbersKeyword);
        if (ns != null) {
            return ns;
        }
        // If the @numbers keyword points to a bogus numbering system name,
        // we return the default for the locale.
        numbersKeyword = "default";
    }

    // Attempt to get the numbering system from the cache
    String baseName = locale.getBaseName();
    // TODO: Caching by locale+numbersKeyword could yield a large cache.
    // Try to load for each locale the mappings from OTHER_NS_KEYWORDS and default
    // to real numbering system names; can we get those from supplemental data?
    // Then look up those mappings for the locale and resolve the keyword.
    String key = baseName+"@numbers="+numbersKeyword;
    LocaleLookupData localeLookupData = new LocaleLookupData(locale, numbersKeyword);
    return cachedLocaleData.getInstance(key, localeLookupData);
}
 
Example 3
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 4
Source File: DateFormatSymbols.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes format symbols for the locale and calendar type
 * @param desiredLocale The locale whose symbols are desired.
 * @param type          The calendar type whose date format symbols are desired.
 * @stable ICU 3.0
 */
//TODO: This protected seems to be marked as @stable accidentally.
// We may need to deescalate this API to @internal.
protected void initializeData(ULocale desiredLocale, String type)
{
    String key = desiredLocale.getBaseName() + '+' + type;
    String ns = desiredLocale.getKeywordValue("numbers");
    if (ns != null && ns.length() > 0) {
        key += '+' + ns;
    }
    DateFormatSymbols dfs = DFSCACHE.getInstance(key, desiredLocale);
    initializeData(dfs);
}
 
Example 5
Source File: CollationSpecifier.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void checkKeywords(ULocale locale, Map<String, String> keywordsToValues, String scheme) {
    for (Entry<String, String> entry : keywordsToValues.entrySet()) {
        if (locale.getKeywordValue(entry.getKey()) == null ||
                !locale.getKeywordValue(entry.getKey()).equalsIgnoreCase(entry.getValue())) {
            throw new InvalidCollationKeywordException(scheme, entry.getKey(), entry.getValue());
        }
    }
}
 
Example 6
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}
    }
}