• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ********************************************************************************
9 */
10 
11 //! [PluralFormatExample1]
12 #include <iostream>
13 #include "unicode/plurfmt.h"
14 #include "unicode/msgfmt.h"
15 #include "unicode/ustdio.h"
16 //! [PluralFormatExample1]
17 
18 using namespace std;
19 using namespace icu;
20 
PluralFormatExample()21 static void PluralFormatExample() {
22 
23 	u_printf("=============================================================================\n");
24 	u_printf(" PluralFormatExample()\n");
25     u_printf("\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");
29 
30 	//! [PluralFormatExample]
31 	UErrorCode status =U_ZERO_ERROR;
32 	Locale locEn = Locale("en");
33     Locale locSl = Locale("sl");
34 
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
37 
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);
44 
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");
48 
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());
58       }
59 
60      u_printf("\n");
61 
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}}";
65 
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());
77       }
78 
79  	delete msgfmtEn;
80 	delete msgfmtSl;
81 	delete altMsgfmtEn;
82 	delete altMsgfmtSl;
83 	//! [PluralFormatExample]
84 
85 	  /*  output of the sample code:
86        ********************************************************************
87         Number			English			Slovenian
88         0				0 dogs			0 psov
89         1				1 dog			1 pes
90         2				2 dogs			2 psa
91         3				3 dogs			3 psi
92         4				4 dogs			4 psi
93         5				5 dogs			5 psov
94         10				10 dogs			10 psov
95         100				100 dogs		100 psov
96         101				101 dogs		101 pes
97         102				102 dogs		102 psa
98 
99       *********************************************************************/
100 }
main(int argc,char * argv[])101 int main (int argc, char* argv[])
102 {
103 	PluralFormatExample();
104 	return 0;
105 }
106