Search in sources :

Example 21 with ICUResourceBundle

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

the class Transliterator method getDisplayName.

/**
 * Returns a name for this transliterator that is appropriate for
 * display to the user in the given locale.  This name is taken
 * from the locale resource data in the standard manner of the
 * <code>java.text</code> package.
 *
 * <p>If no localized names exist in the system resource bundles,
 * a name is synthesized using a localized
 * <code>MessageFormat</code> pattern from the resource data.  The
 * arguments to this pattern are an integer followed by one or two
 * strings.  The integer is the number of strings, either 1 or 2.
 * The strings are formed by splitting the ID for this
 * transliterator at the first '-'.  If there is no '-', then the
 * entire ID forms the only string.
 * @param inLocale the ULocale in which the display name should be
 * localized.
 * @see java.text.MessageFormat
 */
public static String getDisplayName(String id, ULocale inLocale) {
    // Resource bundle containing display name keys and the
    // RB_RULE_BASED_IDS array.
    // 
    // If we ever integrate this with the Sun JDK, the resource bundle
    // root will change to sun.text.resources.LocaleElements
    ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_TRANSLIT_BASE_NAME, inLocale);
    // Normalize the ID
    String[] stv = TransliteratorIDParser.IDtoSTV(id);
    if (stv == null) {
        // No target; malformed id
        return "";
    }
    String ID = stv[0] + '-' + stv[1];
    if (stv[2] != null && stv[2].length() > 0) {
        ID = ID + '/' + stv[2];
    }
    // Use the registered display name, if any
    String n = displayNameCache.get(new CaseInsensitiveString(ID));
    if (n != null) {
        return n;
    }
    // exists.
    try {
        return bundle.getString(RB_DISPLAY_NAME_PREFIX + ID);
    } catch (MissingResourceException e) {
    }
    try {
        // Construct the formatter first; if getString() fails
        // we'll exit the try block
        MessageFormat format = new MessageFormat(bundle.getString(RB_DISPLAY_NAME_PATTERN));
        // Construct the argument array
        Object[] args = new Object[] { Integer.valueOf(2), stv[0], stv[1] };
        // Use display names for the scripts, if they exist
        for (int j = 1; j <= 2; ++j) {
            try {
                args[j] = bundle.getString(RB_SCRIPT_DISPLAY_NAME_PREFIX + (String) args[j]);
            } catch (MissingResourceException e) {
            }
        }
        // Format it using the pattern in the resource
        return (stv[2].length() > 0) ? (format.format(args) + '/' + stv[2]) : format.format(args);
    } catch (MissingResourceException e2) {
    }
    // been deleted from the root RB_LOCALE_ELEMENTS resource.
    throw new RuntimeException();
}
Also used : MessageFormat(java.text.MessageFormat) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) CaseInsensitiveString(android.icu.util.CaseInsensitiveString) CaseInsensitiveString(android.icu.util.CaseInsensitiveString)

Example 22 with ICUResourceBundle

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

the class Calendar method getPatternData.

/**
 * Retrieves the DateTime patterns and overrides from the resource bundle and generates a
 * new PatternData object.
 * @param locale Locale to retrieve.
 * @param calType Calendar type to retrieve. If not found will fallback to gregorian.
 * @return PatternData object for this locale and calendarType.
 */
