]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/plurfmtsample/plurfmtsample.cpp
1 /********************************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 ********************************************************************************
5 ********************************************************************************
6 * Copyright (C) 2008-2013, International Business Machines Corporation and
7 * others. All Rights Reserved.
8 ********************************************************************************
11 //! [PluralFormatExample1]
13 #include "unicode/plurfmt.h"
14 #include "unicode/msgfmt.h"
15 #include "unicode/ustdio.h"
16 //! [PluralFormatExample1]
21 static void PluralFormatExample() {
23 u_printf("=============================================================================\n");
24 u_printf(" PluralFormatExample()\n");
26 u_printf(" Use PluralFormat and Messageformat to get Plural Form for languages below:\n");
27 u_printf(" English, Slovenian\n");
28 u_printf("=============================================================================\n");
30 //! [PluralFormatExample]
31 UErrorCode status
=U_ZERO_ERROR
;
32 Locale locEn
= Locale("en");
33 Locale locSl
= Locale("sl");
35 UnicodeString patEn
= UnicodeString("one{dog} other{dogs}"); // English 'dog'
36 UnicodeString patSl
= UnicodeString("one{pes} two{psa} few{psi} other{psov}"); // Slovenian translation of dog in Plural Form
38 // Create a new PluralFormat for a given locale locale and pattern string
39 PluralFormat plfmtEn
= PluralFormat(locEn
, patEn
,status
);
40 PluralFormat plfmtSl
= PluralFormat(locSl
, patSl
,status
);
41 // Constructs a MessageFormat for given pattern and locale.
42 MessageFormat
* msgfmtEn
= new MessageFormat("{0,number} {1}", locEn
,status
);
43 MessageFormat
* msgfmtSl
= new MessageFormat("{0,number} {1}", locSl
,status
);
45 int numbers
[] = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102};
46 u_printf("Output by using PluralFormat and MessageFormat API\n");
47 u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian");
49 // Use MessageFormat.format () to format the objects and append to the given StringBuffer
50 for (int i
=0;i
<sizeof(numbers
)/sizeof(int);i
++) {
51 UnicodeString msgEn
,msgSl
;
52 FieldPosition fpos
= 0;
53 Formattable argEn
[]={Formattable(numbers
[i
]), Formattable(plfmtEn
.format(numbers
[i
],status
))};
54 Formattable argSl
[]={Formattable(numbers
[i
]), Formattable(plfmtSl
.format(numbers
[i
],status
))};
55 msgfmtEn
->format(argEn
,2,msgEn
,fpos
,status
);
56 msgfmtSl
->format(argSl
,2,msgSl
,fpos
,status
);
57 u_printf("%-16d%-16S%-16S\n", numbers
[i
], msgEn
.getTerminatedBuffer(),msgSl
.getTerminatedBuffer());
62 // Equivalent code with message format pattern
63 UnicodeString msgPatEn
= "{0,plural, one{# dog} other{# dogs}}";
64 UnicodeString msgPatSl
= "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}";
66 MessageFormat
* altMsgfmtEn
= new MessageFormat(msgPatEn
, locEn
,status
);
67 MessageFormat
* altMsgfmtSl
= new MessageFormat(msgPatSl
, locSl
,status
);
68 u_printf("Same Output by using MessageFormat API only\n");
69 u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian");
70 for (int i
=0;i
<sizeof(numbers
)/sizeof(int);i
++) {
71 UnicodeString msgEn
,msgSl
;
72 Formattable arg
[] = {numbers
[i
]};
73 FieldPosition fPos
=0;
74 altMsgfmtEn
->format(arg
, 1, msgEn
, fPos
, status
);
75 altMsgfmtSl
->format(arg
, 1, msgSl
, fPos
,status
);
76 u_printf("%-16d%-16S%-16S\n", numbers
[i
], msgEn
.getTerminatedBuffer(), msgSl
.getTerminatedBuffer());
83 //! [PluralFormatExample]
85 /* output of the sample code:
86 ********************************************************************
87 Number English Slovenian
99 *********************************************************************/
101 int main (int argc
, char* argv
[])
103 PluralFormatExample();