ChoiceFormat is probably not what you need. * Please use MessageFormat * with plural arguments for proper plural selection, * and select arguments for simple selection among a fixed set of choices!
ChoiceFormat
MessageFormat
plural
select
A ChoiceFormat splits * the real number line \htmlonly-∞ to * +∞\endhtmlonly into two * or more contiguous ranges. Each range is mapped to a * string.
-∞
+∞
ChoiceFormat was originally intended * for displaying grammatically correct * plurals such as "There is one file." vs. "There are 2 files." * However, plural rules for many languages * are too complex for the capabilities of ChoiceFormat, * and its requirement of specifying the precise rules for each message * is unmanageable for translators.
There are two methods of defining a ChoiceFormat; both * are equivalent. The first is by using a string pattern. This is the * preferred method in most cases. The second method is through direct * specification of the arrays that logically make up the * ChoiceFormat.
Note: Typically, choice formatting is done (if done at all) via MessageFormat * with a choice argument type, * rather than using a stand-alone ChoiceFormat.
choice
The pattern string defines the range boundaries and the strings for each number range. * Syntax: *
* choiceStyle = number separator message ('|' number separator message)* * number = normal_number | ['-'] \htmlonly∞\endhtmlonly (U+221E, infinity) * normal_number = double value (unlocalized ASCII string) * separator = less_than | less_than_or_equal * less_than = '<' * less_than_or_equal = '#' | \htmlonly≤\endhtmlonly (U+2264) * message: see {@link MessageFormat} *
Each numeric sub-range extends from the current range's number * to the next range's number. * The number itself is included in its range if a less_than_or_equal sign is used, * and excluded from its range (and instead included in the previous range) * if a less_than sign is used.
less_than_or_equal
less_than
When a ChoiceFormat is constructed from * arrays of numbers, closure flags and strings, * they are interpreted just like * the sequence of (number separator string) in an equivalent pattern string. * closure[i]==TRUE corresponds to a less_than separator sign. * The equivalent pattern string will be constructed automatically.
(number separator string)
closure[i]==TRUE
During formatting, a number is mapped to the first range * where the number is not greater than the range's upper limit. * That range's message string is returned. A NaN maps to the very first range.
During parsing, a range is selected for the longest match of * any range's message. That range's number is returned, ignoring the separator/closure. * Only a simple string match is performed, without parsing of arguments that * might be specified in the message strings.
Note that the first range's number is ignored in formatting * but may be returned from parsing.
Here is an example of two arrays that map the number * 1..7 to the English day of the week abbreviations * Sun..Sat. No closures array is given; this is the same as * specifying all closures to be FALSE.
1..7
Sun..Sat
FALSE
{1,2,3,4,5,6,7}, * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1, * +Inf] to three strings. That is, the number line is split into three * ranges: x < 1.0, x = 1.0, and x > 1.0. * (The round parentheses in the notation above indicate an exclusive boundary, * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[ )
{0, 1, 1}, * {FALSE, FALSE, TRUE}, * {"no files", "one file", "many files"}
Here is an example that shows formatting and parsing:
User subclasses are not supported. While clients may write * subclasses, such code will not necessarily work and will not be * guaranteed to work stably from release to release. * * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ class U_I18N_API ChoiceFormat: public NumberFormat { public: /** * Constructs a new ChoiceFormat from the pattern string. * * @param pattern Pattern used to construct object. * @param status Output param to receive success code. If the * pattern cannot be parsed, set to failure code. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ ChoiceFormat(const UnicodeString& pattern, UErrorCode& status); /** * Constructs a new ChoiceFormat with the given limits and message strings. * All closure flags default to FALSE, * equivalent to less_than_or_equal separators. * * Copies the limits and formats instead of adopting them. * * @param limits Array of limit values. * @param formats Array of formats. * @param count Size of 'limits' and 'formats' arrays. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ ChoiceFormat(const double* limits, const UnicodeString* formats, int32_t count ); /** * Constructs a new ChoiceFormat with the given limits, closure flags and message strings. * * Copies the limits and formats instead of adopting them. * * @param limits Array of limit values * @param closures Array of booleans specifying whether each * element of 'limits' is open or closed. If FALSE, then the * corresponding limit number is a member of its range. * If TRUE, then the limit number belongs to the previous range it. * @param formats Array of formats * @param count Size of 'limits', 'closures', and 'formats' arrays * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ ChoiceFormat(const double* limits, const UBool* closures, const UnicodeString* formats, int32_t count); /** * Copy constructor. * * @param that ChoiceFormat object to be copied from * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ ChoiceFormat(const ChoiceFormat& that); /** * Assignment operator. * * @param that ChoiceFormat object to be copied * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ const ChoiceFormat& operator=(const ChoiceFormat& that); /** * Destructor. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual ~ChoiceFormat(); /** * Clones this Format object. The caller owns the * result and must delete it when done. * * @return a copy of this object * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual ChoiceFormat* clone() const; /** * Returns true if the given Format objects are semantically equal. * Objects of different subclasses are considered unequal. * * @param other ChoiceFormat object to be compared * @return true if other is the same as this. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UBool operator==(const Format& other) const; /** * Sets the pattern. * @param pattern The pattern to be applied. * @param status Output param set to success/failure code on * exit. If the pattern is invalid, this will be * set to a failure result. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status); /** * Sets the pattern. * @param pattern The pattern to be applied. * @param parseError Struct to receive information on position * of error if an error is encountered * @param status Output param set to success/failure code on * exit. If the pattern is invalid, this will be * set to a failure result. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status); /** * Gets the pattern. * * @param pattern Output param which will receive the pattern * Previous contents are deleted. * @return A reference to 'pattern' * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UnicodeString& toPattern(UnicodeString &pattern) const; /** * Sets the choices to be used in formatting. * For details see the constructor with the same parameter list. * * @param limitsToCopy Contains the top value that you want * parsed with that format,and should be in * ascending sorted order. When formatting X, * the choice will be the i, where limit[i] * <= X < limit[i+1]. * @param formatsToCopy The format strings you want to use for each limit. * @param count The size of the above arrays. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual void setChoices(const double* limitsToCopy, const UnicodeString* formatsToCopy, int32_t count ); /** * Sets the choices to be used in formatting. * For details see the constructor with the same parameter list. * * @param limits Array of limits * @param closures Array of limit booleans * @param formats Array of format string * @param count The size of the above arrays * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual void setChoices(const double* limits, const UBool* closures, const UnicodeString* formats, int32_t count); /** * Returns NULL and 0. * Before ICU 4.8, this used to return the choice limits array. * * @param count Will be set to 0. * @return NULL * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. */ virtual const double* getLimits(int32_t& count) const; /** * Returns NULL and 0. * Before ICU 4.8, this used to return the limit booleans array. * * @param count Will be set to 0. * @return NULL * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. */ virtual const UBool* getClosures(int32_t& count) const; /** * Returns NULL and 0. * Before ICU 4.8, this used to return the array of choice strings. * * @param count Will be set to 0. * @return NULL * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. */ virtual const UnicodeString* getFormats(int32_t& count) const; using NumberFormat::format; /** * Formats a double number using this object's choices. * * @param number The value to be formatted. * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param pos On input: an alignment field, if desired. * On output: the offsets of the alignment field. * @return Reference to 'appendTo' parameter. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const; /** * Formats an int32_t number using this object's choices. * * @param number The value to be formatted. * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param pos On input: an alignment field, if desired. * On output: the offsets of the alignment field. * @return Reference to 'appendTo' parameter. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const; /** * Formats an int64_t number using this object's choices. * * @param number The value to be formatted. * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param pos On input: an alignment field, if desired. * On output: the offsets of the alignment field. * @return Reference to 'appendTo' parameter. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const; /** * Formats an array of objects using this object's choices. * * @param objs The array of objects to be formatted. * @param cnt The size of objs. * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param pos On input: an alignment field, if desired. * On output: the offsets of the alignment field. * @param success Output param set to success/failure code on * exit. * @return Reference to 'appendTo' parameter. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UnicodeString& format(const Formattable* objs, int32_t cnt, UnicodeString& appendTo, FieldPosition& pos, UErrorCode& success) const; using NumberFormat::parse; /** * Looks for the longest match of any message string on the input text and, * if there is a match, sets the result object to the corresponding range's number. * * If no string matches, then the parsePosition is unchanged. * * @param text The text to be parsed. * @param result Formattable to be set to the parse result. * If parse fails, return contents are undefined. * @param parsePosition The position to start parsing at on input. * On output, moved to after the last successfully * parse character. On parse failure, does not change. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual void parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const; /** * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI". * * @return The class ID for this object. All objects of a * given class have the same class ID. Objects of * other classes have different class IDs. * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. */ virtual UClassID getDynamicClassID(void) const; /** * Returns the class ID for this class. This is useful only for * comparing to a return value from getDynamicClassID(). For example: *
* . Base* polymorphic_pointer = createPolymorphicObject(); * . if (polymorphic_pointer->getDynamicClassID() == * . Derived::getStaticClassID()) ... *