Search in sources :

Example 1 with StandardPlural

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

the class MeasureFormat method formatMeasure.

private StringBuilder formatMeasure(Measure measure, ImmutableNumberFormat nf, StringBuilder appendTo, FieldPosition fieldPosition) {
    Number n = measure.getNumber();
    MeasureUnit unit = measure.getUnit();
    if (unit instanceof Currency) {
        return appendTo.append(currencyFormat.format(new CurrencyAmount(n, (Currency) unit), new StringBuffer(), fieldPosition));
    }
    StringBuffer formattedNumber = new StringBuffer();
    StandardPlural pluralForm = QuantityFormatter.selectPlural(n, nf.nf, rules, formattedNumber, fieldPosition);
    String formatter = getPluralFormatter(unit, formatWidth, pluralForm.ordinal());
    return QuantityFormatter.format(formatter, formattedNumber, appendTo, fieldPosition);
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) Currency(android.icu.util.Currency) StandardPlural(android.icu.impl.StandardPlural) CurrencyAmount(android.icu.util.CurrencyAmount)

Example 2 with StandardPlural

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

the class MeasureFormat method formatMeasureRange.

/**
 * Format a range of measures, such as "3.4-5.1 meters". It is the caller’s
 * responsibility to have the appropriate values in appropriate order,
 * and using the appropriate Number values.
 * <br>Note: If the format doesn’t have enough decimals, or lowValue ≥ highValue,
 * the result will be a degenerate range, like “5-5 meters”.
 * <br>Currency Units are not yet supported.
 *
 * @param lowValue low value in range
 * @param highValue high value in range
 * @return the formatted string.
 * @deprecated This API is ICU internal only.
 * @hide original deprecated declaration
 * @hide draft / provisional / internal are hidden on Android
 */
@Deprecated
public final String formatMeasureRange(Measure lowValue, Measure highValue) {
    MeasureUnit unit = lowValue.getUnit();
    if (!unit.equals(highValue.getUnit())) {
        throw new IllegalArgumentException("Units must match: " + unit + " ≠ " + highValue.getUnit());
    }
    Number lowNumber = lowValue.getNumber();
    Number highNumber = highValue.getNumber();
    final boolean isCurrency = unit instanceof Currency;
    UFieldPosition lowFpos = new UFieldPosition();
    UFieldPosition highFpos = new UFieldPosition();
    StringBuffer lowFormatted = null;
    StringBuffer highFormatted = null;
    if (isCurrency) {
        Currency currency = (Currency) unit;
        int fracDigits = currency.getDefaultFractionDigits();
        int maxFrac = numberFormat.nf.getMaximumFractionDigits();
        int minFrac = numberFormat.nf.getMinimumFractionDigits();
        if (fracDigits != maxFrac || fracDigits != minFrac) {
            DecimalFormat currentNumberFormat = (DecimalFormat) numberFormat.get();
            currentNumberFormat.setMaximumFractionDigits(fracDigits);
            currentNumberFormat.setMinimumFractionDigits(fracDigits);
            lowFormatted = currentNumberFormat.format(lowNumber, new StringBuffer(), lowFpos);
            highFormatted = currentNumberFormat.format(highNumber, new StringBuffer(), highFpos);
        }
    }
    if (lowFormatted == null) {
        lowFormatted = numberFormat.format(lowNumber, new StringBuffer(), lowFpos);
        highFormatted = numberFormat.format(highNumber, new StringBuffer(), highFpos);
    }
    final double lowDouble = lowNumber.doubleValue();
    String keywordLow = rules.select(new PluralRules.FixedDecimal(lowDouble, lowFpos.getCountVisibleFractionDigits(), lowFpos.getFractionDigits()));
    final double highDouble = highNumber.doubleValue();
    String keywordHigh = rules.select(new PluralRules.FixedDecimal(highDouble, highFpos.getCountVisibleFractionDigits(), highFpos.getFractionDigits()));
    final PluralRanges pluralRanges = Factory.getDefaultFactory().getPluralRanges(getLocale());
    StandardPlural resolvedPlural = pluralRanges.get(StandardPlural.fromString(keywordLow), StandardPlural.fromString(keywordHigh));
    String rangeFormatter = getRangeFormat(getLocale(), formatWidth);
    String formattedNumber = SimpleFormatterImpl.formatCompiledPattern(rangeFormatter, lowFormatted, highFormatted);
    if (isCurrency) {
        // Nasty hack
        // have to call this for the side effect
        currencyFormat.format(1d);
        Currency currencyUnit = (Currency) unit;
        StringBuilder result = new StringBuilder();
        appendReplacingCurrency(currencyFormat.getPrefix(lowDouble >= 0), currencyUnit, resolvedPlural, result);
        result.append(formattedNumber);
        appendReplacingCurrency(currencyFormat.getSuffix(highDouble >= 0), currencyUnit, resolvedPlural, result);
        return result.toString();
    // StringBuffer buffer = new StringBuffer();
    // CurrencyAmount currencyLow = (CurrencyAmount) lowValue;
    // CurrencyAmount currencyHigh = (CurrencyAmount) highValue;
    // FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
    // currencyFormat.format(currencyLow, buffer, pos);
    // int startOfInteger = pos.getBeginIndex();
    // StringBuffer buffer2 = new StringBuffer();
    // FieldPosition pos2 = new FieldPosition(0);
    // currencyFormat.format(currencyHigh, buffer2, pos2);
    } else {
        String formatter = getPluralFormatter(lowValue.getUnit(), formatWidth, resolvedPlural.ordinal());
        return SimpleFormatterImpl.formatCompiledPattern(formatter, formattedNumber);
    }
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) Currency(android.icu.util.Currency) StandardPlural(android.icu.impl.StandardPlural)

