1 /********************************************************************
3 * Copyright (c) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 * Copyright (C) 2010 , Yahoo! Inc.
6 ********************************************************************
10 * Modification History:
12 * Date Name Description
13 * 11/11/09 kirtig Finished first cut of implementation.
14 ********************************************************************/
19 #include "unicode/messagepattern.h"
20 #include "unicode/numfmt.h"
21 #include "unicode/utypes.h"
25 * \brief C++ API: SelectFormat object
28 #if !UCONFIG_NO_FORMATTING
35 * <p><code>SelectFormat</code> supports the creation of internationalized
36 * messages by selecting phrases based on keywords. The pattern specifies
37 * how to map keywords to phrases and provides a default phrase. The
38 * object provided to the format method is a string that's matched
39 * against the keywords. If there is a match, the corresponding phrase
40 * is selected; otherwise, the default phrase is used.</p>
42 * <h4>Using <code>SelectFormat</code> for Gender Agreement</h4>
44 * <p>Note: Typically, select formatting is done via <code>MessageFormat</code>
45 * with a <code>select</code> argument type,
46 * rather than using a stand-alone <code>SelectFormat</code>.</p>
48 * <p>The main use case for the select format is gender based inflection.
49 * When names or nouns are inserted into sentences, their gender can affect pronouns,
50 * verb forms, articles, and adjectives. Special care needs to be
51 * taken for the case where the gender cannot be determined.
52 * The impact varies between languages:</p>
55 * <li>English has three genders, and unknown gender is handled as a special
56 * case. Names use the gender of the named person (if known), nouns referring
57 * to people use natural gender, and inanimate objects are usually neutral.
58 * The gender only affects pronouns: "he", "she", "it", "they".
60 * <li>German differs from English in that the gender of nouns is rather
61 * arbitrary, even for nouns referring to people ("Mädchen", girl, is neutral).
62 * The gender affects pronouns ("er", "sie", "es"), articles ("der", "die",
63 * "das"), and adjective forms ("guter Mann", "gute Frau", "gutes Mädchen").
65 * <li>French has only two genders; as in German the gender of nouns
66 * is rather arbitrary - for sun and moon, the genders
67 * are the opposite of those in German. The gender affects
68 * pronouns ("il", "elle"), articles ("le", "la"),
69 * adjective forms ("bon", "bonne"), and sometimes
70 * verb forms ("allé", "allée").
72 * <li>Polish distinguishes five genders (or noun classes),
73 * human masculine, animate non-human masculine, inanimate masculine,
74 * feminine, and neuter.
77 * <p>Some other languages have noun classes that are not related to gender,
78 * but similar in grammatical use.
79 * Some African languages have around 20 noun classes.</p>
81 * <p><b>Note:</b>For the gender of a <i>person</i> in a given sentence,
82 * we usually need to distinguish only between female, male and other/unknown.</p>
84 * <p>To enable localizers to create sentence patterns that take their
85 * language's gender dependencies into consideration, software has to provide
86 * information about the gender associated with a noun or name to
87 * <code>MessageFormat</code>.
88 * Two main cases can be distinguished:</p>
91 * <li>For people, natural gender information should be maintained for each person.
92 * Keywords like "male", "female", "mixed" (for groups of people)
93 * and "unknown" could be used.
95 * <li>For nouns, grammatical gender information should be maintained for
96 * each noun and per language, e.g., in resource bundles.
97 * The keywords "masculine", "feminine", and "neuter" are commonly used,
98 * but some languages may require other keywords.
101 * <p>The resulting keyword is provided to <code>MessageFormat</code> as a
102 * parameter separate from the name or noun it's associated with. For example,
103 * to generate a message such as "Jean went to Paris", three separate arguments
104 * would be provided: The name of the person as argument 0, the gender of
105 * the person as argument 1, and the name of the city as argument 2.
106 * The sentence pattern for English, where the gender of the person has
107 * no impact on this simple sentence, would not refer to argument 1 at all:</p>
109 * <pre>{0} went to {2}.</pre>
111 * <p><b>Note:</b> The entire sentence should be included (and partially repeated)
112 * inside each phrase. Otherwise translators would have to be trained on how to
113 * move bits of the sentence in and out of the select argument of a message.
114 * (The examples below do not follow this recommendation!)</p>
116 * <p>The sentence pattern for French, where the gender of the person affects
117 * the form of the participle, uses a select format based on argument 1:</p>
119 * \htmlonly<pre>{0} est {1, select, female {allée} other {allé}} à {2}.</pre>\endhtmlonly
121 * <p>Patterns can be nested, so that it's possible to handle interactions of
122 * number and gender where necessary. For example, if the above sentence should
123 * allow for the names of several people to be inserted, the following sentence
124 * pattern can be used (with argument 0 the list of people's names,
125 * argument 1 the number of people, argument 2 their combined gender, and
126 * argument 3 the city name):</p>
129 * <pre>{0} {1, plural,
130 * one {est {2, select, female {allée} other {allé}}}
131 * other {sont {2, select, female {allées} other {allés}}}
132 * }à {3}.</pre>
135 * <h4>Patterns and Their Interpretation</h4>
137 * <p>The <code>SelectFormat</code> pattern string defines the phrase output
138 * for each user-defined keyword.
139 * The pattern is a sequence of (keyword, message) pairs.
140 * A keyword is a "pattern identifier": [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+</p>
142 * <p>Each message is a MessageFormat pattern string enclosed in {curly braces}.</p>
144 * <p>You always have to define a phrase for the default keyword
145 * <code>other</code>; this phrase is returned when the keyword
147 * the <code>format</code> method matches no other keyword.
148 * If a pattern does not provide a phrase for <code>other</code>, the method
149 * it's provided to returns the error <code>U_DEFAULT_KEYWORD_MISSING</code>.
151 * Pattern_White_Space between keywords and messages is ignored.
152 * Pattern_White_Space within a message is preserved and output.</p>
157 * UErrorCode status = U_ZERO_ERROR;
158 * MessageFormat *msgFmt = new MessageFormat(UnicodeString("{0} est {1, select, female {allée} other {allé}} à Paris."), Locale("fr"), status);
159 * if (U_FAILURE(status)) {
162 * FieldPosition ignore(FieldPosition::DONT_CARE);
163 * UnicodeString result;
165 * char* str1= "Kirti,female";
166 * Formattable args1[] = {"Kirti","female"};
167 * msgFmt->format(args1, 2, result, ignore, status);
168 * cout << "Input is " << str1 << " and result is: " << result << endl;
175 * Produces the output:<br>
177 * <code>Kirti est allée à Paris.</code>
183 class U_I18N_API SelectFormat
: public Format
{
187 * Creates a new <code>SelectFormat</code> for a given pattern string.
188 * @param pattern the pattern for this <code>SelectFormat</code>.
189 * errors are returned to status if the pattern is invalid.
190 * @param status output param set to success/failure code on exit, which
191 * must not indicate a failure before the function call.
194 SelectFormat(const UnicodeString
& pattern
, UErrorCode
& status
);
200 SelectFormat(const SelectFormat
& other
);
206 virtual ~SelectFormat();
209 * Sets the pattern used by this select format.
210 * for the keyword rules.
211 * Patterns and their interpretation are specified in the class description.
213 * @param pattern the pattern for this select format
214 * errors are returned to status if the pattern is invalid.
215 * @param status output param set to success/failure code on exit, which
216 * must not indicate a failure before the function call.
219 void applyPattern(const UnicodeString
& pattern
, UErrorCode
& status
);
222 using Format::format
;
225 * Selects the phrase for the given keyword
227 * @param keyword The keyword that is used to select an alternative.
228 * @param appendTo output parameter to receive result.
229 * result is appended to existing contents.
230 * @param pos On input: an alignment field, if desired.
231 * On output: the offsets of the alignment field.
232 * @param status output param set to success/failure code on exit, which
233 * must not indicate a failure before the function call.
234 * @return Reference to 'appendTo' parameter.
237 UnicodeString
& format(const UnicodeString
& keyword
,
238 UnicodeString
& appendTo
,
240 UErrorCode
& status
) const;
243 * Assignment operator
245 * @param other the SelectFormat object to copy from.
248 SelectFormat
& operator=(const SelectFormat
& other
);
251 * Return true if another object is semantically equal to this one.
253 * @param other the SelectFormat object to be compared with.
254 * @return true if other is semantically equal to this.
257 virtual UBool
operator==(const Format
& other
) const;
260 * Return true if another object is semantically unequal to this one.
262 * @param other the SelectFormat object to be compared with.
263 * @return true if other is semantically unequal to this.
266 virtual UBool
operator!=(const Format
& other
) const;
269 * Clones this Format object polymorphically. The caller owns the
270 * result and should delete it when done.
273 virtual Format
* clone(void) const;
276 * Format an object to produce a string.
277 * This method handles keyword strings.
278 * If the Formattable object is not a <code>UnicodeString</code>,
279 * then it returns a failing UErrorCode.
281 * @param obj A keyword string that is used to select an alternative.
282 * @param appendTo output parameter to receive result.
283 * Result is appended to existing contents.
284 * @param pos On input: an alignment field, if desired.
285 * On output: the offsets of the alignment field.
286 * @param status output param filled with success/failure status.
287 * @return Reference to 'appendTo' parameter.
290 UnicodeString
& format(const Formattable
& obj
,
291 UnicodeString
& appendTo
,
293 UErrorCode
& status
) const;
296 * Returns the pattern from applyPattern() or constructor.
298 * @param appendTo output parameter to receive result.
299 * Result is appended to existing contents.
300 * @return the UnicodeString with inserted pattern.
303 UnicodeString
& toPattern(UnicodeString
& appendTo
);
306 * This method is not yet supported by <code>SelectFormat</code>.
308 * Before calling, set parse_pos.index to the offset you want to start
309 * parsing at in the source. After calling, parse_pos.index is the end of
310 * the text you parsed. If error occurs, index is unchanged.
312 * When parsing, leading whitespace is discarded (with a successful parse),
313 * while trailing whitespace is left as is.
315 * See Format::parseObject() for more.
317 * @param source The string to be parsed into an object.
318 * @param result Formattable to be set to the parse result.
319 * If parse fails, return contents are undefined.
320 * @param parse_pos The position to start parsing at. Upon return
321 * this param is set to the position after the
322 * last character successfully parsed. If the
323 * source is not parsed successfully, this param
324 * will remain unchanged.
327 virtual void parseObject(const UnicodeString
& source
,
329 ParsePosition
& parse_pos
) const;
332 * ICU "poor man's RTTI", returns a UClassID for this class.
335 static UClassID U_EXPORT2
getStaticClassID(void);
338 * ICU "poor man's RTTI", returns a UClassID for the actual class.
341 virtual UClassID
getDynamicClassID() const;
344 friend class MessageFormat
;
346 SelectFormat(); // default constructor not implemented.
349 * Finds the SelectFormat sub-message for the given keyword, or the "other" sub-message.
350 * @param pattern A MessagePattern.
351 * @param partIndex the index of the first SelectFormat argument style part.
352 * @param keyword a keyword to be matched to one of the SelectFormat argument's keywords.
353 * @param ec Error code.
354 * @return the sub-message start part index.
356 static int32_t findSubMessage(const MessagePattern
& pattern
, int32_t partIndex
,
357 const UnicodeString
& keyword
, UErrorCode
& ec
);
359 MessagePattern msgPattern
;
364 #endif /* #if !UCONFIG_NO_FORMATTING */