Search in sources :

Example 46 with MessageFormat

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

the class TestMessageFormat method TestSelectOrdinal.

@Test
public void TestSelectOrdinal() {
    // Test plural & ordinal together,
    // to make sure that we get the correct cached PluralSelector for each.
    MessageFormat m = new MessageFormat("{0,plural,one{1 file}other{# files}}, " + "{0,selectordinal,one{#st file}two{#nd file}few{#rd file}other{#th file}}", ULocale.ENGLISH);
    Object[] args = new Object[] { 21 };
    FieldPosition ignore = null;
    StringBuffer result = new StringBuffer();
    assertEquals("plural-and-ordinal format(21)", "21 files, 21st file", m.format(args, result, ignore).toString());
    args[0] = 2;
    result.delete(0, result.length());
    assertEquals("plural-and-ordinal format(2) failed", "2 files, 2nd file", m.format(args, result, ignore).toString());
    args[0] = 1;
    result.delete(0, result.length());
    assertEquals("plural-and-ordinal format(1) failed", "1 file, 1st file", m.format(args, result, ignore).toString());
    args[0] = 3;
    result.delete(0, result.length());
    assertEquals("plural-and-ordinal format(3) failed", "3 files, 3rd file", m.format(args, result, ignore).toString());
}
Also used : MessageFormat(android.icu.text.MessageFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 47 with MessageFormat

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

the class TestMessageFormat method testNestedFormatsInPluralFormat.

// This tests nested Formats inside PluralFormat.
@Test
public void testNestedFormatsInPluralFormat() {
    try {
        MessageFormat msgFmt = new MessageFormat("{0, plural, one {{0, number,C''est #,##0.0# fichier}} " + "other {Ce sont # fichiers}} dans la liste.", new ULocale("fr"));
        Object[] objArray = { new Long(0) };
        HashMap objMap = new HashMap();
        objMap.put("argument", objArray[0]);
        String result = msgFmt.format(objArray);
        if (!result.equals("C'est 0,0 fichier dans la liste.")) {
            errln("PluralFormat produced wrong message string.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
}
Also used : ULocale(android.icu.util.ULocale) MessageFormat(android.icu.text.MessageFormat) HashMap(java.util.HashMap) AttributedString(java.text.AttributedString) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 48 with MessageFormat

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

the class TestMessageFormat method TestSetLocale.

@Test
public void TestSetLocale() {
    Object[] arguments = { new Double(456.83), new Date(871068000000L), "deposit" };
    StringBuffer result = new StringBuffer();
    // String formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
    String formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
    // {sfb} to get $, would need Locale::US, not Locale::ENGLISH
    // Just use unlocalized currency symbol.
    // String compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
    String compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of ";
    compareStrEng += '\u00a4';
    compareStrEng += "456.83.";
    // {sfb} to get DM, would need Locale::GERMANY, not Locale::GERMAN
    // Just use unlocalized currency symbol.
    // String compareStrGer = "At <time> on 08.08.1997, you made a deposit of 456,83 DM.";
    String compareStrGer = "At <time> on 08.08.1997, you made a deposit of ";
    compareStrGer += "456,83\u00a0";
    compareStrGer += '\u00a4';
    compareStrGer += ".";
    MessageFormat msg = new MessageFormat(formatStr, Locale.ENGLISH);
    result.setLength(0);
    FieldPosition pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStrEng, result.toString());
    msg.setLocale(Locale.ENGLISH);
    assertEquals("getLocale", Locale.ENGLISH, msg.getLocale());
    msg.setLocale(Locale.GERMAN);
    assertEquals("getLocale", Locale.GERMAN, msg.getLocale());
    msg.applyPattern(formatStr);
    result.setLength(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStrGer, result.toString());
    // Cover getULocale()
    logln("Testing set/get ULocale ...");
    msg.setLocale(ULocale.ENGLISH);
    assertEquals("getULocale", ULocale.ENGLISH, msg.getULocale());
    msg.setLocale(ULocale.GERMAN);
    assertEquals("getULocale", ULocale.GERMAN, msg.getULocale());
    msg.applyPattern(formatStr);
    result.setLength(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStrGer, result.toString());
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) Date(java.util.Date) Test(org.junit.Test)

Example 49 with MessageFormat

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

the class TestMessageFormat method testNumericFormatWithMap.

@Test
public void testNumericFormatWithMap() {
    MessageFormat mf = new MessageFormat("X:{2} Y:{1}");
    if (mf.usesNamedArguments()) {
        errln("should not use named arguments");
    }
    Map map12 = new HashMap();
    map12.put("1", "one");
    map12.put("2", "two");
    String target = "X:two Y:one";
    String result = mf.format(map12);
    if (!target.equals(result)) {
        errln("expected '" + target + "' but got '" + result + "'");
    }
    try {
        Map mapResult = mf.parseToMap(target);
        if (!map12.equals(mapResult)) {
            errln("expected " + map12 + " but got " + mapResult);
        }
    } catch (ParseException e) {
        errln("unexpected exception: " + e.getMessage());
    }
    Map map10 = new HashMap();
    map10.put("1", "one");
    map10.put("0", "zero");
    target = "X:{2} Y:one";
    result = mf.format(map10);
    if (!target.equals(result)) {
        errln("expected '" + target + "' but got '" + result + "'");
    }
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
    Map fmtMap = new HashMap();
    fmtMap.put("1", dateFormat);
    fmtMap.put("2", timeFormat);
    mf.setFormatsByArgumentName(fmtMap);
    Date date = new Date(661439820000L);
    try {
        // should fail, wrong argument type
        result = mf.format(map12);
        fail("expected exception but got '" + result + "'");
    } catch (IllegalArgumentException e) {
    // expect this
    }
    Map argMap = new HashMap();
    argMap.put("1", date);
    argMap.put("2", date);
    target = "X:5:17:00 AM Y:Dec 17, 1990";
    result = mf.format(argMap);
    if (!target.equals(result)) {
        errln("expected '" + target + "' but got '" + result + "'");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) HashMap(java.util.HashMap) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) AttributedString(java.text.AttributedString) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Date(java.util.Date) Test(org.junit.Test)

Example 50 with MessageFormat

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

the class TestMessageFormat method TestSetGetFormats.

@Test
public void TestSetGetFormats() {
    Object[] arguments = { new Double(456.83), new Date(871068000000L), "deposit" };
    StringBuffer result = new StringBuffer();
    String formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
    // original expected format result
    String compareStr = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
    // the date being German-style, but the currency being English-style
    String compareStr2 = "At <time> on 08.08.1997, you made a deposit of ";
    compareStr2 += '\u00a4';
    compareStr2 += "456.83.";
    // both date and currency formats are German-style
    String compareStr3 = "At <time> on 08.08.1997, you made a deposit of ";
    compareStr3 += "456,83\u00a0";
    compareStr3 += '\u00a4';
    compareStr3 += ".";
    MessageFormat msg = new MessageFormat(formatStr, ULocale.US);
    result.setLength(0);
    FieldPosition pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStr, result.toString());
    // constructs a Format array with a English-style Currency formatter
    // and a German-style Date formatter
    // might not meaningful, just for testing setFormatsByArgIndex
    Format[] fmts = new Format[] { NumberFormat.getCurrencyInstance(ULocale.ENGLISH), DateFormat.getDateInstance(DateFormat.DEFAULT, ULocale.GERMAN) };
    msg.setFormatsByArgumentIndex(fmts);
    result.setLength(0);
    pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStr2, result.toString());
    // Construct a German-style Currency formatter, replace the corresponding one
    // Thus both formatters should format objects with German-style
    Format newFmt = NumberFormat.getCurrencyInstance(ULocale.GERMAN);
    msg.setFormatByArgumentIndex(0, newFmt);
    result.setLength(0);
    pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStr3, result.toString());
    // verify getFormatsByArgumentIndex
    // you should got three formats by that
    // - DecimalFormat     locale: de
    // - SimpleDateFormat  locale: de
    // - null
    Format[] fmts2 = msg.getFormatsByArgumentIndex();
    assertEquals("1st subformmater: Format Class", "android.icu.text.DecimalFormat", fmts2[0].getClass().getName());
    assertEquals("1st subformmater: its Locale", ULocale.GERMAN, ((UFormat) fmts2[0]).getLocale(ULocale.VALID_LOCALE));
    assertEquals("2nd subformatter: Format Class", "android.icu.text.SimpleDateFormat", fmts2[1].getClass().getName());
    assertEquals("2nd subformmater: its Locale", ULocale.GERMAN, ((UFormat) fmts2[1]).getLocale(ULocale.VALID_LOCALE));
    assertTrue("The third subFormatter is null", null == fmts2[2]);
}
Also used : UFormat(android.icu.text.UFormat) Format(java.text.Format) MessageFormat(android.icu.text.MessageFormat) DateFormat(android.icu.text.DateFormat) NumberFormat(android.icu.text.NumberFormat) DecimalFormat(android.icu.text.DecimalFormat) ChoiceFormat(java.text.ChoiceFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) Date(java.util.Date) 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