]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/plurfmtsample/plurfmtsample.cpp
1 /********************************************************************************
2 * Copyright (C) 2008-2013, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 *******************************************************************************
7 //! [PluralFormatExample1]
9 #include "unicode/plurfmt.h"
10 #include "unicode/msgfmt.h"
11 #include "unicode/ustdio.h"
12 //! [PluralFormatExample1]
15 static void PluralFormatExample() {
17 u_printf("=============================================================================\n");
18 u_printf(" PluralFormatExample()\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");
24 //! [PluralFormatExample]
25 UErrorCode status
=U_ZERO_ERROR
;
26 Locale locEn
= Locale("en");
27 Locale locSl
= Locale("sl");
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
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
);
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");
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());
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}}";
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());
77 //! [PluralFormatExample]
79 /* output of the sample code:
80 ********************************************************************
81 Number English Slovenian
93 *********************************************************************/
95 int main (int argc
, char* argv
[])
97 PluralFormatExample();