]>
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]
19 static void PluralFormatExample() {
21 u_printf("=============================================================================\n");
22 u_printf(" PluralFormatExample()\n");
24 u_printf(" Use PluralFormat and Messageformat to get Plural Form for languages below:\n");
25 u_printf(" English, Slovenian\n");
26 u_printf("=============================================================================\n");
28 //! [PluralFormatExample]
29 UErrorCode status
=U_ZERO_ERROR
;
30 Locale locEn
= Locale("en");
31 Locale locSl
= Locale("sl");
33 UnicodeString patEn
= UnicodeString("one{dog} other{dogs}"); // English 'dog'
34 UnicodeString patSl
= UnicodeString("one{pes} two{psa} few{psi} other{psov}"); // Slovenian translation of dog in Plural Form
36 // Create a new PluralFormat for a given locale locale and pattern string
37 PluralFormat plfmtEn
= PluralFormat(locEn
, patEn
,status
);
38 PluralFormat plfmtSl
= PluralFormat(locSl
, patSl
,status
);
39 // Constructs a MessageFormat for given pattern and locale.
40 MessageFormat
* msgfmtEn
= new MessageFormat("{0,number} {1}", locEn
,status
);
41 MessageFormat
* msgfmtSl
= new MessageFormat("{0,number} {1}", locSl
,status
);
43 int numbers
[] = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102};
44 u_printf("Output by using PluralFormat and MessageFormat API\n");
45 u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian");
47 // Use MessageFormat.format () to format the objects and append to the given StringBuffer
48 for (int i
=0;i
<sizeof(numbers
)/sizeof(int);i
++) {
49 UnicodeString msgEn
,msgSl
;
50 FieldPosition fpos
= 0;
51 Formattable argEn
[]={Formattable(numbers
[i
]), Formattable(plfmtEn
.format(numbers
[i
],status
))};
52 Formattable argSl
[]={Formattable(numbers
[i
]), Formattable(plfmtSl
.format(numbers
[i
],status
))};
53 msgfmtEn
->format(argEn
,2,msgEn
,fpos
,status
);
54 msgfmtSl
->format(argSl
,2,msgSl
,fpos
,status
);
55 u_printf("%-16d%-16S%-16S\n", numbers
[i
], msgEn
.getTerminatedBuffer(),msgSl
.getTerminatedBuffer());
60 // Equivalent code with message format pattern
61 UnicodeString msgPatEn
= "{0,plural, one{# dog} other{# dogs}}";
62 UnicodeString msgPatSl
= "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}";
64 MessageFormat
* altMsgfmtEn
= new MessageFormat(msgPatEn
, locEn
,status
);
65 MessageFormat
* altMsgfmtSl
= new MessageFormat(msgPatSl
, locSl
,status
);
66 u_printf("Same Output by using MessageFormat API only\n");
67 u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian");
68 for (int i
=0;i
<sizeof(numbers
)/sizeof(int);i
++) {
69 UnicodeString msgEn
,msgSl
;
70 Formattable arg
[] = {numbers
[i
]};
71 FieldPosition fPos
=0;
72 altMsgfmtEn
->format(arg
, 1, msgEn
, fPos
, status
);
73 altMsgfmtSl
->format(arg
, 1, msgSl
, fPos
,status
);
74 u_printf("%-16d%-16S%-16S\n", numbers
[i
], msgEn
.getTerminatedBuffer(), msgSl
.getTerminatedBuffer());
81 //! [PluralFormatExample]
83 /* output of the sample code:
84 ********************************************************************
85 Number English Slovenian
97 *********************************************************************/
99 int main (int argc
, char* argv
[])
101 PluralFormatExample();