Example 3 with StandardPlural

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

the class QuantityFormatter method format.

/**
 * Format formats a number with this object.
 * @param number the number to be formatted
 * @param numberFormat used to actually format the number.
 * @param pluralRules uses the number and the numberFormat to determine what plural
 *  variant to use for fetching the formatting template.
 * @return the formatted string e.g '3 apples'
 */
public String format(double number, NumberFormat numberFormat, PluralRules pluralRules) {
    String formatStr = numberFormat.format(number);
    StandardPlural p = selectPlural(number, numberFormat, pluralRules);
    SimpleFormatter formatter = templates[p.ordinal()];
    if (formatter == null) {
        formatter = templates[StandardPlural.OTHER_INDEX];
        assert formatter != null;
    }
    return formatter.format(formatStr);
}
Also used : StandardPlural(android.icu.impl.StandardPlural)

Example 4 with StandardPlural

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

the class RelativeDateTimeFormatter method format.

/**
 * Formats a relative date with a quantity such as "in 5 days" or
 * "3 months ago"
 * @param quantity The numerical amount e.g 5. This value is formatted
 * according to this object's {@link NumberFormat} object.
 * @param direction NEXT means a future relative date; LAST means a past
 * relative date.
 * @param unit the unit e.g day? month? year?
 * @return the formatted string
 * @throws IllegalArgumentException if direction is something other than
 * NEXT or LAST.
 */
public String format(double quantity, Direction direction, RelativeUnit unit) {
    if (direction != Direction.LAST && direction != Direction.NEXT) {
        throw new IllegalArgumentException("direction must be NEXT or LAST");
    }
    String result;
    int pastFutureIndex = (direction == Direction.NEXT ? 1 : 0);
    // class we must guarantee that only one thread at a time uses our numberFormat.
    synchronized (numberFormat) {
        StringBuffer formatStr = new StringBuffer();
        DontCareFieldPosition fieldPosition = DontCareFieldPosition.INSTANCE;
        StandardPlural pluralForm = QuantityFormatter.selectPlural(quantity, numberFormat, pluralRules, formatStr, fieldPosition);
        String formatter = getRelativeUnitPluralPattern(style, unit, pastFutureIndex, pluralForm);
        result = SimpleFormatterImpl.formatCompiledPattern(formatter, formatStr);
    }
    return adjustForContext(result);
}
Also used : StandardPlural(android.icu.impl.StandardPlural) DontCareFieldPosition(android.icu.impl.DontCareFieldPosition)

Example 5 with StandardPlural

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

the class PluralRangesTest method TestLocaleData.

@Test
public void TestLocaleData() {
    String[][] tests = { { "de", "other", "one", "one" }, { "xxx", "few", "few", "few" }, { "de", "one", "other", "other" }, { "de", "other", "one", "one" }, { "de", "other", "other", "other" }, { "ro", "one", "few", "few" }, { "ro", "one", "other", "other" }, { "ro", "few", "one", "few" } };
    for (String[] test : tests) {
        final ULocale locale = new ULocale(test[0]);
        final StandardPlural start = StandardPlural.fromString(test[1]);
        final StandardPlural end = StandardPlural.fromString(test[2]);
        final StandardPlural expected = StandardPlural.fromString(test[3]);
        final PluralRanges pluralRanges = Factory.getDefaultFactory().getPluralRanges(locale);
        StandardPlural actual = pluralRanges.get(start, end);
        assertEquals("Deriving range category", expected, actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) StandardPlural(android.icu.impl.StandardPlural) PluralRanges(android.icu.text.PluralRanges) Test(org.junit.Test)

Aggregations

StandardPlural (android.icu.impl.StandardPlural)7 PluralRanges (android.icu.text.PluralRanges)2 Currency (android.icu.util.Currency)2 MeasureUnit (android.icu.util.MeasureUnit)2 Test (org.junit.Test)2 DontCareFieldPosition (android.icu.impl.DontCareFieldPosition)1 CurrencyAmount (android.icu.util.CurrencyAmount)1 ULocale (android.icu.util.ULocale)1