12e5b6d6dSopenharmony_ci---
22e5b6d6dSopenharmony_cilayout: default
32e5b6d6dSopenharmony_cititle: Message Formatting Examples
42e5b6d6dSopenharmony_cinav_order: 1
52e5b6d6dSopenharmony_ciparent: Formatting Messages
62e5b6d6dSopenharmony_cigrand_parent: Formatting
72e5b6d6dSopenharmony_ci---
82e5b6d6dSopenharmony_ci<!--
92e5b6d6dSopenharmony_ci© 2020 and later: Unicode, Inc. and others.
102e5b6d6dSopenharmony_ciLicense & terms of use: http://www.unicode.org/copyright.html
112e5b6d6dSopenharmony_ci-->
122e5b6d6dSopenharmony_ci
132e5b6d6dSopenharmony_ci# Message Formatting Examples
142e5b6d6dSopenharmony_ci{: .no_toc }
152e5b6d6dSopenharmony_ci
162e5b6d6dSopenharmony_ci## Contents
172e5b6d6dSopenharmony_ci{: .no_toc .text-delta }
182e5b6d6dSopenharmony_ci
192e5b6d6dSopenharmony_ci1. TOC
202e5b6d6dSopenharmony_ci{:toc}
212e5b6d6dSopenharmony_ci
222e5b6d6dSopenharmony_ci---
232e5b6d6dSopenharmony_ci
242e5b6d6dSopenharmony_ci## `MessageFormat` Class
252e5b6d6dSopenharmony_ci
262e5b6d6dSopenharmony_ciICU's `MessageFormat` class can be used to format messages in a locale-independent
272e5b6d6dSopenharmony_cimanner to localize the user interface (UI) strings.
282e5b6d6dSopenharmony_ci
292e5b6d6dSopenharmony_ci### C++
302e5b6d6dSopenharmony_ci
312e5b6d6dSopenharmony_ci```cpp
322e5b6d6dSopenharmony_ci
332e5b6d6dSopenharmony_ci/* The strings below can be isolated into a resource bundle
342e5b6d6dSopenharmony_ci* and retrieved dynamically
352e5b6d6dSopenharmony_ci*/
362e5b6d6dSopenharmony_ci#define LANGUAGE_NAMES "{0}<{1}languages {2}>\n"
372e5b6d6dSopenharmony_ci#define LANG_ATTRIB   "{0}<language id=\"{1}\" >{2}</language>\n"
382e5b6d6dSopenharmony_ci#define MONTH_NAMES "{0}<monthNames>\n"
392e5b6d6dSopenharmony_ci#define END_MONTH_NAMES "{0}</monthNames>\n"
402e5b6d6dSopenharmony_ci#define MONTH   "{0}<month id=\"{1}\">{2}</month>\n"
412e5b6d6dSopenharmony_ci#define MONTH_ABBR "{0}<monthAbbr>\n"
422e5b6d6dSopenharmony_ci#define END_MONTH_ABBR "{0}</monthAbbr>\n"
432e5b6d6dSopenharmony_ci
442e5b6d6dSopenharmony_ciUnicodeString CXMLGenerator::formatString(UnicodeString& str,UnicodeString&
452e5b6d6dSopenharmony_ciargument){
462e5b6d6dSopenharmony_ciFormattable args[] ={ argument};
472e5b6d6dSopenharmony_ciUnicodeString result;
482e5b6d6dSopenharmony_ciMessageFormat format(str,mError);
492e5b6d6dSopenharmony_ciFieldPosition fpos=0;
502e5b6d6dSopenharmony_ciformat.format(args,1, result,fpos,mError);
512e5b6d6dSopenharmony_ciif(U_FAILURE(mError)) {
522e5b6d6dSopenharmony_ci  return UnicodeString("Illegal argument");
532e5b6d6dSopenharmony_ci}
542e5b6d6dSopenharmony_ci
552e5b6d6dSopenharmony_cireturn result;
562e5b6d6dSopenharmony_ci}
572e5b6d6dSopenharmony_ci
582e5b6d6dSopenharmony_civoid CXMLGenerator::writeLanguage(UnicodeString& xmlString){
592e5b6d6dSopenharmony_ci
602e5b6d6dSopenharmony_ciUnicodeString *itemTags, *items;
612e5b6d6dSopenharmony_cichar* key="Languages";
622e5b6d6dSopenharmony_ciint32_t numItems;
632e5b6d6dSopenharmony_ci
642e5b6d6dSopenharmony_ciif(U_FAILURE(mError)) {
652e5b6d6dSopenharmony_ci  return;
662e5b6d6dSopenharmony_ci}
672e5b6d6dSopenharmony_ci
682e5b6d6dSopenharmony_cimRBundle.getTaggedArray(key,itemTags, items, numItems, mError);
692e5b6d6dSopenharmony_ciif(mError!=U_USING_DEFAULT_ERROR && U_SUCCESS(mError) &&
702e5b6d6dSopenharmony_cimError!=U_ERROR_INFO_START){
712e5b6d6dSopenharmony_ci
722e5b6d6dSopenharmony_ci  Formattable args[]={indentOffset,"",""};
732e5b6d6dSopenharmony_ci  xmlString= formatString(UnicodeString(LANGUAGE_NAMES),args,3);
742e5b6d6dSopenharmony_ci  indentOffset.append("\t");
752e5b6d6dSopenharmony_ci  for(int32_t i=0;i<numItems;i++){
762e5b6d6dSopenharmony_ci
772e5b6d6dSopenharmony_ci    args[0] = indentOffset;
782e5b6d6dSopenharmony_ci    args[1] =itemTags[i] ;
792e5b6d6dSopenharmony_ci    args[2] = items[i] ;
802e5b6d6dSopenharmony_ci    xmlString.append(formatString(UnicodeString(LANG_ATTRIB),args,3));
812e5b6d6dSopenharmony_ci  }
822e5b6d6dSopenharmony_ci
832e5b6d6dSopenharmony_ci  chopIndent();
842e5b6d6dSopenharmony_ci  args[0]=indentOffset;
852e5b6d6dSopenharmony_ci  args[1] =(UnicodeString(XML_END_SLASH));
862e5b6d6dSopenharmony_ci  args[2] = "";
872e5b6d6dSopenharmony_ci  xmlString.append(formatString(UnicodeString(LANGUAGE_NAMES),args,3));
882e5b6d6dSopenharmony_ci
892e5b6d6dSopenharmony_ci  return;
902e5b6d6dSopenharmony_ci}
912e5b6d6dSopenharmony_cimError=U_ZERO_ERROR;
922e5b6d6dSopenharmony_cixmlString.remove();
932e5b6d6dSopenharmony_ci}
942e5b6d6dSopenharmony_ci
952e5b6d6dSopenharmony_ci
962e5b6d6dSopenharmony_civoid CXMLGenerator::writeMonthNames(UnicodeString& xmlString){
972e5b6d6dSopenharmony_ci
982e5b6d6dSopenharmony_ciint32_t lNum;
992e5b6d6dSopenharmony_ciconst UnicodeString* longMonths=
1002e5b6d6dSopenharmony_cimRBundle.getStringArray("MonthNames",lNum,mError);
1012e5b6d6dSopenharmony_ciif(mError!=U_USING_DEFAULT_ERROR && mError!=U_ERROR_INFO_START && mError !=
1022e5b6d6dSopenharmony_ciU_MISSING_RESOURCE_ERROR){
1032e5b6d6dSopenharmony_ci  xmlString.append(formatString(UnicodeString(MONTH_NAMES),indentOffset));
1042e5b6d6dSopenharmony_ci  indentOffset.append("\t");
1052e5b6d6dSopenharmony_ci  for(int i=0;i<lNum;i++){
1062e5b6d6dSopenharmony_ci   char c;
1072e5b6d6dSopenharmony_ci   itoa(i+1,&c,10);
1082e5b6d6dSopenharmony_ci   Formattable args[]={indentOffset,UnicodeString(&c),longMonths[i]};
1092e5b6d6dSopenharmony_ci   xmlString.append(formatString(UnicodeString(MONTH),args,3));
1102e5b6d6dSopenharmony_ci  }
1112e5b6d6dSopenharmony_ci  chopIndent();
1122e5b6d6dSopenharmony_ci  xmlString.append(formatString(UnicodeString(END_MONTH_NAMES),indentOffset));
1132e5b6d6dSopenharmony_ci  mError=U_ZERO_ERROR;
1142e5b6d6dSopenharmony_ci  return;
1152e5b6d6dSopenharmony_ci}
1162e5b6d6dSopenharmony_cixmlString.remove();
1172e5b6d6dSopenharmony_cimError= U_ZERO_ERROR;
1182e5b6d6dSopenharmony_ci}
1192e5b6d6dSopenharmony_ci```
1202e5b6d6dSopenharmony_ci
1212e5b6d6dSopenharmony_ci### C
1222e5b6d6dSopenharmony_ci
1232e5b6d6dSopenharmony_ci```c
1242e5b6d6dSopenharmony_ci
1252e5b6d6dSopenharmony_civoid msgSample1(){
1262e5b6d6dSopenharmony_ci
1272e5b6d6dSopenharmony_ci    UChar *result, *tzID, *str;
1282e5b6d6dSopenharmony_ci    UChar pattern[100];
1292e5b6d6dSopenharmony_ci    int32_t resultLengthOut, resultlength;
1302e5b6d6dSopenharmony_ci    UCalendar *cal;
1312e5b6d6dSopenharmony_ci    UDate d1;
1322e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
1332e5b6d6dSopenharmony_ci    str=(UChar*)malloc(sizeof(UChar) * (strlen("disturbance in force") +1));
1342e5b6d6dSopenharmony_ci    u_uastrcpy(str, "disturbance in force");
1352e5b6d6dSopenharmony_ci    tzID=(UChar*)malloc(sizeof(UChar) * 4);
1362e5b6d6dSopenharmony_ci     u_uastrcpy(tzID, "PST");
1372e5b6d6dSopenharmony_ci     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
1382e5b6d6dSopenharmony_ci     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
1392e5b6d6dSopenharmony_ci     d1=ucal_getMillis(cal, &status);
1402e5b6d6dSopenharmony_ci     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet
1412e5b6d6dSopenharmony_ci{2,number,integer}");
1422e5b6d6dSopenharmony_ci     resultlength=0;
1432e5b6d6dSopenharmony_ci     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern),
1442e5b6d6dSopenharmony_ciNULL,
1452e5b6d6dSopenharmony_ciresultlength, &status, d1, str, 7);
1462e5b6d6dSopenharmony_ci     if(status==U_BUFFER_OVERFLOW_ERROR){
1472e5b6d6dSopenharmony_ci         status=U_ZERO_ERROR;
1482e5b6d6dSopenharmony_ci         resultlength=resultLengthOut+1;
1492e5b6d6dSopenharmony_ci         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
1502e5b6d6dSopenharmony_ci         u_formatMessage( "en_US", pattern, u_strlen(pattern), result,
1512e5b6d6dSopenharmony_ciresultlength, &status, d1, str, 7);
1522e5b6d6dSopenharmony_ci     }
1532e5b6d6dSopenharmony_ci     printf("%s\n",austrdup(result) ); //austrdup( a function used to convert
1542e5b6d6dSopenharmony_ciUChar* to char*)
1552e5b6d6dSopenharmony_ci     free(tzID);
1562e5b6d6dSopenharmony_ci     free(str);
1572e5b6d6dSopenharmony_ci     free(result);
1582e5b6d6dSopenharmony_ci}
1592e5b6d6dSopenharmony_ci
1602e5b6d6dSopenharmony_cichar *austrdup(const UChar* unichars)
1612e5b6d6dSopenharmony_ci
1622e5b6d6dSopenharmony_ci{
1632e5b6d6dSopenharmony_ci    int   length;
1642e5b6d6dSopenharmony_ci    char *newString;
1652e5b6d6dSopenharmony_ci
1662e5b6d6dSopenharmony_ci    length    = u_strlen ( unichars );
1672e5b6d6dSopenharmony_ci    newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );
1682e5b6d6dSopenharmony_ci    if ( newString == NULL )
1692e5b6d6dSopenharmony_ci        return NULL;
1702e5b6d6dSopenharmony_ci
1712e5b6d6dSopenharmony_ci    u_austrcpy ( newString, unichars );
1722e5b6d6dSopenharmony_ci
1732e5b6d6dSopenharmony_ci    return newString;
1742e5b6d6dSopenharmony_ci}
1752e5b6d6dSopenharmony_ci
1762e5b6d6dSopenharmony_ciThis is a more practical sample which retrieves data from a resource bundle
1772e5b6d6dSopenharmony_ciand
1782e5b6d6dSopenharmony_cifeeds the data
1792e5b6d6dSopenharmony_cito u_formatMessage to produce a formatted string
1802e5b6d6dSopenharmony_ci
1812e5b6d6dSopenharmony_civoid msgSample3(){
1822e5b6d6dSopenharmony_ci
1832e5b6d6dSopenharmony_cichar* key="Languages";
1842e5b6d6dSopenharmony_ciint32_t numItems;
1852e5b6d6dSopenharmony_ci    /* This constant string can also be in the resouce bundle and retrieved at
1862e5b6d6dSopenharmony_cithe time
1872e5b6d6dSopenharmony_ci     * of formatting
1882e5b6d6dSopenharmony_ci     * eg:
1892e5b6d6dSopenharmony_ci     * UResouceBundle* myResB = ures_open("myResources",currentLocale,&err);
1902e5b6d6dSopenharmony_ci     * UChar* Lang_Attrib = ures_getString(myResb,"LANG_ATTRIB",&err);
1912e5b6d6dSopenharmony_ci     */
1922e5b6d6dSopenharmony_ci    UChar* LANG_ATTRIB   =(UChar*) "{0}<language id=\"{1}\"
1932e5b6d6dSopenharmony_ci>{2}</language>\n";
1942e5b6d6dSopenharmony_ci    UChar *result;
1952e5b6d6dSopenharmony_ci    UResourceBundle* pResB,*pDeltaResB=NULL;
1962e5b6d6dSopenharmony_ci    UErrorCode err=U_ZERO_ERROR;
1972e5b6d6dSopenharmony_ci    UChar* indentOffset = (UChar*)"\t\t\t";
1982e5b6d6dSopenharmony_ci    pResB = ures_open("","en",&err);
1992e5b6d6dSopenharmony_ciif(U_FAILURE(err)) {
2002e5b6d6dSopenharmony_ci  return;
2012e5b6d6dSopenharmony_ci}
2022e5b6d6dSopenharmony_ci
2032e5b6d6dSopenharmony_ci    ures_getByKey(pResB, key, pDeltaResB, &err);
2042e5b6d6dSopenharmony_ci
2052e5b6d6dSopenharmony_ci    if(U_SUCCESS(err)) {
2062e5b6d6dSopenharmony_ci        const UChar *value = 0;
2072e5b6d6dSopenharmony_ci        const char *key = 0;
2082e5b6d6dSopenharmony_ci        int32_t len = 0;
2092e5b6d6dSopenharmony_ci        int16_t indexR = -1;
2102e5b6d6dSopenharmony_ci        int32_t resultLength=0,resultLengthOut=0;
2112e5b6d6dSopenharmony_ci        numItems = ures_getSize(pDeltaResB);
2122e5b6d6dSopenharmony_ci        for(;numItems-->0;){
2132e5b6d6dSopenharmony_ci            key= ures_getKey(pDeltaResB);
2142e5b6d6dSopenharmony_ci            value = ures_get(pDeltaResB,key,&err);
2152e5b6d6dSopenharmony_ci            resultLength=0;
2162e5b6d6dSopenharmony_ci            resultLengthOut=u_formatMessage( "en_US", LANG_ATTRIB,
2172e5b6d6dSopenharmony_ciu_strlen(LANG_ATTRIB),
2182e5b6d6dSopenharmony_ci                                                NULL, resultLength, &err,
2192e5b6d6dSopenharmony_ciindentOffset, value, key);
2202e5b6d6dSopenharmony_ci            if(err==U_BUFFER_OVERFLOW_ERROR){
2212e5b6d6dSopenharmony_ci                 err=U_ZERO_ERROR;
2222e5b6d6dSopenharmony_ci                 resultLength=resultLengthOut+1;
2232e5b6d6dSopenharmony_ci                 result=(UChar*)realloc(result, sizeof(UChar) * resultLength);
2242e5b6d6dSopenharmony_ci                 u_formatMessage("en_US",LANG_ATTRIB,u_strlen(LANG_ATTRIB),
2252e5b6d6dSopenharmony_ci                                result,resultLength,&err,indentOffset,
2262e5b6d6dSopenharmony_ci                                value,key);
2272e5b6d6dSopenharmony_ci
2282e5b6d6dSopenharmony_ci                 printf("%s\n", austrdup(result) );
2292e5b6d6dSopenharmony_ci            }
2302e5b6d6dSopenharmony_ci
2312e5b6d6dSopenharmony_ci        }
2322e5b6d6dSopenharmony_ci
2332e5b6d6dSopenharmony_ci  return;
2342e5b6d6dSopenharmony_ci
2352e5b6d6dSopenharmony_ci}
2362e5b6d6dSopenharmony_cierr=U_ZERO_ERROR;
2372e5b6d6dSopenharmony_ci}
2382e5b6d6dSopenharmony_ci```
2392e5b6d6dSopenharmony_ci
2402e5b6d6dSopenharmony_ci### Java
2412e5b6d6dSopenharmony_ci
2422e5b6d6dSopenharmony_ci```java
2432e5b6d6dSopenharmony_ciimport com.ibm.icu.text.*;
2442e5b6d6dSopenharmony_ciimport java.util.Date;
2452e5b6d6dSopenharmony_ciimport java.text.FieldPosition;
2462e5b6d6dSopenharmony_ci
2472e5b6d6dSopenharmony_cipublic class TestMessageFormat{
2482e5b6d6dSopenharmony_ci    public void runTest() {
2492e5b6d6dSopenharmony_ci        String format = "At {1,time,::jmm} on {1,date,::dMMMM}, there was {2} on planet {3,number,integer}.";
2502e5b6d6dSopenharmony_ci        MessageFormat mf = new MessageFormat(format);
2512e5b6d6dSopenharmony_ci        Object objectsToFormat[] = { new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()), "a Disturbance in the Force", new Integer(5)};
2522e5b6d6dSopenharmony_ci        FieldPosition fp = new FieldPosition(1);
2532e5b6d6dSopenharmony_ci        StringBuffer sb = new StringBuffer();
2542e5b6d6dSopenharmony_ci        try{
2552e5b6d6dSopenharmony_ci            sb = mf.format(objectsToFormat, sb, fp);
2562e5b6d6dSopenharmony_ci            System.out.println(sb.toString());
2572e5b6d6dSopenharmony_ci        }catch(IllegalArgumentException e){
2582e5b6d6dSopenharmony_ci            System.out.println("Exception during formatting of type :" +e);
2592e5b6d6dSopenharmony_ci        }
2602e5b6d6dSopenharmony_ci    }
2612e5b6d6dSopenharmony_ci
2622e5b6d6dSopenharmony_ci    public static void main(String args[]){
2632e5b6d6dSopenharmony_ci        try{
2642e5b6d6dSopenharmony_ci            new TestMessageFormat().runTest();
2652e5b6d6dSopenharmony_ci        }catch(Exception e){
2662e5b6d6dSopenharmony_ci            System.out.println("Exception of type: "+e);
2672e5b6d6dSopenharmony_ci        }
2682e5b6d6dSopenharmony_ci    }
2692e5b6d6dSopenharmony_ci}
2702e5b6d6dSopenharmony_ci```
2712e5b6d6dSopenharmony_ci
2722e5b6d6dSopenharmony_ci## `ChoiceFormat` Class
2732e5b6d6dSopenharmony_ci
2742e5b6d6dSopenharmony_ci**Important:** The following documentation is outdated. *`ChoiceFormat` is
2752e5b6d6dSopenharmony_ciprobably not what you need. Please use `MessageFormat` with plural arguments for
2762e5b6d6dSopenharmony_ciproper plural selection, and select arguments for simple selection among a fixed
2772e5b6d6dSopenharmony_ciset of choices!*
2782e5b6d6dSopenharmony_ci
2792e5b6d6dSopenharmony_ciICU's `ChoiceFormat` class provides more flexibility than the `printf()` and `scanf()`
2802e5b6d6dSopenharmony_cistyle functions for formatting UI strings. This interface can be useful if you
2812e5b6d6dSopenharmony_ciwould like a message to change according to the number of items you are
2822e5b6d6dSopenharmony_cidisplaying. 
2832e5b6d6dSopenharmony_ci
2842e5b6d6dSopenharmony_ciNote: Some Asian languages do not have plural words or phrases.
2852e5b6d6dSopenharmony_ci
2862e5b6d6dSopenharmony_ci### C++
2872e5b6d6dSopenharmony_ci
2882e5b6d6dSopenharmony_ci```cpp
2892e5b6d6dSopenharmony_civoid msgSample1(){
2902e5b6d6dSopenharmony_ci
2912e5b6d6dSopenharmony_ci    UChar *result, *tzID, *str;
2922e5b6d6dSopenharmony_ci    UChar pattern[100];
2932e5b6d6dSopenharmony_ci    int32_t resultLengthOut, resultlength;
2942e5b6d6dSopenharmony_ci    UCalendar *cal;
2952e5b6d6dSopenharmony_ci    UDate d1;
2962e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
2972e5b6d6dSopenharmony_ci    str=(UChar*)malloc(sizeof(UChar) * (strlen("disturbance in force") +1));
2982e5b6d6dSopenharmony_ci    u_uastrcpy(str, "disturbance in force");
2992e5b6d6dSopenharmony_ci    tzID=(UChar*)malloc(sizeof(UChar) * 4);
3002e5b6d6dSopenharmony_ci     u_uastrcpy(tzID, "PST");
3012e5b6d6dSopenharmony_ci     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
3022e5b6d6dSopenharmony_ci     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
3032e5b6d6dSopenharmony_ci     d1=ucal_getMillis(cal, &status);
3042e5b6d6dSopenharmony_ci     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet
3052e5b6d6dSopenharmony_ci
3062e5b6d6dSopenharmony_ci{2,number,integer}");
3072e5b6d6dSopenharmony_ci     resultlength=0;
3082e5b6d6dSopenharmony_ci     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern),
3092e5b6d6dSopenharmony_ciNULL,
3102e5b6d6dSopenharmony_ciresultlength, &status, d1, str, 7);
3112e5b6d6dSopenharmony_ci     if(status==U_BUFFER_OVERFLOW_ERROR){
3122e5b6d6dSopenharmony_ci         status=U_ZERO_ERROR;
3132e5b6d6dSopenharmony_ci         resultlength=resultLengthOut+1;
3142e5b6d6dSopenharmony_ci         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
3152e5b6d6dSopenharmony_ci         u_formatMessage( "en_US", pattern, u_strlen(pattern), result,
3162e5b6d6dSopenharmony_ciresultlength, &status, d1, str, 7);
3172e5b6d6dSopenharmony_ci     }
3182e5b6d6dSopenharmony_ci     printf("%s\n",austrdup(result) ); //austrdup( a function used to convert
3192e5b6d6dSopenharmony_ciUChar* to char*)
3202e5b6d6dSopenharmony_ci     free(tzID);
3212e5b6d6dSopenharmony_ci     free(str);
3222e5b6d6dSopenharmony_cidouble filelimits[] = {0,1,2};
3232e5b6d6dSopenharmony_ciUErrorCode err;
3242e5b6d6dSopenharmony_ciUnicodeString filepart[] = {"are no files","is one file","are {2} files"};
3252e5b6d6dSopenharmony_ciChoiceFormat fileform(filelimits, filepart,err);
3262e5b6d6dSopenharmony_ciFormat testFormats[] = {fileform, null, NumberFormat.getInstance()};
3272e5b6d6dSopenharmony_ciMessageFormat pattform("There {0} on {1}",err);
3282e5b6d6dSopenharmony_cipattform.setFormats(testFormats);
3292e5b6d6dSopenharmony_ciFormattable testArgs[] = {null, "ADisk", null};
3302e5b6d6dSopenharmony_cifor (int i = 0; i < 4; ++i) {
3312e5b6d6dSopenharmony_ci     testArgs[0] = i;
3322e5b6d6dSopenharmony_ci     testArgs[2] = testArgs[0];
3332e5b6d6dSopenharmony_ci     FieldPosition fpos=0;
3342e5b6d6dSopenharmony_ci     format.format(args,1, result,fpos,mError);
3352e5b6d6dSopenharmony_ci     UnicodeString result = pattform.format(testArgs);
3362e5b6d6dSopenharmony_ci}
3372e5b6d6dSopenharmony_ci```
3382e5b6d6dSopenharmony_ci
3392e5b6d6dSopenharmony_ci### C
3402e5b6d6dSopenharmony_ci
3412e5b6d6dSopenharmony_ci```c
3422e5b6d6dSopenharmony_civoid msgSample2(){
3432e5b6d6dSopenharmony_ci     UChar* str;
3442e5b6d6dSopenharmony_ci     UErrorCode status = U_ZERO_ERROR;
3452e5b6d6dSopenharmony_ci     UChar *result;
3462e5b6d6dSopenharmony_ci     UChar pattern[100];
3472e5b6d6dSopenharmony_ci     int32_t resultlength,resultLengthOut, i;
3482e5b6d6dSopenharmony_ci     double testArgs[3]= { 100.0, 1.0, 0.0};
3492e5b6d6dSopenharmony_ci     str=(UChar*)malloc(sizeof(UChar) * 10);
3502e5b6d6dSopenharmony_ci     u_uastrcpy(str, "MyDisk");
3512e5b6d6dSopenharmony_ci     u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one
3522e5b6d6dSopenharmony_cifile|1<{0,number,integer} files}");
3532e5b6d6dSopenharmony_ci     for(i=0; i<3; i++){
3542e5b6d6dSopenharmony_ci         resultlength=0;
3552e5b6d6dSopenharmony_ci         resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern),
3562e5b6d6dSopenharmony_ciNULL, resultlength, &status, testArgs[i], str);
3572e5b6d6dSopenharmony_ci         if(status==U_BUFFER_OVERFLOW_ERROR){
3582e5b6d6dSopenharmony_ci             status=U_ZERO_ERROR;
3592e5b6d6dSopenharmony_ci             resultlength=resultLengthOut+1;
3602e5b6d6dSopenharmony_ci             result=(UChar*)malloc(sizeof(UChar) * resultlength);
3612e5b6d6dSopenharmony_ci             u_formatMessage( "en_US", pattern, u_strlen(pattern), result,
3622e5b6d6dSopenharmony_ciresultlength, &status, testArgs[i], str);
3632e5b6d6dSopenharmony_ci         }
3642e5b6d6dSopenharmony_ci     }
3652e5b6d6dSopenharmony_ci     printf("%s\n", austrdup(result) ); //austrdup( a function used to
3662e5b6d6dSopenharmony_ciconvert
3672e5b6d6dSopenharmony_ciUChar* to char*)
3682e5b6d6dSopenharmony_ci     free(result);
3692e5b6d6dSopenharmony_ci
3702e5b6d6dSopenharmony_ci}
3712e5b6d6dSopenharmony_ci```
3722e5b6d6dSopenharmony_ci
3732e5b6d6dSopenharmony_ci### Java
3742e5b6d6dSopenharmony_ci
3752e5b6d6dSopenharmony_ci```java
3762e5b6d6dSopenharmony_ciimport java.text.ChoiceFormat;
3772e5b6d6dSopenharmony_ciimport com.ibm.icu.text.*;
3782e5b6d6dSopenharmony_ciimport java.text.Format;
3792e5b6d6dSopenharmony_ci
3802e5b6d6dSopenharmony_cipublic class TestChoiceFormat{
3812e5b6d6dSopenharmony_ci    public void run(){
3822e5b6d6dSopenharmony_ci        double[] filelimits = {0,1,2};
3832e5b6d6dSopenharmony_ci        String[] filepart = {"are no files","is one file","are {2} files"};
3842e5b6d6dSopenharmony_ci        ChoiceFormat fileform = new ChoiceFormat(filelimits,filepart);
3852e5b6d6dSopenharmony_ci        Format[] testFormats = {fileform,null,NumberFormat.getInstance()};
3862e5b6d6dSopenharmony_ci        MessageFormat pattform = new MessageFormat("There {0} on {1}");
3872e5b6d6dSopenharmony_ci        Object[] testArgs = {null,"ADisk",null};
3882e5b6d6dSopenharmony_ci        for(int i=0;i<4;++i) {
3892e5b6d6dSopenharmony_ci            testArgs[0] = new Integer(i);
3902e5b6d6dSopenharmony_ci            testArgs[2] = testArgs[0];
3912e5b6d6dSopenharmony_ci            System.out.println(pattform.format(testArgs));
3922e5b6d6dSopenharmony_ci        }
3932e5b6d6dSopenharmony_ci    }
3942e5b6d6dSopenharmony_ci
3952e5b6d6dSopenharmony_ci    public static void main(String args[]){
3962e5b6d6dSopenharmony_ci        new TestChoiceFormat().run();
3972e5b6d6dSopenharmony_ci    }
3982e5b6d6dSopenharmony_ci}
3992e5b6d6dSopenharmony_ci```
400