]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
729e4ab9 A |
3 | /******************************************************************** |
4 | * COPYRIGHT: | |
4388f060 | 5 | * Copyright (c) 1997-2011, International Business Machines Corporation and |
729e4ab9 A |
6 | * others. All Rights Reserved. |
7 | * Copyright (C) 2010 , Yahoo! Inc. | |
8 | ******************************************************************** | |
9 | * | |
10 | * File SELFMT.H | |
11 | * | |
12 | * Modification History: | |
13 | * | |
14 | * Date Name Description | |
15 | * 11/11/09 kirtig Finished first cut of implementation. | |
16 | ********************************************************************/ | |
17 | ||
18 | #ifndef SELFMT | |
19 | #define SELFMT | |
20 | ||
4388f060 | 21 | #include "unicode/messagepattern.h" |
729e4ab9 | 22 | #include "unicode/numfmt.h" |
4388f060 | 23 | #include "unicode/utypes.h" |
729e4ab9 A |
24 | |
25 | /** | |
26 | * \file | |
27 | * \brief C++ API: SelectFormat object | |
28 | */ | |
29 | ||
30 | #if !UCONFIG_NO_FORMATTING | |
31 | ||
f3c0d7a5 | 32 | #if U_SHOW_CPLUSPLUS_API |
729e4ab9 A |
33 | U_NAMESPACE_BEGIN |
34 | ||
4388f060 | 35 | class MessageFormat; |
729e4ab9 A |
36 | |
37 | /** | |
38 | * <p><code>SelectFormat</code> supports the creation of internationalized | |
39 | * messages by selecting phrases based on keywords. The pattern specifies | |
40 | * how to map keywords to phrases and provides a default phrase. The | |
41 | * object provided to the format method is a string that's matched | |
42 | * against the keywords. If there is a match, the corresponding phrase | |
43 | * is selected; otherwise, the default phrase is used.</p> | |
44 | * | |
45 | * <h4>Using <code>SelectFormat</code> for Gender Agreement</h4> | |
46 | * | |
4388f060 A |
47 | * <p>Note: Typically, select formatting is done via <code>MessageFormat</code> |
48 | * with a <code>select</code> argument type, | |
49 | * rather than using a stand-alone <code>SelectFormat</code>.</p> | |
50 | * | |
729e4ab9 A |
51 | * <p>The main use case for the select format is gender based inflection. |
52 | * When names or nouns are inserted into sentences, their gender can affect pronouns, | |
53 | * verb forms, articles, and adjectives. Special care needs to be | |
54 | * taken for the case where the gender cannot be determined. | |
55 | * The impact varies between languages:</p> | |
56 | * \htmlonly | |
57 | * <ul> | |
58 | * <li>English has three genders, and unknown gender is handled as a special | |
59 | * case. Names use the gender of the named person (if known), nouns referring | |
60 | * to people use natural gender, and inanimate objects are usually neutral. | |
61 | * The gender only affects pronouns: "he", "she", "it", "they". | |
62 | * | |
63 | * <li>German differs from English in that the gender of nouns is rather | |
64 | * arbitrary, even for nouns referring to people ("Mädchen", girl, is neutral). | |
65 | * The gender affects pronouns ("er", "sie", "es"), articles ("der", "die", | |
66 | * "das"), and adjective forms ("guter Mann", "gute Frau", "gutes Mädchen"). | |
67 | * | |
68 | * <li>French has only two genders; as in German the gender of nouns | |
69 | * is rather arbitrary - for sun and moon, the genders | |
70 | * are the opposite of those in German. The gender affects | |
71 | * pronouns ("il", "elle"), articles ("le", "la"), | |
72 | * adjective forms ("bon", "bonne"), and sometimes | |
73 | * verb forms ("allé", "allée"). | |
74 | * | |
75 | * <li>Polish distinguishes five genders (or noun classes), | |
76 | * human masculine, animate non-human masculine, inanimate masculine, | |
77 | * feminine, and neuter. | |
78 | * </ul> | |
79 | * \endhtmlonly | |
80 | * <p>Some other languages have noun classes that are not related to gender, | |
81 | * but similar in grammatical use. | |
82 | * Some African languages have around 20 noun classes.</p> | |
83 | * | |
4388f060 A |
84 | * <p><b>Note:</b>For the gender of a <i>person</i> in a given sentence, |
85 | * we usually need to distinguish only between female, male and other/unknown.</p> | |
86 | * | |
729e4ab9 A |
87 | * <p>To enable localizers to create sentence patterns that take their |
88 | * language's gender dependencies into consideration, software has to provide | |
89 | * information about the gender associated with a noun or name to | |
90 | * <code>MessageFormat</code>. | |
91 | * Two main cases can be distinguished:</p> | |
92 | * | |
93 | * <ul> | |
94 | * <li>For people, natural gender information should be maintained for each person. | |
4388f060 A |
95 | * Keywords like "male", "female", "mixed" (for groups of people) |
96 | * and "unknown" could be used. | |
729e4ab9 A |
97 | * |
98 | * <li>For nouns, grammatical gender information should be maintained for | |
99 | * each noun and per language, e.g., in resource bundles. | |
100 | * The keywords "masculine", "feminine", and "neuter" are commonly used, | |
101 | * but some languages may require other keywords. | |
102 | * </ul> | |
103 | * | |
104 | * <p>The resulting keyword is provided to <code>MessageFormat</code> as a | |
105 | * parameter separate from the name or noun it's associated with. For example, | |
106 | * to generate a message such as "Jean went to Paris", three separate arguments | |
107 | * would be provided: The name of the person as argument 0, the gender of | |
108 | * the person as argument 1, and the name of the city as argument 2. | |
109 | * The sentence pattern for English, where the gender of the person has | |
110 | * no impact on this simple sentence, would not refer to argument 1 at all:</p> | |
111 | * | |
112 | * <pre>{0} went to {2}.</pre> | |
113 | * | |
4388f060 A |
114 | * <p><b>Note:</b> The entire sentence should be included (and partially repeated) |
115 | * inside each phrase. Otherwise translators would have to be trained on how to | |
116 | * move bits of the sentence in and out of the select argument of a message. | |
117 | * (The examples below do not follow this recommendation!)</p> | |
118 | * | |
729e4ab9 A |
119 | * <p>The sentence pattern for French, where the gender of the person affects |
120 | * the form of the participle, uses a select format based on argument 1:</p> | |
121 | * | |
122 | * \htmlonly<pre>{0} est {1, select, female {allée} other {allé}} à {2}.</pre>\endhtmlonly | |
123 | * | |
124 | * <p>Patterns can be nested, so that it's possible to handle interactions of | |
125 | * number and gender where necessary. For example, if the above sentence should | |
126 | * allow for the names of several people to be inserted, the following sentence | |
127 | * pattern can be used (with argument 0 the list of people's names, | |
128 | * argument 1 the number of people, argument 2 their combined gender, and | |
129 | * argument 3 the city name):</p> | |
130 | * | |
131 | * \htmlonly | |
132 | * <pre>{0} {1, plural, | |
133 | * one {est {2, select, female {allée} other {allé}}} | |
134 | * other {sont {2, select, female {allées} other {allés}}} | |
135 | * }à {3}.</pre> | |
136 | * \endhtmlonly | |
137 | * | |
138 | * <h4>Patterns and Their Interpretation</h4> | |
139 | * | |
4388f060 | 140 | * <p>The <code>SelectFormat</code> pattern string defines the phrase output |
729e4ab9 | 141 | * for each user-defined keyword. |
4388f060 A |
142 | * The pattern is a sequence of (keyword, message) pairs. |
143 | * A keyword is a "pattern identifier": [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+</p> | |
144 | * | |
145 | * <p>Each message is a MessageFormat pattern string enclosed in {curly braces}.</p> | |
729e4ab9 | 146 | * |
4388f060 | 147 | * <p>You always have to define a phrase for the default keyword |
729e4ab9 A |
148 | * <code>other</code>; this phrase is returned when the keyword |
149 | * provided to | |
150 | * the <code>format</code> method matches no other keyword. | |
151 | * If a pattern does not provide a phrase for <code>other</code>, the method | |
152 | * it's provided to returns the error <code>U_DEFAULT_KEYWORD_MISSING</code>. | |
729e4ab9 | 153 | * <br> |
4388f060 A |
154 | * Pattern_White_Space between keywords and messages is ignored. |
155 | * Pattern_White_Space within a message is preserved and output.</p> | |
729e4ab9 | 156 | * |
4388f060 | 157 | * <p><pre>Example: |
729e4ab9 A |
158 | * \htmlonly |
159 | * | |
160 | * UErrorCode status = U_ZERO_ERROR; | |
161 | * MessageFormat *msgFmt = new MessageFormat(UnicodeString("{0} est {1, select, female {allée} other {allé}} à Paris."), Locale("fr"), status); | |
162 | * if (U_FAILURE(status)) { | |
163 | * return; | |
164 | * } | |
165 | * FieldPosition ignore(FieldPosition::DONT_CARE); | |
166 | * UnicodeString result; | |
167 | * | |
168 | * char* str1= "Kirti,female"; | |
169 | * Formattable args1[] = {"Kirti","female"}; | |
170 | * msgFmt->format(args1, 2, result, ignore, status); | |
171 | * cout << "Input is " << str1 << " and result is: " << result << endl; | |
172 | * delete msgFmt; | |
173 | * | |
174 | * \endhtmlonly | |
4388f060 | 175 | * </pre> |
729e4ab9 A |
176 | * </p> |
177 | * | |
178 | * Produces the output:<br> | |
179 | * \htmlonly | |
180 | * <code>Kirti est allée à Paris.</code> | |
181 | * \endhtmlonly | |
182 | * | |
183 | * @stable ICU 4.4 | |
184 | */ | |
185 | ||
186 | class U_I18N_API SelectFormat : public Format { | |
187 | public: | |
188 | ||
189 | /** | |
190 | * Creates a new <code>SelectFormat</code> for a given pattern string. | |
191 | * @param pattern the pattern for this <code>SelectFormat</code>. | |
192 | * errors are returned to status if the pattern is invalid. | |
193 | * @param status output param set to success/failure code on exit, which | |
194 | * must not indicate a failure before the function call. | |
195 | * @stable ICU 4.4 | |
196 | */ | |
197 | SelectFormat(const UnicodeString& pattern, UErrorCode& status); | |
198 | ||
199 | /** | |
200 | * copy constructor. | |
201 | * @stable ICU 4.4 | |
202 | */ | |
203 | SelectFormat(const SelectFormat& other); | |
204 | ||
205 | /** | |
206 | * Destructor. | |
207 | * @stable ICU 4.4 | |
208 | */ | |
209 | virtual ~SelectFormat(); | |
210 | ||
211 | /** | |
212 | * Sets the pattern used by this select format. | |
213 | * for the keyword rules. | |
214 | * Patterns and their interpretation are specified in the class description. | |
215 | * | |
216 | * @param pattern the pattern for this select format | |
217 | * errors are returned to status if the pattern is invalid. | |
218 | * @param status output param set to success/failure code on exit, which | |
219 | * must not indicate a failure before the function call. | |
220 | * @stable ICU 4.4 | |
221 | */ | |
222 | void applyPattern(const UnicodeString& pattern, UErrorCode& status); | |
223 | ||
224 | ||
225 | using Format::format; | |
226 | ||
227 | /** | |
228 | * Selects the phrase for the given keyword | |
229 | * | |
230 | * @param keyword The keyword that is used to select an alternative. | |
231 | * @param appendTo output parameter to receive result. | |
232 | * result is appended to existing contents. | |
233 | * @param pos On input: an alignment field, if desired. | |
234 | * On output: the offsets of the alignment field. | |
235 | * @param status output param set to success/failure code on exit, which | |
236 | * must not indicate a failure before the function call. | |
237 | * @return Reference to 'appendTo' parameter. | |
238 | * @stable ICU 4.4 | |
239 | */ | |
240 | UnicodeString& format(const UnicodeString& keyword, | |
241 | UnicodeString& appendTo, | |
242 | FieldPosition& pos, | |
243 | UErrorCode& status) const; | |
244 | ||
245 | /** | |
246 | * Assignment operator | |
247 | * | |
248 | * @param other the SelectFormat object to copy from. | |
249 | * @stable ICU 4.4 | |
250 | */ | |
251 | SelectFormat& operator=(const SelectFormat& other); | |
252 | ||
253 | /** | |
254 | * Return true if another object is semantically equal to this one. | |
255 | * | |
256 | * @param other the SelectFormat object to be compared with. | |
257 | * @return true if other is semantically equal to this. | |
258 | * @stable ICU 4.4 | |
259 | */ | |
260 | virtual UBool operator==(const Format& other) const; | |
261 | ||
262 | /** | |
263 | * Return true if another object is semantically unequal to this one. | |
264 | * | |
265 | * @param other the SelectFormat object to be compared with. | |
266 | * @return true if other is semantically unequal to this. | |
267 | * @stable ICU 4.4 | |
268 | */ | |
269 | virtual UBool operator!=(const Format& other) const; | |
270 | ||
271 | /** | |
272 | * Clones this Format object polymorphically. The caller owns the | |
273 | * result and should delete it when done. | |
274 | * @stable ICU 4.4 | |
275 | */ | |
276 | virtual Format* clone(void) const; | |
277 | ||
278 | /** | |
279 | * Format an object to produce a string. | |
280 | * This method handles keyword strings. | |
281 | * If the Formattable object is not a <code>UnicodeString</code>, | |
282 | * then it returns a failing UErrorCode. | |
283 | * | |
284 | * @param obj A keyword string that is used to select an alternative. | |
285 | * @param appendTo output parameter to receive result. | |
286 | * Result is appended to existing contents. | |
287 | * @param pos On input: an alignment field, if desired. | |
288 | * On output: the offsets of the alignment field. | |
289 | * @param status output param filled with success/failure status. | |
290 | * @return Reference to 'appendTo' parameter. | |
291 | * @stable ICU 4.4 | |
292 | */ | |
293 | UnicodeString& format(const Formattable& obj, | |
294 | UnicodeString& appendTo, | |
295 | FieldPosition& pos, | |
296 | UErrorCode& status) const; | |
297 | ||
298 | /** | |
299 | * Returns the pattern from applyPattern() or constructor. | |
300 | * | |
301 | * @param appendTo output parameter to receive result. | |
302 | * Result is appended to existing contents. | |
303 | * @return the UnicodeString with inserted pattern. | |
304 | * @stable ICU 4.4 | |
305 | */ | |
306 | UnicodeString& toPattern(UnicodeString& appendTo); | |
307 | ||
308 | /** | |
309 | * This method is not yet supported by <code>SelectFormat</code>. | |
310 | * <P> | |
311 | * Before calling, set parse_pos.index to the offset you want to start | |
312 | * parsing at in the source. After calling, parse_pos.index is the end of | |
313 | * the text you parsed. If error occurs, index is unchanged. | |
314 | * <P> | |
315 | * When parsing, leading whitespace is discarded (with a successful parse), | |
316 | * while trailing whitespace is left as is. | |
317 | * <P> | |
318 | * See Format::parseObject() for more. | |
319 | * | |
320 | * @param source The string to be parsed into an object. | |
321 | * @param result Formattable to be set to the parse result. | |
322 | * If parse fails, return contents are undefined. | |
323 | * @param parse_pos The position to start parsing at. Upon return | |
324 | * this param is set to the position after the | |
325 | * last character successfully parsed. If the | |
326 | * source is not parsed successfully, this param | |
327 | * will remain unchanged. | |
328 | * @stable ICU 4.4 | |
329 | */ | |
330 | virtual void parseObject(const UnicodeString& source, | |
331 | Formattable& result, | |
332 | ParsePosition& parse_pos) const; | |
333 | ||
334 | /** | |
335 | * ICU "poor man's RTTI", returns a UClassID for this class. | |
336 | * @stable ICU 4.4 | |
337 | */ | |
338 | static UClassID U_EXPORT2 getStaticClassID(void); | |
339 | ||
340 | /** | |
341 | * ICU "poor man's RTTI", returns a UClassID for the actual class. | |
342 | * @stable ICU 4.4 | |
343 | */ | |
344 | virtual UClassID getDynamicClassID() const; | |
345 | ||
346 | private: | |
4388f060 | 347 | friend class MessageFormat; |
729e4ab9 A |
348 | |
349 | SelectFormat(); // default constructor not implemented. | |
729e4ab9 | 350 | |
4388f060 A |
351 | /** |
352 | * Finds the SelectFormat sub-message for the given keyword, or the "other" sub-message. | |
353 | * @param pattern A MessagePattern. | |
354 | * @param partIndex the index of the first SelectFormat argument style part. | |
355 | * @param keyword a keyword to be matched to one of the SelectFormat argument's keywords. | |
356 | * @param ec Error code. | |
357 | * @return the sub-message start part index. | |
358 | */ | |
359 | static int32_t findSubMessage(const MessagePattern& pattern, int32_t partIndex, | |
360 | const UnicodeString& keyword, UErrorCode& ec); | |
361 | ||
362 | MessagePattern msgPattern; | |
729e4ab9 A |
363 | }; |
364 | ||
365 | U_NAMESPACE_END | |
f3c0d7a5 | 366 | #endif // U_SHOW_CPLUSPLUS_API |
729e4ab9 A |
367 | |
368 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
369 | ||
370 | #endif // _SELFMT | |
371 | //eof |