Search in sources :

Example 31 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class DateIntervalInfo method setup.

/*
     * Initialize DateIntervalInfo from calendar data
     * @param calData  calendar data
     */
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);
            // the most preferred calendar
            calendarTypeToUse = preferredCalendarTypes[0];
        }
        if (calendarTypeToUse == null) {
            // fallback
            calendarTypeToUse = "gregorian";
        }
        // 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}
    }
}
Also used : ICUException(android.icu.util.ICUException) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 32 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class DateIntervalFormat method getConcatenationPattern.

/**
 * Retrieves the concatenation DateTime pattern from the resource bundle.
 * @param locale Locale to retrieve.
 * @return Concatenation DateTime pattern.
 */
private String getConcatenationPattern(ULocale locale) {
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
    ICUResourceBundle dtPatternsRb = rb.getWithFallback("calendar/gregorian/DateTimePatterns");
    ICUResourceBundle concatenationPatternRb = (ICUResourceBundle) dtPatternsRb.get(8);
    if (concatenationPatternRb.getType() == UResourceBundle.STRING) {
        return concatenationPatternRb.getString();
    } else {
        return concatenationPatternRb.getString(0);
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 33 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class DateTimePatternGenerator method addCLDRData.

private void addCLDRData(PatternInfo returnInfo, ULocale uLocale) {
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, uLocale);
    String calendarTypeToUse = getCalendarTypeToUse(uLocale);
    // ICU4J getWithFallback does not work well when
    // 1) A nested table is an alias to /LOCALE/...
    // 2) getWithFallback is called multiple times for going down hierarchical resource path
    // #9987 resolved the issue of alias table when full path is specified in getWithFallback,
    // but there is no easy solution when the equivalent operation is done by multiple operations.
    // This issue is addressed in #9964.
    // Load append item formats.
    AppendItemFormatsSink appendItemFormatsSink = new AppendItemFormatsSink();
    try {
        rb.getAllItemsWithFallback("calendar/" + calendarTypeToUse + "/appendItems", appendItemFormatsSink);
    } catch (MissingResourceException e) {
    }
    // Load CLDR item names.
    AppendItemNamesSink appendItemNamesSink = new AppendItemNamesSink();
    try {
        rb.getAllItemsWithFallback("fields", appendItemNamesSink);
    } catch (MissingResourceException e) {
    }
    // Load the available formats from CLDR.
    AvailableFormatsSink availableFormatsSink = new AvailableFormatsSink(returnInfo);
    try {
        rb.getAllItemsWithFallback("calendar/" + calendarTypeToUse + "/availableFormats", availableFormatsSink);
    } catch (MissingResourceException e) {
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 34 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class MeasureFormat method getRangeFormat.

/**
 * Return a formatter (compiled SimpleFormatter pattern) for a range, such as "{0}–{1}".
 * @param forLocale locale to get the format for
 * @param width the format width
 * @return range formatter, such as "{0}–{1}"
 * @deprecated This API is ICU internal only.
 * @hide original deprecated declaration
 * @hide draft / provisional / internal are hidden on Android
 */
@Deprecated
public static String getRangeFormat(ULocale forLocale, FormatWidth width) {
    // TODO fix Hack for French
    if (forLocale.getLanguage().equals("fr")) {
        return getRangeFormat(ULocale.ROOT, width);
    }
    String result = localeIdToRangeFormat.get(forLocale);
    if (result == null) {
        ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, forLocale);
        ULocale realLocale = rb.getULocale();
        if (!forLocale.equals(realLocale)) {
            // if the child would inherit, then add a cache entry for it.
            result = localeIdToRangeFormat.get(forLocale);
            if (result != null) {
                localeIdToRangeFormat.put(forLocale, result);
                return result;
            }
        }
        // At this point, both the forLocale and the realLocale don't have an item
        // So we have to make one.
        NumberingSystem ns = NumberingSystem.getInstance(forLocale);
        String resultString = null;
        try {
            resultString = rb.getStringWithFallback("NumberElements/" + ns.getName() + "/miscPatterns/range");
        } catch (MissingResourceException ex) {
            resultString = rb.getStringWithFallback("NumberElements/latn/patterns/range");
        }
        result = SimpleFormatterImpl.compileToStringMinMaxArguments(resultString, new StringBuilder(), 2, 2);
        localeIdToRangeFormat.put(forLocale, result);
        if (!forLocale.equals(realLocale)) {
            localeIdToRangeFormat.put(realLocale, result);
        }
    }
    return result;
}
Also used : ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 35 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class DecimalFormatSymbols method loadData.

private static CacheData loadData(ULocale locale) {
    String nsName;
    // Attempt to set the decimal digits based on the numbering system for the requested locale.
    NumberingSystem ns = NumberingSystem.getInstance(locale);
    String[] digits = new String[10];
    if (ns != null && ns.getRadix() == 10 && !ns.isAlgorithmic() && NumberingSystem.isValidDigitString(ns.getDescription())) {
        String digitString = ns.getDescription();
        for (int i = 0, offset = 0; i < 10; i++) {
            int cp = digitString.codePointAt(offset);
            int nextOffset = offset + Character.charCount(cp);
            digits[i] = digitString.substring(offset, nextOffset);
            offset = nextOffset;
        }
        nsName = ns.getName();
    } else {
        // Default numbering system
        digits = DEF_DIGIT_STRINGS_ARRAY;
        nsName = "latn";
    }
    // Open the resource bundle and get the locale IDs.
    // TODO: Is there a better way to get the locale than making an ICUResourceBundle instance?
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
    // TODO: Determine actual and valid locale correctly.
    ULocale validLocale = rb.getULocale();
    String[] numberElements = new String[SYMBOL_KEYS.length];
    // Load using a data sink
    DecFmtDataSink sink = new DecFmtDataSink(numberElements);
    try {
        rb.getAllItemsWithFallback(NUMBER_ELEMENTS + "/" + nsName + "/" + SYMBOLS, sink);
    } catch (MissingResourceException e) {
    // The symbols don't exist for the given nsName and resource bundle.
    // Silently ignore and fall back to Latin.
    }
    // Load the Latin fallback if necessary
    boolean hasNull = false;
    for (String entry : numberElements) {
        if (entry == null) {
            hasNull = true;
            break;
        }
    }
    if (hasNull && !nsName.equals(LATIN_NUMBERING_SYSTEM)) {
        rb.getAllItemsWithFallback(NUMBER_ELEMENTS + "/" + LATIN_NUMBERING_SYSTEM + "/" + SYMBOLS, sink);
    }
    // Fill in any remaining missing values
    for (int i = 0; i < SYMBOL_KEYS.length; i++) {
        if (numberElements[i] == null) {
            numberElements[i] = SYMBOL_DEFAULTS[i];
        }
    }
    // their non-monetary counterparts.
    if (numberElements[10] == null) {
        numberElements[10] = numberElements[0];
    }
    if (numberElements[11] == null) {
        numberElements[11] = numberElements[1];
    }
    return new CacheData(validLocale, digits, numberElements);
}
Also used : ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Aggregations

ICUResourceBundle (android.icu.impl.ICUResourceBundle)42 MissingResourceException (java.util.MissingResourceException)21 ULocale (android.icu.util.ULocale)13 Test (org.junit.Test)11 UResourceBundle (android.icu.util.UResourceBundle)8 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 Map (java.util.Map)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Calendar (android.icu.util.Calendar)2 HebrewCalendar (android.icu.util.HebrewCalendar)2 UResourceBundleIterator (android.icu.util.UResourceBundleIterator)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 TreeMap (java.util.TreeMap)2 ChineseDateFormat (android.icu.text.ChineseDateFormat)1 ChineseDateFormatSymbols (android.icu.text.ChineseDateFormatSymbols)1 DateFormat (android.icu.text.DateFormat)1 DateFormatSymbols (android.icu.text.DateFormatSymbols)1