1 /******************************************************************************** 2 * Copyright (C) 2008-2013, International Business Machines Corporation and 3 * others. All Rights Reserved. 4 ******************************************************************************* 5 */ 6 7 //! [PluralFormatExample1] 8 #include <iostream> 9 #include "unicode/plurfmt.h" 10 #include "unicode/msgfmt.h" 11 #include "unicode/ustdio.h" 12 //! [PluralFormatExample1] 13 14 using namespace std; 15 static void PluralFormatExample() { 16 17 u_printf("=============================================================================\n"); 18 u_printf(" PluralFormatExample()\n"); 19 u_printf("\n"); 20 u_printf(" Use PluralFormat and Messageformat to get Plural Form for languages below:\n"); 21 u_printf(" English, Slovenian\n"); 22 u_printf("=============================================================================\n"); 23 24 //! [PluralFormatExample] 25 UErrorCode status =U_ZERO_ERROR; 26 Locale locEn = Locale("en"); 27 Locale locSl = Locale("sl"); 28 29 UnicodeString patEn = UnicodeString("one{dog} other{dogs}"); // English 'dog' 30 UnicodeString patSl = UnicodeString("one{pes} two{psa} few{psi} other{psov}"); // Slovenian translation of dog in Plural Form 31 32 // Create a new PluralFormat for a given locale locale and pattern string 33 PluralFormat plfmtEn = PluralFormat(locEn, patEn,status); 34 PluralFormat plfmtSl = PluralFormat(locSl, patSl,status); 35 // Constructs a MessageFormat for given pattern and locale. 36 MessageFormat* msgfmtEn = new MessageFormat("{0,number} {1}", locEn,status); 37 MessageFormat* msgfmtSl = new MessageFormat("{0,number} {1}", locSl,status); 38 39 int numbers[] = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102}; 40 u_printf("Output by using PluralFormat and MessageFormat API\n"); 41 u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian"); 42 43 // Use MessageFormat.format () to format the objects and append to the given StringBuffer 44 for (int i=0;i<sizeof(numbers)/sizeof(int);i++) { 45 UnicodeString msgEn,msgSl; 46 FieldPosition fpos = 0; 47 Formattable argEn[]={Formattable(numbers[i]), Formattable(plfmtEn.format(numbers[i],status))}; 48 Formattable argSl[]={Formattable(numbers[i]), Formattable(plfmtSl.format(numbers[i],status))}; 49 msgfmtEn->format(argEn,2,msgEn,fpos,status); 50 msgfmtSl->format(argSl,2,msgSl,fpos,status); 51 u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(),msgSl.getTerminatedBuffer()); 52 } 53 54 u_printf("\n"); 55 56 // Equivalent code with message format pattern 57 UnicodeString msgPatEn = "{0,plural, one{# dog} other{# dogs}}"; 58 UnicodeString msgPatSl = "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}"; 59 60 MessageFormat* altMsgfmtEn = new MessageFormat(msgPatEn, locEn,status); 61 MessageFormat* altMsgfmtSl = new MessageFormat(msgPatSl, locSl,status); 62 u_printf("Same Output by using MessageFormat API only\n"); 63 u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian"); 64 for (int i=0;i<sizeof(numbers)/sizeof(int);i++) { 65 UnicodeString msgEn,msgSl; 66 Formattable arg[] = {numbers[i]}; 67 FieldPosition fPos =0; 68 altMsgfmtEn->format(arg, 1, msgEn, fPos, status); 69 altMsgfmtSl->format(arg, 1, msgSl, fPos,status); 70 u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(), msgSl.getTerminatedBuffer()); 71 } 72 73 delete msgfmtEn; 74 delete msgfmtSl; 75 delete altMsgfmtEn; 76 delete altMsgfmtSl; 77 //! [PluralFormatExample] 78 79 /* output of the sample code: 80 ******************************************************************** 81 Number English Slovenian 82 0 0 dogs 0 psov 83 1 1 dog 1 pes 84 2 2 dogs 2 psa 85 3 3 dogs 3 psi 86 4 4 dogs 4 psi 87 5 5 dogs 5 psov 88 10 10 dogs 10 psov 89 100 100 dogs 100 psov 90 101 101 dogs 101 pes 91 102 102 dogs 102 psa 92 93 *********************************************************************/ 94 } 95 int main (int argc, char* argv[]) 96 { 97 PluralFormatExample(); 98 return 0; 99 }