Search in sources :

Example 1 with ICUResourceBundle

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

the class SimpleDateFormat method getDefaultPattern.

/*
     * Returns the default date and time pattern (SHORT) for the default locale.
     * This method is only used by the default SimpleDateFormat constructor.
     */
private static synchronized String getDefaultPattern() {
    ULocale defaultLocale = ULocale.getDefault(Category.FORMAT);
    if (!defaultLocale.equals(cachedDefaultLocale)) {
        cachedDefaultLocale = defaultLocale;
        Calendar cal = Calendar.getInstance(cachedDefaultLocale);
        try {
            // Load the calendar data directly.
            ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, cachedDefaultLocale);
            String resourcePath = "calendar/" + cal.getType() + "/DateTimePatterns";
            ICUResourceBundle patternsRb = rb.findWithFallback(resourcePath);
            if (patternsRb == null) {
                patternsRb = rb.findWithFallback("calendar/gregorian/DateTimePatterns");
            }
            if (patternsRb == null || patternsRb.getSize() < 9) {
                cachedDefaultPattern = FALLBACKPATTERN;
            } else {
                int defaultIndex = 8;
                if (patternsRb.getSize() >= 13) {
                    defaultIndex += (SHORT + 1);
                }
                String basePattern = patternsRb.getString(defaultIndex);
                cachedDefaultPattern = SimpleFormatterImpl.formatRawPattern(basePattern, 2, 2, patternsRb.getString(SHORT), patternsRb.getString(SHORT + 4));
            }
        } catch (MissingResourceException e) {
            cachedDefaultPattern = FALLBACKPATTERN;
        }
    }
    return cachedDefaultPattern;
}
Also used : ULocale(android.icu.util.ULocale) HebrewCalendar(android.icu.util.HebrewCalendar) Calendar(android.icu.util.Calendar) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) AttributedString(java.text.AttributedString)

Example 2 with ICUResourceBundle

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

the class TimeUnitFormat method setup.

private void setup(String resourceKey, Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns, int style, Set<String> pluralKeywords) {
    // fill timeUnitToCountToPatterns from resource file
    try {
        ICUResourceBundle resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, locale);
        TimeUnitFormatSetupSink sink = new TimeUnitFormatSetupSink(timeUnitToCountToPatterns, style, pluralKeywords, locale);
        resource.getAllItemsWithFallback(resourceKey, sink);
    } catch (MissingResourceException e) {
    }
    // there should be patterns for each plural rule in each time unit.
    // For each time unit,
    // for each plural rule, following is unit pattern fall-back rule:
    // ( for example: "one" hour )
    // look for its unit pattern in its locale tree.
    // if pattern is not found in its own locale, such as de_DE,
    // look for the pattern in its parent, such as de,
    // keep looking till found or till root.
    // if the pattern is not found in root either,
    // fallback to plural count "other",
    // look for the pattern of "other" in the locale tree:
    // "de_DE" to "de" to "root".
    // If not found, fall back to value of
    // static variable DEFAULT_PATTERN_FOR_xxx, such as "{0} h".
    // 
    // Following is consistency check to create pattern for each
    // plural rule in each time unit using above fall-back rule.
    // 
    final TimeUnit[] timeUnits = TimeUnit.values();
    Set<String> keywords = pluralRules.getKeywords();
    for (int i = 0; i < timeUnits.length; ++i) {
        // for each time unit,
        // get all the patterns for each plural rule in this locale.
        final TimeUnit timeUnit = timeUnits[i];
        Map<String, Object[]> countToPatterns = timeUnitToCountToPatterns.get(timeUnit);
        if (countToPatterns == null) {
            countToPatterns = new TreeMap<String, Object[]>();
            timeUnitToCountToPatterns.put(timeUnit, countToPatterns);
        }
        for (String pluralCount : keywords) {
            if (countToPatterns.get(pluralCount) == null || countToPatterns.get(pluralCount)[style] == null) {
                // look through parents
                searchInTree(resourceKey, style, timeUnit, pluralCount, pluralCount, countToPatterns);
            }
        }
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) TimeUnit(android.icu.util.TimeUnit)

Example 3 with ICUResourceBundle

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

the class TimeUnitFormat method searchInTree.