private static PatternData getPatternData(ULocale locale, String calType) {
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
    ICUResourceBundle dtPatternsRb = rb.findWithFallback("calendar/" + calType + "/DateTimePatterns");
    if (dtPatternsRb == null) {
        dtPatternsRb = rb.getWithFallback("calendar/gregorian/DateTimePatterns");
    }
    int patternsSize = dtPatternsRb.getSize();
    String[] dateTimePatterns = new String[patternsSize];
    String[] dateTimePatternsOverrides = new String[patternsSize];
    for (int i = 0; i < patternsSize; i++) {
        ICUResourceBundle concatenationPatternRb = (ICUResourceBundle) dtPatternsRb.get(i);
        switch(concatenationPatternRb.getType()) {
            case UResourceBundle.STRING:
                dateTimePatterns[i] = concatenationPatternRb.getString();
                break;
            case UResourceBundle.ARRAY:
                dateTimePatterns[i] = concatenationPatternRb.getString(0);
                dateTimePatternsOverrides[i] = concatenationPatternRb.getString(1);
                break;
        }
    }
    return new PatternData(dateTimePatterns, dateTimePatternsOverrides);
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 23 with ICUResourceBundle

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

the class CollationLoader method loadTailoring.

public static CollationTailoring loadTailoring(ULocale locale, Output<ULocale> outValidLocale) {
    // Java porting note: ICU4J getWithFallback/getStringWithFallback currently does not
    // work well when alias table is involved in a resource path, unless full path is specified.
    // For now, collation resources does not contain such data, so the code below should work fine.
    CollationTailoring root = CollationRoot.getRoot();
    String localeName = locale.getName();
    if (localeName.length() == 0 || localeName.equals("root")) {
        outValidLocale.value = ULocale.ROOT;
        return root;
    }
    UResourceBundle bundle = null;
    try {
        bundle = ICUResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, locale, ICUResourceBundle.OpenType.LOCALE_ROOT);
    } catch (MissingResourceException e) {
        outValidLocale.value = ULocale.ROOT;
        return root;
    }
    ULocale validLocale = bundle.getULocale();
    // Normalize the root locale. See
    // http://bugs.icu-project.org/trac/ticket/10715
    String validLocaleName = validLocale.getName();
    if (validLocaleName.length() == 0 || validLocaleName.equals("root")) {
        validLocale = ULocale.ROOT;
    }
    outValidLocale.value = validLocale;
    // There are zero or more tailorings in the collations table.
    UResourceBundle collations;
    try {
        collations = bundle.get("collations");
        if (collations == null) {
            return root;
        }
    } catch (MissingResourceException ignored) {
        return root;
    }
    // Fetch the collation type from the locale ID and the default type from the data.
    String type = locale.getKeywordValue("collation");
    String defaultType = "standard";
    String defT = ((ICUResourceBundle) collations).findStringWithFallback("default");
    if (defT != null) {
        defaultType = defT;
    }
    if (type == null || type.equals("default")) {
        type = defaultType;
    } else {
        type = ASCII.toLowerCase(type);
    }
    // Load the collations/type tailoring, with type fallback.
    // Java porting note: typeFallback is used for setting U_USING_DEFAULT_WARNING in
    // ICU4C, but not used by ICU4J
    // boolean typeFallback = false;
    UResourceBundle data = findWithFallback(collations, type);
    if (data == null && type.length() > 6 && type.startsWith("search")) {
        // fall back from something like "searchjl" to "search"
        // typeFallback = true;
        type = "search";
        data = findWithFallback(collations, type);
    }
    if (data == null && !type.equals(defaultType)) {
        // fall back to the default type
        // typeFallback = true;
        type = defaultType;
        data = findWithFallback(collations, type);
    }
    if (data == null && !type.equals("standard")) {
        // fall back to the "standard" type
        // typeFallback = true;
        type = "standard";
        data = findWithFallback(collations, type);
    }
    if (data == null) {
        return root;
    }
    // Is this the same as the root collator? If so, then use that instead.
    ULocale actualLocale = data.getULocale();
    // http://bugs.icu-project.org/trac/ticket/10715 ICUResourceBundle(root).getULocale() != ULocale.ROOT
    // Therefore not just if (actualLocale.equals(ULocale.ROOT) && type.equals("standard")) {
    String actualLocaleName = actualLocale.getName();
    if (actualLocaleName.length() == 0 || actualLocaleName.equals("root")) {
        actualLocale = ULocale.ROOT;
        if (type.equals("standard")) {
            return root;
        }
    }
    CollationTailoring t = new CollationTailoring(root.settings);
    t.actualLocale = actualLocale;
    // deserialize
    UResourceBundle binary = data.get("%%CollationBin");
    ByteBuffer inBytes = binary.getBinary();
    try {
        CollationDataReader.read(root, inBytes, t);
    } catch (IOException e) {
        throw new ICUUncheckedIOException("Failed to load collation tailoring data for locale:" + actualLocale + " type:" + type, e);
    }
    // Try to fetch the optional rules string.
    try {
        t.setRulesResource(data.get("Sequence"));
    } catch (MissingResourceException ignored) {
    }
    // For the valid locale, suppress the default type.
    if (!type.equals(defaultType)) {
        outValidLocale.value = validLocale.setKeywordValue("collation", type);
    }
    // For the actual locale "zh" we need to suppress pinyin instead.
    if (!actualLocale.equals(validLocale)) {
        // Opening a bundle for the actual locale should always succeed.
        UResourceBundle actualBundle = UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, actualLocale);
        defT = ((ICUResourceBundle) actualBundle).findStringWithFallback("collations/default");
        if (defT != null) {
            defaultType = defT;
        }
    }
    if (!type.equals(defaultType)) {
        t.actualLocale = t.actualLocale.setKeywordValue("collation", type);
    }
    return t;
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 24 with ICUResourceBundle

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

the class CollationLoader method loadRules.

static String loadRules(ULocale locale, String collationType) {
    UResourceBundle bundle = UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, locale);
    UResourceBundle data = ((ICUResourceBundle) bundle).getWithFallback("collations/" + ASCII.toLowerCase(collationType));
    String rules = data.getString("Sequence");
    return rules;
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 25 with ICUResourceBundle

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

the class NumberFormat method getPattern.

/**
 * Returns the pattern for the provided locale and choice.
 * @param forLocale the locale of the data.
 * @param choice the pattern format.
 * @return the pattern
 */
protected static String getPattern(ULocale forLocale, int choice) {
    /* for ISOCURRENCYSTYLE and PLURALCURRENCYSTYLE,
         * the pattern is the same as the pattern of CURRENCYSTYLE
         * but by replacing the single currency sign with
         * double currency sign or triple currency sign.
         */
    String patternKey = null;
    switch(choice) {
        case NUMBERSTYLE:
        case INTEGERSTYLE:
            patternKey = "decimalFormat";
            break;
        case CURRENCYSTYLE:
            String cfKeyValue = forLocale.getKeywordValue("cf");
            patternKey = (cfKeyValue != null && cfKeyValue.equals("account")) ? "accountingFormat" : "currencyFormat";
            break;
        case CASHCURRENCYSTYLE:
        case ISOCURRENCYSTYLE:
        case PLURALCURRENCYSTYLE:
        case STANDARDCURRENCYSTYLE:
            patternKey = "currencyFormat";
            break;
        case PERCENTSTYLE:
            patternKey = "percentFormat";
            break;
        case SCIENTIFICSTYLE:
            patternKey = "scientificFormat";
            break;
        case ACCOUNTINGCURRENCYSTYLE:
            patternKey = "accountingFormat";
            break;
        default:
            assert false;
            patternKey = "decimalFormat";
            break;
    }
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, forLocale);
    NumberingSystem ns = NumberingSystem.getInstance(forLocale);
    String result = rb.findStringWithFallback("NumberElements/" + ns.getName() + "/patterns/" + patternKey);
    if (result == null) {
        result = rb.getStringWithFallback("NumberElements/latn/patterns/" + patternKey);
    }
    return result;
}
Also used : 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