SelectFormat supports the creation of internationalized 41 * messages by selecting phrases based on keywords. The pattern specifies 42 * how to map keywords to phrases and provides a default phrase. The 43 * object provided to the format method is a string that's matched 44 * against the keywords. If there is a match, the corresponding phrase 45 * is selected; otherwise, the default phrase is used.
SelectFormat
Note: Typically, select formatting is done via MessageFormat 50 * with a select argument type, 51 * rather than using a stand-alone SelectFormat.
MessageFormat
select
The main use case for the select format is gender based inflection. 54 * When names or nouns are inserted into sentences, their gender can affect pronouns, 55 * verb forms, articles, and adjectives. Special care needs to be 56 * taken for the case where the gender cannot be determined. 57 * The impact varies between languages:
Some other languages have noun classes that are not related to gender, 83 * but similar in grammatical use. 84 * Some African languages have around 20 noun classes.
Note:For the gender of a person in a given sentence, 87 * we usually need to distinguish only between female, male and other/unknown.
To enable localizers to create sentence patterns that take their 90 * language's gender dependencies into consideration, software has to provide 91 * information about the gender associated with a noun or name to 92 * MessageFormat. 93 * Two main cases can be distinguished:
The resulting keyword is provided to MessageFormat as a 107 * parameter separate from the name or noun it's associated with. For example, 108 * to generate a message such as "Jean went to Paris", three separate arguments 109 * would be provided: The name of the person as argument 0, the gender of 110 * the person as argument 1, and the name of the city as argument 2. 111 * The sentence pattern for English, where the gender of the person has 112 * no impact on this simple sentence, would not refer to argument 1 at all:
{0} went to {2}.
Note: The entire sentence should be included (and partially repeated) 117 * inside each phrase. Otherwise translators would have to be trained on how to 118 * move bits of the sentence in and out of the select argument of a message. 119 * (The examples below do not follow this recommendation!)
The sentence pattern for French, where the gender of the person affects 122 * the form of the participle, uses a select format based on argument 1:
{0} est {1, select, female {allée} other {allé}} à {2}.
Patterns can be nested, so that it's possible to handle interactions of 127 * number and gender where necessary. For example, if the above sentence should 128 * allow for the names of several people to be inserted, the following sentence 129 * pattern can be used (with argument 0 the list of people's names, 130 * argument 1 the number of people, argument 2 their combined gender, and 131 * argument 3 the city name):
{0} {1, plural, 135 * one {est {2, select, female {allée} other {allé}}} 136 * other {sont {2, select, female {allées} other {allés}}} 137 * }à {3}.
The SelectFormat pattern string defines the phrase output 143 * for each user-defined keyword. 144 * The pattern is a sequence of (keyword, message) pairs. 145 * A keyword is a "pattern identifier": [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
Each message is a MessageFormat pattern string enclosed in {curly braces}.
You always have to define a phrase for the default keyword 150 * other; this phrase is returned when the keyword 151 * provided to 152 * the format method matches no other keyword. 153 * If a pattern does not provide a phrase for other, the method 154 * it's provided to returns the error U_DEFAULT_KEYWORD_MISSING. 155 * 156 * Pattern_White_Space between keywords and messages is ignored. 157 * Pattern_White_Space within a message is preserved and output.
other
format
U_DEFAULT_KEYWORD_MISSING
Example: 160 * \htmlonly 161 * 162 * UErrorCode status = U_ZERO_ERROR; 163 * MessageFormat *msgFmt = new MessageFormat(UnicodeString("{0} est {1, select, female {allée} other {allé}} à Paris."), Locale("fr"), status); 164 * if (U_FAILURE(status)) { 165 * return; 166 * } 167 * FieldPosition ignore(FieldPosition::DONT_CARE); 168 * UnicodeString result; 169 * 170 * char* str1= "Kirti,female"; 171 * Formattable args1[] = {"Kirti","female"}; 172 * msgFmt->format(args1, 2, result, ignore, status); 173 * cout << "Input is " << str1 << " and result is: " << result << endl; 174 * delete msgFmt; 175 * 176 * \endhtmlonly 177 *
Kirti est allée à Paris.
UnicodeString
313 * Before calling, set parse_pos.index to the offset you want to start 314 * parsing at in the source. After calling, parse_pos.index is the end of 315 * the text you parsed. If error occurs, index is unchanged. 316 *
317 * When parsing, leading whitespace is discarded (with a successful parse), 318 * while trailing whitespace is left as is. 319 *
320 * See Format::parseObject() for more. 321 * 322 * @param source The string to be parsed into an object. 323 * @param result Formattable to be set to the parse result. 324 * If parse fails, return contents are undefined. 325 * @param parse_pos The position to start parsing at. Upon return 326 * this param is set to the position after the 327 * last character successfully parsed. If the 328 * source is not parsed successfully, this param 329 * will remain unchanged. 330 * @stable ICU 4.4 331 */ 332 virtual void parseObject(const UnicodeString& source, 333 Formattable& result, 334 ParsePosition& parse_pos) const override; 335 336 /** 337 * ICU "poor man's RTTI", returns a UClassID for this class. 338 * @stable ICU 4.4 339 */ 340 static UClassID U_EXPORT2 getStaticClassID(void); 341 342 /** 343 * ICU "poor man's RTTI", returns a UClassID for the actual class. 344 * @stable ICU 4.4 345 */ 346 virtual UClassID getDynamicClassID() const override; 347 348 private: 349 friend class MessageFormat; 350 351 SelectFormat() = delete; // default constructor not implemented. 352 353 /** 354 * Finds the SelectFormat sub-message for the given keyword, or the "other" sub-message. 355 * @param pattern A MessagePattern. 356 * @param partIndex the index of the first SelectFormat argument style part. 357 * @param keyword a keyword to be matched to one of the SelectFormat argument's keywords. 358 * @param ec Error code. 359 * @return the sub-message start part index. 360 */ 361 static int32_t findSubMessage(const MessagePattern& pattern, int32_t partIndex, 362 const UnicodeString& keyword, UErrorCode& ec); 363 364 MessagePattern msgPattern; 365 }; 366 367 U_NAMESPACE_END 368 369 #endif /* #if !UCONFIG_NO_FORMATTING */ 370 371 #endif /* U_SHOW_CPLUSPLUS_API */ 372 373 #endif // _SELFMT 374 //eof