Search in sources :

Example 16 with ICUResourceBundle

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

the class MeasureUnit method populateCache.

/**
 * Populate the MeasureUnit cache with all types from the data.
 * Population is done lazily, in response to MeasureUnit.getAvailable()
 * or other API that expects to see all of the MeasureUnits.
 *
 * <p>At static initialization time the MeasureUnits cache is populated
 * with public static instances (G_FORCE, METER_PER_SECOND_SQUARED, etc.) only.
 * Adding of others is deferred until later to avoid circular static init
 * dependencies with classes Currency and TimeUnit.
 *
 * <p>Synchronization: this function must be called from static synchronized methods only.
 *
 * @hide draft / provisional / internal are hidden on Android
 */
private static void populateCache() {
    if (cacheIsPopulated) {
        return;
    }
    cacheIsPopulated = true;
    /*  Schema:
         *
         *  units{
         *    duration{
         *      day{
         *        one{"{0} ден"}
         *        other{"{0} дена"}
         *      }
         */
    // Load the unit types.  Use English, since we know that that is a superset.
    ICUResourceBundle rb1 = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, "en");
    rb1.getAllItemsWithFallback("units", new MeasureUnitSink());
    // Load the currencies
    ICUResourceBundle rb2 = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "currencyNumericCodes", ICUResourceBundle.ICU_DATA_CLASS_LOADER);
    rb2.getAllItemsWithFallback("codeMap", new CurrencyNumericCodeSink());
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 17 with ICUResourceBundle

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

the class LocaleData method getDelimiter.

/**
 * Retrieves a delimiter string from the locale data.
 *
 * @param type      The type of delimiter string desired.  Currently,
 *                  the valid choices are QUOTATION_START, QUOTATION_END,
 *                  ALT_QUOTATION_START, or ALT_QUOTATION_END.
 * @return          The desired delimiter string.
 */
public String getDelimiter(int type) {
    ICUResourceBundle delimitersBundle = (ICUResourceBundle) bundle.get("delimiters");
    // Only some of the quotation marks may be here. So we make sure that we do a multilevel fallback.
    ICUResourceBundle stringBundle = delimitersBundle.getWithFallback(DELIMITER_TYPES[type]);
    if (noSubstitute && !bundle.isRoot() && stringBundle.isRoot()) {
        return null;
    }
    return stringBundle.getString();
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 18 with ICUResourceBundle

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

the class LocaleData method getLocaleSeparator.

/**
 * Returns LocaleDisplaySeparator for this locale.
 * @return locale display separator as a char.
 */
public String getLocaleSeparator() {
    String sub0 = "{0}";
    String sub1 = "{1}";
    ICUResourceBundle locDispBundle = (ICUResourceBundle) langBundle.get(LOCALE_DISPLAY_PATTERN);
    String localeSeparator = locDispBundle.getStringWithFallback(SEPARATOR);
    int index0 = localeSeparator.indexOf(sub0);
    int index1 = localeSeparator.indexOf(sub1);
    if (index0 >= 0 && index1 >= 0 && index0 <= index1) {
        return localeSeparator.substring(index0 + sub0.length(), index1);
    }
    return localeSeparator;
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 19 with ICUResourceBundle

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

the class LocaleData method getLocaleDisplayPattern.

/**
 * Returns LocaleDisplayPattern for this locale, e.g., {0}({1})
 * @return locale display pattern as a String.
 */
public String getLocaleDisplayPattern() {
    ICUResourceBundle locDispBundle = (ICUResourceBundle) langBundle.get(LOCALE_DISPLAY_PATTERN);
    String localeDisplayPattern = locDispBundle.getStringWithFallback(PATTERN);
    return localeDisplayPattern;
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 20 with ICUResourceBundle

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

the class MyNumberFormat method Test4122840.

/**
 * Locale data should use generic currency symbol
 *
 * 1) Make sure that all currency formats use the generic currency symbol.
 * 2) Make sure we get the same results using the generic symbol or a
 *    hard-coded one.
 */
@Test
public void Test4122840() {
    Locale[] locales = NumberFormat.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
        ICUResourceBundle rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locales[i]);
        // 
        // Get the currency pattern for this locale.  We have to fish it
        // out of the ResourceBundle directly, since DecimalFormat.toPattern
        // will return the localized symbol, not \00a4
        // 
        String pattern = rb.getStringWithFallback("NumberElements/latn/patterns/currencyFormat");
        if (pattern.indexOf('\u00A4') == -1) {
            // 'x' not "x" -- workaround bug in IBM JDK 1.4.1
            errln("Currency format for " + locales[i] + " does not contain generic currency symbol:" + pattern);
        }
        // Create a DecimalFormat using the pattern we got and format a number
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(locales[i]);
        DecimalFormat fmt1 = new DecimalFormat(pattern, symbols);
        String result1 = fmt1.format(1.111);
        // 
        // Now substitute in the locale's currency symbol and create another
        // pattern.  Replace the decimal separator with the monetary separator.
        // 
        // char decSep = symbols.getDecimalSeparator(); //The variable is never used
        char monSep = symbols.getMonetaryDecimalSeparator();
        StringBuffer buf = new StringBuffer(pattern);
        for (int j = 0; j < buf.length(); j++) {
            if (buf.charAt(j) == '\u00a4') {
                String cur = "'" + symbols.getCurrencySymbol() + "'";
                buf.replace(j, j + 1, cur);
                j += cur.length() - 1;
            }
        }
        symbols.setDecimalSeparator(monSep);
        DecimalFormat fmt2 = new DecimalFormat(buf.toString(), symbols);
        // Actual width of decimal fractions and rounding option are inherited
        // from the currency, not the pattern itself.  So we need to force
        // maximum/minimumFractionDigits and rounding option for the second
        // DecimalForamt instance.  The fix for ticket#7282 requires this test
        // code change to make it work properly.
        fmt2.setMaximumFractionDigits(fmt1.getMaximumFractionDigits());
        fmt2.setMinimumFractionDigits(fmt1.getMinimumFractionDigits());
        fmt2.setRoundingIncrement(fmt1.getRoundingIncrement());
        String result2 = fmt2.format(1.111);
        // NOTE: en_IN is a special case (ChoiceFormat currency display name)
        if (!result1.equals(result2) && !locales[i].toString().equals("en_IN")) {
            errln("Results for " + locales[i] + " differ: " + result1 + " vs " + result2);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

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