Search in sources :

Example 41 with ICUResourceBundle

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

the class ICUResourceBundleCollationTest method TestGetWithFallback.

@Test
public void TestGetWithFallback() {
    /*
        UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
        String key = bundle.getStringWithFallback("Keys/collation");
        if(!key.equals("COLLATION")){
            errln("Did not get the expected result from getStringWithFallback method.");
        }
        String type = bundle.getStringWithFallback("Types/collation/direct");
        if(!type.equals("DIRECT")){
            errln("Did not get the expected result form getStringWithFallback method.");
        }
        */
    ICUResourceBundle bundle = null;
    String key = null;
    try {
        bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, ULocale.canonicalize("de__PHONEBOOK"));
        if (!bundle.getULocale().getName().equals("de")) {
            errln("did not get the expected bundle");
        }
        key = bundle.getStringWithFallback("collations/collation/default");
        if (!key.equals("phonebook")) {
            errln("Did not get the expected result from getStringWithFallback method.");
        }
    } catch (MissingResourceException ex) {
        logln("got the expected exception");
    }
    bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, "fr_FR");
    key = bundle.getStringWithFallback("collations/default");
    if (!key.equals("standard")) {
        errln("Did not get the expected result from getStringWithFallback method.");
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Example 42 with ICUResourceBundle

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

the class DateTimeTextProvider method createStore.

private Object createStore(TemporalField field, Locale locale) {
    Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
    if (field == ERA) {
        for (TextStyle textStyle : TextStyle.values()) {
            if (textStyle.isStandalone()) {
                // Stand-alone isn't applicable to era names.
                continue;
            }
            Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", Calendar.ERA, textStyle.toCalendarStyle(), locale);
            if (displayNames != null) {
                Map<Long, String> map = new HashMap<>();
                for (Entry<String, Integer> entry : displayNames.entrySet()) {
                    map.put((long) entry.getValue(), entry.getKey());
                }
                if (!map.isEmpty()) {
                    styleMap.put(textStyle, map);
                }
            }
        }
        return new LocaleStore(styleMap);
    }
    if (field == MONTH_OF_YEAR) {
        for (TextStyle textStyle : TextStyle.values()) {
            Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", Calendar.MONTH, textStyle.toCalendarStyle(), locale);
            Map<Long, String> map = new HashMap<>();
            if (displayNames != null) {
                for (Entry<String, Integer> entry : displayNames.entrySet()) {
                    map.put((long) (entry.getValue() + 1), entry.getKey());
                }
            } else {
                // Get names one by one in that case.
                for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
                    String name;
                    name = CalendarDataUtility.retrieveJavaTimeFieldValueName("gregory", Calendar.MONTH, month, textStyle.toCalendarStyle(), locale);
                    if (name == null) {
                        break;
                    }
                    map.put((long) (month + 1), name);
                }
            }
            if (!map.isEmpty()) {
                styleMap.put(textStyle, map);
            }
        }
        return new LocaleStore(styleMap);
    }
    if (field == DAY_OF_WEEK) {
        for (TextStyle textStyle : TextStyle.values()) {
            Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", Calendar.DAY_OF_WEEK, textStyle.toCalendarStyle(), locale);
            Map<Long, String> map = new HashMap<>();
            if (displayNames != null) {
                for (Entry<String, Integer> entry : displayNames.entrySet()) {
                    map.put((long) toWeekDay(entry.getValue()), entry.getKey());
                }
            } else {
                // Get names one by one in that case.
                for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
                    String name;
                    name = CalendarDataUtility.retrieveJavaTimeFieldValueName("gregory", Calendar.DAY_OF_WEEK, wday, textStyle.toCalendarStyle(), locale);
                    if (name == null) {
                        break;
                    }
                    map.put((long) toWeekDay(wday), name);
                }
            }
            if (!map.isEmpty()) {
                styleMap.put(textStyle, map);
            }
        }
        return new LocaleStore(styleMap);
    }
    if (field == AMPM_OF_DAY) {
        for (TextStyle textStyle : TextStyle.values()) {
            if (textStyle.isStandalone()) {
                // Stand-alone isn't applicable to AM/PM.
                continue;
            }
            Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", Calendar.AM_PM, textStyle.toCalendarStyle(), locale);
            if (displayNames != null) {
                Map<Long, String> map = new HashMap<>();
                for (Entry<String, Integer> entry : displayNames.entrySet()) {
                    map.put((long) entry.getValue(), entry.getKey());
                }
                if (!map.isEmpty()) {
                    styleMap.put(textStyle, map);
                }
            }
        }
        return new LocaleStore(styleMap);
    }
    if (field == IsoFields.QUARTER_OF_YEAR) {
        // BEGIN Android-changed: Use ICU resources.
        /*
            // The order of keys must correspond to the TextStyle.values() order.
            final String[] keys = {
                "QuarterNames",
                "standalone.QuarterNames",
                "QuarterAbbreviations",
                "standalone.QuarterAbbreviations",
                "QuarterNarrows",
                "standalone.QuarterNarrows",
            };
            for (int i = 0; i < keys.length; i++) {
                String[] names = getLocalizedResource(keys[i], locale);
                if (names != null) {
                    Map<Long, String> map = new HashMap<>();
                    for (int q = 0; q < names.length; q++) {
                        map.put((long) (q + 1), names[q]);
                    }
                    styleMap.put(TextStyle.values()[i], map);
                }
            }
            */
        ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
        ICUResourceBundle quartersRb = rb.getWithFallback("calendar/gregorian/quarters");
        ICUResourceBundle formatRb = quartersRb.getWithFallback("format");
        ICUResourceBundle standaloneRb = quartersRb.getWithFallback("stand-alone");
        styleMap.put(TextStyle.FULL, extractQuarters(formatRb, "wide"));
        styleMap.put(TextStyle.FULL_STANDALONE, extractQuarters(standaloneRb, "wide"));
        styleMap.put(TextStyle.SHORT, extractQuarters(formatRb, "abbreviated"));
        styleMap.put(TextStyle.SHORT_STANDALONE, extractQuarters(standaloneRb, "abbreviated"));
        styleMap.put(TextStyle.NARROW, extractQuarters(formatRb, "narrow"));
        styleMap.put(TextStyle.NARROW_STANDALONE, extractQuarters(standaloneRb, "narrow"));
        // END Android-changed: Use ICU resources.
        return new LocaleStore(styleMap);
    }
    // null marker for map
    return "";
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ICUResourceBundle(android.icu.impl.ICUResourceBundle) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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