ChoiceFormat is probably not what you need. 55 * Please use MessageFormat 56 * with plural arguments for proper plural selection, 57 * and select arguments for simple selection among a fixed set of choices!
ChoiceFormat
MessageFormat
plural
select
A ChoiceFormat splits 60 * the real number line \htmlonly-∞ to 61 * +∞\endhtmlonly into two 62 * or more contiguous ranges. Each range is mapped to a 63 * string.
-∞
+∞
ChoiceFormat was originally intended 66 * for displaying grammatically correct 67 * plurals such as "There is one file." vs. "There are 2 files." 68 * However, plural rules for many languages 69 * are too complex for the capabilities of ChoiceFormat, 70 * and its requirement of specifying the precise rules for each message 71 * is unmanageable for translators.
There are two methods of defining a ChoiceFormat; both 74 * are equivalent. The first is by using a string pattern. This is the 75 * preferred method in most cases. The second method is through direct 76 * specification of the arrays that logically make up the 77 * ChoiceFormat.
Note: Typically, choice formatting is done (if done at all) via MessageFormat 80 * with a choice argument type, 81 * rather than using a stand-alone ChoiceFormat.
choice
The pattern string defines the range boundaries and the strings for each number range. 86 * Syntax: 87 *
88 * choiceStyle = number separator message ('|' number separator message)* 89 * number = normal_number | ['-'] \htmlonly∞\endhtmlonly (U+221E, infinity) 90 * normal_number = double value (unlocalized ASCII string) 91 * separator = less_than | less_than_or_equal 92 * less_than = '<' 93 * less_than_or_equal = '#' | \htmlonly≤\endhtmlonly (U+2264) 94 * message: see {@link MessageFormat} 95 *
Each numeric sub-range extends from the current range's number 100 * to the next range's number. 101 * The number itself is included in its range if a less_than_or_equal sign is used, 102 * and excluded from its range (and instead included in the previous range) 103 * if a less_than sign is used.
less_than_or_equal
less_than
When a ChoiceFormat is constructed from 106 * arrays of numbers, closure flags and strings, 107 * they are interpreted just like 108 * the sequence of (number separator string) in an equivalent pattern string. 109 * closure[i]==true corresponds to a less_than separator sign. 110 * The equivalent pattern string will be constructed automatically.
(number separator string)
closure[i]==true
During formatting, a number is mapped to the first range 113 * where the number is not greater than the range's upper limit. 114 * 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 117 * any range's message. That range's number is returned, ignoring the separator/closure. 118 * Only a simple string match is performed, without parsing of arguments that 119 * might be specified in the message strings.
Note that the first range's number is ignored in formatting 122 * but may be returned from parsing.
Here is an example of two arrays that map the number 127 * 1..7 to the English day of the week abbreviations 128 * Sun..Sat. No closures array is given; this is the same as 129 * specifying all closures to be false.
1..7
Sun..Sat
false
{1,2,3,4,5,6,7}, 132 * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1, 135 * +Inf] to three strings. That is, the number line is split into three 136 * ranges: x < 1.0, x = 1.0, and x > 1.0. 137 * (The round parentheses in the notation above indicate an exclusive boundary, 138 * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[ )
{0, 1, 1}, 141 * {false, false, true}, 142 * {"no files", "one file", "many files"}
Here is an example that shows formatting and parsing:
User subclasses are not supported. While clients may write 171 * subclasses, such code will not necessarily work and will not be 172 * guaranteed to work stably from release to release. 173 * 174 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 175 */ 176 class U_I18N_API ChoiceFormat: public NumberFormat { 177 public: 178 /** 179 * Constructs a new ChoiceFormat from the pattern string. 180 * 181 * @param pattern Pattern used to construct object. 182 * @param status Output param to receive success code. If the 183 * pattern cannot be parsed, set to failure code. 184 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 185 */ 186 ChoiceFormat(const UnicodeString& pattern, 187 UErrorCode& status); 188 189 190 /** 191 * Constructs a new ChoiceFormat with the given limits and message strings. 192 * All closure flags default to false, 193 * equivalent to less_than_or_equal separators. 194 * 195 * Copies the limits and formats instead of adopting them. 196 * 197 * @param limits Array of limit values. 198 * @param formats Array of formats. 199 * @param count Size of 'limits' and 'formats' arrays. 200 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 201 */ 202 ChoiceFormat(const double* limits, 203 const UnicodeString* formats, 204 int32_t count ); 205 206 /** 207 * Constructs a new ChoiceFormat with the given limits, closure flags and message strings. 208 * 209 * Copies the limits and formats instead of adopting them. 210 * 211 * @param limits Array of limit values 212 * @param closures Array of booleans specifying whether each 213 * element of 'limits' is open or closed. If false, then the 214 * corresponding limit number is a member of its range. 215 * If true, then the limit number belongs to the previous range it. 216 * @param formats Array of formats 217 * @param count Size of 'limits', 'closures', and 'formats' arrays 218 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 219 */ 220 ChoiceFormat(const double* limits, 221 const UBool* closures, 222 const UnicodeString* formats, 223 int32_t count); 224 225 /** 226 * Copy constructor. 227 * 228 * @param that ChoiceFormat object to be copied from 229 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 230 */ 231 ChoiceFormat(const ChoiceFormat& that); 232 233 /** 234 * Assignment operator. 235 * 236 * @param that ChoiceFormat object to be copied 237 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 238 */ 239 const ChoiceFormat& operator=(const ChoiceFormat& that); 240 241 /** 242 * Destructor. 243 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 244 */ 245 virtual ~ChoiceFormat(); 246 247 /** 248 * Clones this Format object. The caller owns the 249 * result and must delete it when done. 250 * 251 * @return a copy of this object 252 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 253 */ 254 virtual ChoiceFormat* clone() const override; 255 256 /** 257 * Returns true if the given Format objects are semantically equal. 258 * Objects of different subclasses are considered unequal. 259 * 260 * @param other ChoiceFormat object to be compared 261 * @return true if other is the same as this. 262 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 263 */ 264 virtual bool operator==(const Format& other) const override; 265 266 /** 267 * Sets the pattern. 268 * @param pattern The pattern to be applied. 269 * @param status Output param set to success/failure code on 270 * exit. If the pattern is invalid, this will be 271 * set to a failure result. 272 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 273 */ 274 virtual void applyPattern(const UnicodeString& pattern, 275 UErrorCode& status); 276 277 /** 278 * Sets the pattern. 279 * @param pattern The pattern to be applied. 280 * @param parseError Struct to receive information on position 281 * of error if an error is encountered 282 * @param status Output param set to success/failure code on 283 * exit. If the pattern is invalid, this will be 284 * set to a failure result. 285 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 286 */ 287 virtual void applyPattern(const UnicodeString& pattern, 288 UParseError& parseError, 289 UErrorCode& status); 290 /** 291 * Gets the pattern. 292 * 293 * @param pattern Output param which will receive the pattern 294 * Previous contents are deleted. 295 * @return A reference to 'pattern' 296 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 297 */ 298 virtual UnicodeString& toPattern(UnicodeString &pattern) const; 299 300 /** 301 * Sets the choices to be used in formatting. 302 * For details see the constructor with the same parameter list. 303 * 304 * @param limitsToCopy Contains the top value that you want 305 * parsed with that format,and should be in 306 * ascending sorted order. When formatting X, 307 * the choice will be the i, where limit[i] 308 * <= X < limit[i+1]. 309 * @param formatsToCopy The format strings you want to use for each limit. 310 * @param count The size of the above arrays. 311 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 312 */ 313 virtual void setChoices(const double* limitsToCopy, 314 const UnicodeString* formatsToCopy, 315 int32_t count ); 316 317 /** 318 * Sets the choices to be used in formatting. 319 * For details see the constructor with the same parameter list. 320 * 321 * @param limits Array of limits 322 * @param closures Array of limit booleans 323 * @param formats Array of format string 324 * @param count The size of the above arrays 325 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 326 */ 327 virtual void setChoices(const double* limits, 328 const UBool* closures, 329 const UnicodeString* formats, 330 int32_t count); 331 332 /** 333 * Returns nullptr and 0. 334 * Before ICU 4.8, this used to return the choice limits array. 335 * 336 * @param count Will be set to 0. 337 * @return nullptr 338 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. 339 */ 340 virtual const double* getLimits(int32_t& count) const; 341 342 /** 343 * Returns nullptr and 0. 344 * Before ICU 4.8, this used to return the limit booleans array. 345 * 346 * @param count Will be set to 0. 347 * @return nullptr 348 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. 349 */ 350 virtual const UBool* getClosures(int32_t& count) const; 351 352 /** 353 * Returns nullptr and 0. 354 * Before ICU 4.8, this used to return the array of choice strings. 355 * 356 * @param count Will be set to 0. 357 * @return nullptr 358 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. 359 */ 360 virtual const UnicodeString* getFormats(int32_t& count) const; 361 362 363 using NumberFormat::format; 364 365 /** 366 * Formats a double number using this object's choices. 367 * 368 * @param number The value to be formatted. 369 * @param appendTo Output parameter to receive result. 370 * Result is appended to existing contents. 371 * @param pos On input: an alignment field, if desired. 372 * On output: the offsets of the alignment field. 373 * @return Reference to 'appendTo' parameter. 374 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 375 */ 376 virtual UnicodeString& format(double number, 377 UnicodeString& appendTo, 378 FieldPosition& pos) const override; 379 /** 380 * Formats an int32_t number using this object's choices. 381 * 382 * @param number The value to be formatted. 383 * @param appendTo Output parameter to receive result. 384 * Result is appended to existing contents. 385 * @param pos On input: an alignment field, if desired. 386 * On output: the offsets of the alignment field. 387 * @return Reference to 'appendTo' parameter. 388 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 389 */ 390 virtual UnicodeString& format(int32_t number, 391 UnicodeString& appendTo, 392 FieldPosition& pos) const override; 393 394 /** 395 * Formats an int64_t number using this object's choices. 396 * 397 * @param number The value to be formatted. 398 * @param appendTo Output parameter to receive result. 399 * Result is appended to existing contents. 400 * @param pos On input: an alignment field, if desired. 401 * On output: the offsets of the alignment field. 402 * @return Reference to 'appendTo' parameter. 403 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 404 */ 405 virtual UnicodeString& format(int64_t number, 406 UnicodeString& appendTo, 407 FieldPosition& pos) const override; 408 409 /** 410 * Formats an array of objects using this object's choices. 411 * 412 * @param objs The array of objects to be formatted. 413 * @param cnt The size of objs. 414 * @param appendTo Output parameter to receive result. 415 * Result is appended to existing contents. 416 * @param pos On input: an alignment field, if desired. 417 * On output: the offsets of the alignment field. 418 * @param success Output param set to success/failure code on 419 * exit. 420 * @return Reference to 'appendTo' parameter. 421 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 422 */ 423 virtual UnicodeString& format(const Formattable* objs, 424 int32_t cnt, 425 UnicodeString& appendTo, 426 FieldPosition& pos, 427 UErrorCode& success) const; 428 429 using NumberFormat::parse; 430 431 /** 432 * Looks for the longest match of any message string on the input text and, 433 * if there is a match, sets the result object to the corresponding range's number. 434 * 435 * If no string matches, then the parsePosition is unchanged. 436 * 437 * @param text The text to be parsed. 438 * @param result Formattable to be set to the parse result. 439 * If parse fails, return contents are undefined. 440 * @param parsePosition The position to start parsing at on input. 441 * On output, moved to after the last successfully 442 * parse character. On parse failure, does not change. 443 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 444 */ 445 virtual void parse(const UnicodeString& text, 446 Formattable& result, 447 ParsePosition& parsePosition) const override; 448 449 /** 450 * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI". 451 * 452 * @return The class ID for this object. All objects of a 453 * given class have the same class ID. Objects of 454 * other classes have different class IDs. 455 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 456 */ 457 virtual UClassID getDynamicClassID(void) const override; 458 459 /** 460 * Returns the class ID for this class. This is useful only for 461 * comparing to a return value from getDynamicClassID(). For example: 462 *
463 * . Base* polymorphic_pointer = createPolymorphicObject(); 464 * . if (polymorphic_pointer->getDynamicClassID() == 465 * . Derived::getStaticClassID()) ... 466 *