1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2013-2014, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.samples.text.pluralformat; 10 // ---PluralFormatExample 11 import java.text.FieldPosition; 12 13 import com.ibm.icu.text.MessageFormat; 14 import com.ibm.icu.text.PluralFormat; 15 import com.ibm.icu.util.ULocale; 16 // ---PluralFormatExample 17 18 public class PluralFormatSample { 19 20 public static void main(String[] args) { 21 PluralFormatExample(); 22 } 23 24 private static void PluralFormatExample(){ 25 26 System.out.println("======================================================================================="); 27 System.out.println(" PluralFormatExample()"); 28 System.out.println(); 29 System.out.println(" Use PluralFormat and Messageformat to get appropriate Plural Form for languages below:"); 30 System.out.println(" English, Slovenian"); 31 System.out.println("======================================================================================="); 32 // ---PluralFormatExample 33 ULocale locEn = new ULocale("en"); 34 ULocale locSl = new ULocale("sl"); 35 36 String patEn = "one{dog} other{dogs}"; // English 'dog' 37 String patSl = "one{pes} two{psa} few{psi} other{psov}"; // Slovenian translation of dog in Plural Form 38 39 // Create a new PluralFormat for a given locale locale and pattern string 40 PluralFormat plfmtEn = new PluralFormat(locEn, patEn); 41 PluralFormat plfmtSl = new PluralFormat(locSl, patSl); 42 // Constructs a MessageFormat for the specified locale and pattern. 43 MessageFormat msgfmtEn = new MessageFormat("{0,number} {1}", locEn); 44 MessageFormat msgfmtSl = new MessageFormat("{0,number} {1}", locSl); 45 46 final int[] numbers = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102}; 47 System.out.println("Output by using PluralFormat and MessageFormat API\n"); 48 System.out.printf("%-16s%-16s%-16s\n", "Number", "English", "Slovenian"); 49 50 // Use MessageFormat.format () to format the objects and appends to the given StringBuffer 51 for (int num : numbers) { 52 StringBuffer msgEn = new StringBuffer(); 53 StringBuffer msgSl = new StringBuffer(); 54 55 msgfmtEn.format(new Object[] {num, plfmtEn.format(num)}, msgEn, new FieldPosition(0)); 56 msgfmtSl.format(new Object[] {num, plfmtSl.format(num)}, msgSl, new FieldPosition(0)); 57 58 System.out.printf("%-16s%-16s%-16s\n", num, msgEn, msgSl); 59 } 60 61 System.out.println(); 62 63 // Equivalent code with message format pattern 64 String msgPatEn = "{0,plural, one{# dog} other{# dogs}}"; 65 String msgPatSl = "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}"; 66 67 MessageFormat altMsgfmtEn = new MessageFormat(msgPatEn, locEn); 68 MessageFormat altMsgfmtSl = new MessageFormat(msgPatSl, locSl); 69 System.out.println("Same Output by using MessageFormat API only\n"); 70 System.out.printf("%-16s%-16s%-16s\n", "Number", "English", "Slovenian"); 71 for (int num : numbers) { 72 StringBuffer msgEn = new StringBuffer(); 73 StringBuffer msgSl = new StringBuffer(); 74 75 altMsgfmtEn.format(new Object[] {num}, msgEn, new FieldPosition(0)); 76 altMsgfmtSl.format(new Object[] {num}, msgSl, new FieldPosition(0)); 77 78 System.out.printf("%-16s%-16s%-16s\n", num, msgEn, msgSl); 79 } 80 /** output of the sample code: 81 ******************************************************************** 82 Number English Slovenian 83 0 0 dogs 0 psov 84 1 1 dog 1 pes 85 2 2 dogs 2 psa 86 3 3 dogs 3 psi 87 4 4 dogs 4 psi 88 5 5 dogs 5 psov 89 10 10 dogs 10 psov 90 100 100 dogs 100 psov 91 101 101 dogs 101 pes 92 102 102 dogs 102 psa 93 94 *******************************************************************/ 95 // ---PluralFormatExample 96 } 97 }