// srcPluralCount is the original plural count on which the pattern is
// searched for.
// searchPluralCount is the fallback plural count.
// For example, to search for pattern for ""one" hour",
// "one" is the srcPluralCount,
// if the pattern is not found even in root, fallback to
// using patterns of plural count "other",
// then, "other" is the searchPluralCount.
private void searchInTree(String resourceKey, int styl, TimeUnit timeUnit, String srcPluralCount, String searchPluralCount, Map<String, Object[]> countToPatterns) {
    ULocale parentLocale = locale;
    String srcTimeUnitName = timeUnit.toString();
    while (parentLocale != null) {
        try {
            // look for pattern for srcPluralCount in locale tree
            ICUResourceBundle unitsRes = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, parentLocale);
            unitsRes = unitsRes.getWithFallback(resourceKey);
            ICUResourceBundle oneUnitRes = unitsRes.getWithFallback(srcTimeUnitName);
            String pattern = oneUnitRes.getStringWithFallback(searchPluralCount);
            final MessageFormat messageFormat = new MessageFormat(pattern, locale);
            Object[] pair = countToPatterns.get(srcPluralCount);
            if (pair == null) {
                pair = new Object[2];
                countToPatterns.put(srcPluralCount, pair);
            }
            pair[styl] = messageFormat;
            return;
        } catch (MissingResourceException e) {
        }
        parentLocale = parentLocale.getFallback();
    }
    // then search the units resource fallback from the current level to root
    if (parentLocale == null && resourceKey.equals("unitsShort")) {
        searchInTree("units", styl, timeUnit, srcPluralCount, searchPluralCount, countToPatterns);
        if (countToPatterns.get(srcPluralCount) != null && countToPatterns.get(srcPluralCount)[styl] != null) {
            return;
        }
    }
    // fall-back to plural count "other"
    if (searchPluralCount.equals("other")) {
        // set default fall back the same as the resource in root
        MessageFormat messageFormat = null;
        if (timeUnit == TimeUnit.SECOND) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_SECOND, locale);
        } else if (timeUnit == TimeUnit.MINUTE) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_MINUTE, locale);
        } else if (timeUnit == TimeUnit.HOUR) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_HOUR, locale);
        } else if (timeUnit == TimeUnit.WEEK) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_WEEK, locale);
        } else if (timeUnit == TimeUnit.DAY) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_DAY, locale);
        } else if (timeUnit == TimeUnit.MONTH) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_MONTH, locale);
        } else if (timeUnit == TimeUnit.YEAR) {
            messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_YEAR, locale);
        }
        Object[] pair = countToPatterns.get(srcPluralCount);
        if (pair == null) {
            pair = new Object[2];
            countToPatterns.put(srcPluralCount, pair);
        }
        pair[styl] = messageFormat;
    } else {
        // fall back to rule "other", and search in parents
        searchInTree(resourceKey, styl, timeUnit, srcPluralCount, "other", countToPatterns);
    }
}
Also used : ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 4 with ICUResourceBundle

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

the class MeasureFormat method loadLocaleData.

/**
 * Returns formatting data for all MeasureUnits except for currency ones.
 */
private static MeasureFormatData loadLocaleData(ULocale locale) {
    ICUResourceBundle resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, locale);
    MeasureFormatData cacheData = new MeasureFormatData();
    UnitDataSink sink = new UnitDataSink(cacheData);
    resource.getAllItemsWithFallback("", sink);
    return cacheData;
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 5 with ICUResourceBundle

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

the class DictionaryData method loadDictionaryFor.

public static DictionaryMatcher loadDictionaryFor(String dictType) throws IOException {
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BRKITR_BASE_NAME);
    String dictFileName = rb.getStringWithFallback("dictionaries/" + dictType);
    dictFileName = ICUData.ICU_BRKITR_NAME + '/' + dictFileName;
    ByteBuffer bytes = ICUBinary.getRequiredData(dictFileName);
    ICUBinary.readHeader(bytes, DATA_FORMAT_ID, null);
    int[] indexes = new int[IX_COUNT];
    // TODO: read indexes[IX_STRING_TRIE_OFFSET] first, then read a variable-length indexes[]
    for (int i = 0; i < IX_COUNT; i++) {
        indexes[i] = bytes.getInt();
    }
    int offset = indexes[IX_STRING_TRIE_OFFSET];
    Assert.assrt(offset >= (4 * IX_COUNT));
    if (offset > (4 * IX_COUNT)) {
        int diff = offset - (4 * IX_COUNT);
        ICUBinary.skipBytes(bytes, diff);
    }
    int trieType = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
    int totalSize = indexes[IX_TOTAL_SIZE] - offset;
    DictionaryMatcher m = null;
    if (trieType == TRIE_TYPE_BYTES) {
        int transform = indexes[IX_TRANSFORM];
        byte[] data = new byte[totalSize];
        bytes.get(data);
        m = new BytesDictionaryMatcher(data, transform);
    } else if (trieType == TRIE_TYPE_UCHARS) {
        Assert.assrt(totalSize % 2 == 0);
        String data = ICUBinary.getString(bytes, totalSize / 2, totalSize & 1);
        m = new CharsDictionaryMatcher(data);
    } else {
        m = null;
    }
    return m;
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) ByteBuffer(java.nio.ByteBuffer)

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