From 6dd8688bae468a44e929c138f1e2b9e828bb4039 Mon Sep 17 00:00:00 2001 From: Bing Long Date: Tue, 14 May 2013 20:51:05 +0000 Subject: [PATCH] ICU-10113 Added PluralFormatSample.java. Updated the API document for PluralFormat(ULocale ulocale, String pattern). X-SVN-Rev: 33653 --- .../src/com/ibm/icu/text/PluralFormat.java | 3 +- .../text/pluralformat/PluralFormatSample.java | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 icu4j/samples/src/com/ibm/icu/samples/text/pluralformat/PluralFormatSample.java diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java index 194527cf222..9c23ed94d9c 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2007-2012, International Business Machines Corporation and + * Copyright (C) 2007-2013, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ @@ -255,6 +255,7 @@ public class PluralFormat extends UFormat { * locale. * The locale will be used to get the set of plural rules and for * standard number formatting. + *

Example code:{@.jcite com.ibm.icu.samples.text.pluralformat.PluralFormatSample:---PluralFormatExample} * @param ulocale the PluralFormat will be configured with * rules for this locale. This locale will also be used for standard * number formatting. diff --git a/icu4j/samples/src/com/ibm/icu/samples/text/pluralformat/PluralFormatSample.java b/icu4j/samples/src/com/ibm/icu/samples/text/pluralformat/PluralFormatSample.java new file mode 100644 index 00000000000..bafe4fc12da --- /dev/null +++ b/icu4j/samples/src/com/ibm/icu/samples/text/pluralformat/PluralFormatSample.java @@ -0,0 +1,93 @@ +/* + ******************************************************************************* + * Copyright (C) 2013, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + */ +package com.ibm.icu.samples.text.pluralformat; + +import java.text.FieldPosition; +import com.ibm.icu.text.MessageFormat; +import com.ibm.icu.text.PluralFormat; +import com.ibm.icu.util.ULocale; + +public class PluralFormatSample { + + public static void main(String[] args) { + PluralFormatExample(); + } + + private static void PluralFormatExample(){ + + System.out.println("======================================================================================="); + System.out.println(" PluralFormatExample()"); + System.out.println(); + System.out.println(" Use PluralFormat and Messageformat to get appropriate Plural Form for languages below:"); + System.out.println(" English, Slovenian"); + System.out.println("======================================================================================="); + // ---PluralFormatExample + ULocale locEn = new ULocale("en"); + ULocale locSl = new ULocale("sl"); + + String patEn = "one{dog} other{dogs}"; // English 'dog' + String patSl = "one{pes} two{psa} few{psi} other{psov}"; // Slovenian translation of dog in Plural Form + + // Create a new PluralFormat for a given locale locale and pattern string + PluralFormat plfmtEn = new PluralFormat(locEn, patEn); + PluralFormat plfmtSl = new PluralFormat(locSl, patSl); + // Constructs a MessageFormat for the specified locale and pattern. + MessageFormat msgfmtEn = new MessageFormat("{0,number} {1}", locEn); + MessageFormat msgfmtSl = new MessageFormat("{0,number} {1}", locSl); + + final int[] numbers = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102}; + System.out.println("Output by using PluralFormat and MessageFormat API\n"); + System.out.printf("%-16s%-16s%-16s\n", "Number", "English", "Slovenian"); + + // Use MessageFormat.format () to format the objects and appends to the given StringBuffer + for (int num : numbers) { + StringBuffer msgEn = new StringBuffer(); + StringBuffer msgSl = new StringBuffer(); + + msgfmtEn.format(new Object[] {num, plfmtEn.format(num)}, msgEn, new FieldPosition(0)); + msgfmtSl.format(new Object[] {num, plfmtSl.format(num)}, msgSl, new FieldPosition(0)); + + System.out.printf("%-16s%-16s%-16s\n", num, msgEn, msgSl); + } + + System.out.println(); + + // Equivalent code with message format pattern + String msgPatEn = "{0,plural, one{# dog} other{# dogs}}"; + String msgPatSl = "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}"; + + MessageFormat altMsgfmtEn = new MessageFormat(msgPatEn, locEn); + MessageFormat altMsgfmtSl = new MessageFormat(msgPatSl, locSl); + System.out.println("Same Output by using MessageFormat API only\n"); + System.out.printf("%-16s%-16s%-16s\n", "Number", "English", "Slovenian"); + for (int num : numbers) { + StringBuffer msgEn = new StringBuffer(); + StringBuffer msgSl = new StringBuffer(); + + altMsgfmtEn.format(new Object[] {num}, msgEn, new FieldPosition(0)); + altMsgfmtSl.format(new Object[] {num}, msgSl, new FieldPosition(0)); + + System.out.printf("%-16s%-16s%-16s\n", num, msgEn, msgSl); + } + /** output of the sample code: + ******************************************************************** + Number English Slovenian + 0 0 dogs 0 psov + 1 1 dog 1 pes + 2 2 dogs 2 psa + 3 3 dogs 3 psi + 4 4 dogs 4 psi + 5 5 dogs 5 psov + 10 10 dogs 10 psov + 100 100 dogs 100 psov + 101 101 dogs 101 pes + 102 102 dogs 102 psa + + *******************************************************************/ + // ---PluralFormatExample + } +} \ No newline at end of file