• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************************
2 * Copyright (C) 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;
PluralFormatExample()19 static void PluralFormatExample() {
20 
21 	u_printf("=============================================================================\n");
22 	u_printf(" PluralFormatExample()\n");
23     u_printf("\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");
27 
28 	//! [PluralFormatExample]
29 	UErrorCode status =U_ZERO_ERROR;
30 	Locale locEn = Locale("en");
31     Locale locSl = Locale("sl");
32 
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
35 
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);
42 
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");
46 
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());
56       }
57 
58      u_printf("\n");
59 
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}}";
63 
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());
75       }
76 
77  	delete msgfmtEn;
78 	delete msgfmtSl;
79 	delete altMsgfmtEn;
80 	delete altMsgfmtSl;
81 	//! [PluralFormatExample]
82 
83 	  /*  output of the sample code:
84        ********************************************************************
85         Number			English			Slovenian
86         0				0 dogs			0 psov
87         1				1 dog			1 pes
88         2				2 dogs			2 psa
89         3				3 dogs			3 psi
90         4				4 dogs			4 psi
91         5				5 dogs			5 psov
92         10				10 dogs			10 psov
93         100				100 dogs		100 psov
94         101				101 dogs		101 pes
95         102				102 dogs		102 psa
96 
97       *********************************************************************/
98 }
main(int argc,char * argv[])99 int main (int argc, char* argv[])
100 {
101 	PluralFormatExample();
102 	return 0;
103 }
104