Search in sources :

Example 1 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class RelativeDateFormat method initializeCombinedFormat.

private MessageFormat initializeCombinedFormat(Calendar cal, ULocale locale) {
    String pattern;
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
    String resourcePath = "calendar/" + cal.getType() + "/DateTimePatterns";
    ICUResourceBundle patternsRb = rb.findWithFallback(resourcePath);
    if (patternsRb == null && !cal.getType().equals("gregorian")) {
        // Try again with gregorian, if not already attempted.
        patternsRb = rb.findWithFallback("calendar/gregorian/DateTimePatterns");
    }
    if (patternsRb == null || patternsRb.getSize() < 9) {
        // Undefined or too few elements.
        pattern = "{1} {0}";
    } else {
        int glueIndex = 8;
        if (patternsRb.getSize() >= 13) {
            if (fDateStyle >= DateFormat.FULL && fDateStyle <= DateFormat.SHORT) {
                glueIndex += fDateStyle + 1;
            } else if (fDateStyle >= DateFormat.RELATIVE_FULL && fDateStyle <= DateFormat.RELATIVE_SHORT) {
                glueIndex += fDateStyle + 1 - DateFormat.RELATIVE;
            }
        }
        int elementType = patternsRb.get(glueIndex).getType();
        if (elementType == UResourceBundle.ARRAY) {
            pattern = patternsRb.get(glueIndex).getString(0);
        } else {
            pattern = patternsRb.getString(glueIndex);
        }
    }
    combinedFormatHasDateAtStart = pattern.startsWith("{1}");
    fCombinedFormat = new MessageFormat(pattern, locale);
    return fCombinedFormat;
}
Also used : MessageFormat(android.icu.text.MessageFormat)

Example 2 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class PluralFormatUnitTest method TestExtendedPluralFormat.

@Test
public void TestExtendedPluralFormat() {
    String[] targets = { "There are no widgets.", "There is one widget.", "There is a bling widget and one other widget.", "There is a bling widget and 2 other widgets.", "There is a bling widget and 3 other widgets.", "Widgets, five (5-1=4) there be.", "There is a bling widget and 5 other widgets.", "There is a bling widget and 6 other widgets." };
    String pluralStyle = "offset:1.0 " + "=0 {There are no widgets.} " + "=1.0 {There is one widget.} " + "=5 {Widgets, five (5-1=#) there be.} " + "one {There is a bling widget and one other widget.} " + "other {There is a bling widget and # other widgets.}";
    PluralFormat pf = new PluralFormat(ULocale.ENGLISH, pluralStyle);
    MessageFormat mf = new MessageFormat("{0,plural," + pluralStyle + "}", ULocale.ENGLISH);
    Integer[] args = new Integer[1];
    for (int i = 0; i <= 7; ++i) {
        String result = pf.format(i);
        assertEquals("PluralFormat.format(value " + i + ")", targets[i], result);
        args[0] = i;
        result = mf.format(args);
        assertEquals("MessageFormat.format(value " + i + ")", targets[i], result);
    }
    // Try explicit values after keywords.
    pf.applyPattern("other{zz}other{yy}one{xx}one{ww}=1{vv}=1{uu}");
    assertEquals("should find first matching *explicit* value", "vv", pf.format(1));
}
Also used : MessageFormat(android.icu.text.MessageFormat) PluralFormat(android.icu.text.PluralFormat) Test(org.junit.Test)

Example 3 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class PluralFormatUnitTest method TestPattern.

@Test
public void TestPattern() {
    Object[] args = { "acme", null };
    {
        // ICU 4.8 PluralFormat does not trim() its pattern any more.
        // None of the other *Format classes do.
        String pat = "  one {one ''widget} other {# widgets}  ";
        PluralFormat pf = new PluralFormat(pat);
        assertEquals("should not trim() the pattern", pat, pf.toPattern());
    }
    MessageFormat pfmt = new MessageFormat("The disk ''{0}'' contains {1, plural,  one {one ''''{1, number, #.0}'''' widget} other {# widgets}}.");
    logln("");
    for (int i = 0; i < 3; ++i) {
        args[1] = new Integer(i);
        logln(pfmt.format(args));
    }
    /* ICU 4.8 returns null instead of a choice/plural/select Format object
         * (because it does not create an object for any "complex" argument).
        PluralFormat pf = (PluralFormat)pfmt.getFormatsByArgumentIndex()[1];
        logln(pf.toPattern());
         */
    logln(pfmt.toPattern());
    MessageFormat pfmt2 = new MessageFormat(pfmt.toPattern());
    assertEquals("message formats are equal", pfmt, pfmt2);
}
Also used : MessageFormat(android.icu.text.MessageFormat) PluralFormat(android.icu.text.PluralFormat) Test(org.junit.Test)

Example 4 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4114739.

/* @bug 4114739 (FIX and add javadoc)
     * MessageFormat.format has undocumented behavior about empty format objects.
     */
@Test
public void Test4114739() {
    MessageFormat mf = new MessageFormat("<{0}>");
    Object[] objs1 = null;
    Object[] objs2 = {};
    Object[] objs3 = { null };
    try {
        logln("pattern: \"" + mf.toPattern() + "\"");
        log("format(null) : ");
        logln("\"" + mf.format(objs1) + "\"");
        log("format({})   : ");
        logln("\"" + mf.format(objs2) + "\"");
        log("format({null}) :");
        logln("\"" + mf.format(objs3) + "\"");
    } catch (Exception e) {
        errln("Exception thrown for null argument tests.");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4120552.

/* @bug 4120552
     * MessageFormat.parse incorrectly sets errorIndex.
     */
@Test
public void Test4120552() {
    MessageFormat mf = new MessageFormat("pattern");
    String[] texts = { "pattern", "pat", "1234" };
    logln("pattern: \"" + mf.toPattern() + "\"");
    for (int i = 0; i < texts.length; i++) {
        ParsePosition pp = new ParsePosition(0);
        Object[] objs = mf.parse(texts[i], pp);
        log("  text for parsing: \"" + texts[i] + "\"");
        if (objs == null) {
            logln("  (incorrectly formatted string)");
            if (pp.getErrorIndex() == -1)
                errln("Incorrect error index: " + pp.getErrorIndex());
        } else {
            logln("  (correctly formatted string)");
        }
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

MessageFormat (android.icu.text.MessageFormat)61 Test (org.junit.Test)53 AttributedString (java.text.AttributedString)24 HashMap (java.util.HashMap)15 FieldPosition (java.text.FieldPosition)12 ParsePosition (java.text.ParsePosition)10 Date (java.util.Date)10 ChoiceFormat (java.text.ChoiceFormat)9 ParseException (java.text.ParseException)9 IOException (java.io.IOException)7 Map (java.util.Map)7 TreeMap (java.util.TreeMap)6 SimpleDateFormat (android.icu.text.SimpleDateFormat)5 DateFormat (android.icu.text.DateFormat)4 DecimalFormat (android.icu.text.DecimalFormat)4 NumberFormat (android.icu.text.NumberFormat)4 UFormat (android.icu.text.UFormat)3 ULocale (android.icu.util.ULocale)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3