jpayne@69
|
1 // © 2016 and later: Unicode, Inc. and others.
|
jpayne@69
|
2 // License & terms of use: http://www.unicode.org/copyright.html
|
jpayne@69
|
3 /*
|
jpayne@69
|
4 *******************************************************************************
|
jpayne@69
|
5 * Copyright (C) 2007-2014, International Business Machines Corporation and
|
jpayne@69
|
6 * others. All Rights Reserved.
|
jpayne@69
|
7 *******************************************************************************
|
jpayne@69
|
8 *
|
jpayne@69
|
9
|
jpayne@69
|
10 * File PLURFMT.H
|
jpayne@69
|
11 ********************************************************************************
|
jpayne@69
|
12 */
|
jpayne@69
|
13
|
jpayne@69
|
14 #ifndef PLURFMT
|
jpayne@69
|
15 #define PLURFMT
|
jpayne@69
|
16
|
jpayne@69
|
17 #include "unicode/utypes.h"
|
jpayne@69
|
18
|
jpayne@69
|
19 #if U_SHOW_CPLUSPLUS_API
|
jpayne@69
|
20
|
jpayne@69
|
21 /**
|
jpayne@69
|
22 * \file
|
jpayne@69
|
23 * \brief C++ API: PluralFormat object
|
jpayne@69
|
24 */
|
jpayne@69
|
25
|
jpayne@69
|
26 #if !UCONFIG_NO_FORMATTING
|
jpayne@69
|
27
|
jpayne@69
|
28 #include "unicode/messagepattern.h"
|
jpayne@69
|
29 #include "unicode/numfmt.h"
|
jpayne@69
|
30 #include "unicode/plurrule.h"
|
jpayne@69
|
31
|
jpayne@69
|
32 U_NAMESPACE_BEGIN
|
jpayne@69
|
33
|
jpayne@69
|
34 class Hashtable;
|
jpayne@69
|
35 class NFRule;
|
jpayne@69
|
36
|
jpayne@69
|
37 /**
|
jpayne@69
|
38 * <p>
|
jpayne@69
|
39 * <code>PluralFormat</code> supports the creation of internationalized
|
jpayne@69
|
40 * messages with plural inflection. It is based on <i>plural
|
jpayne@69
|
41 * selection</i>, i.e. the caller specifies messages for each
|
jpayne@69
|
42 * plural case that can appear in the user's language and the
|
jpayne@69
|
43 * <code>PluralFormat</code> selects the appropriate message based on
|
jpayne@69
|
44 * the number.
|
jpayne@69
|
45 * </p>
|
jpayne@69
|
46 * <h4>The Problem of Plural Forms in Internationalized Messages</h4>
|
jpayne@69
|
47 * <p>
|
jpayne@69
|
48 * Different languages have different ways to inflect
|
jpayne@69
|
49 * plurals. Creating internationalized messages that include plural
|
jpayne@69
|
50 * forms is only feasible when the framework is able to handle plural
|
jpayne@69
|
51 * forms of <i>all</i> languages correctly. <code>ChoiceFormat</code>
|
jpayne@69
|
52 * doesn't handle this well, because it attaches a number interval to
|
jpayne@69
|
53 * each message and selects the message whose interval contains a
|
jpayne@69
|
54 * given number. This can only handle a finite number of
|
jpayne@69
|
55 * intervals. But in some languages, like Polish, one plural case
|
jpayne@69
|
56 * applies to infinitely many intervals (e.g., the plural case applies to
|
jpayne@69
|
57 * numbers ending with 2, 3, or 4 except those ending with 12, 13, or
|
jpayne@69
|
58 * 14). Thus <code>ChoiceFormat</code> is not adequate.
|
jpayne@69
|
59 * </p><p>
|
jpayne@69
|
60 * <code>PluralFormat</code> deals with this by breaking the problem
|
jpayne@69
|
61 * into two parts:
|
jpayne@69
|
62 * <ul>
|
jpayne@69
|
63 * <li>It uses <code>PluralRules</code> that can define more complex
|
jpayne@69
|
64 * conditions for a plural case than just a single interval. These plural
|
jpayne@69
|
65 * rules define both what plural cases exist in a language, and to
|
jpayne@69
|
66 * which numbers these cases apply.
|
jpayne@69
|
67 * <li>It provides predefined plural rules for many languages. Thus, the programmer
|
jpayne@69
|
68 * need not worry about the plural cases of a language and
|
jpayne@69
|
69 * does not have to define the plural cases; they can simply
|
jpayne@69
|
70 * use the predefined keywords. The whole plural formatting of messages can
|
jpayne@69
|
71 * be done using localized patterns from resource bundles. For predefined plural
|
jpayne@69
|
72 * rules, see the CLDR <i>Language Plural Rules</i> page at
|
jpayne@69
|
73 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
|
jpayne@69
|
74 * </ul>
|
jpayne@69
|
75 * </p>
|
jpayne@69
|
76 * <h4>Usage of <code>PluralFormat</code></h4>
|
jpayne@69
|
77 * <p>Note: Typically, plural formatting is done via <code>MessageFormat</code>
|
jpayne@69
|
78 * with a <code>plural</code> argument type,
|
jpayne@69
|
79 * rather than using a stand-alone <code>PluralFormat</code>.
|
jpayne@69
|
80 * </p><p>
|
jpayne@69
|
81 * This discussion assumes that you use <code>PluralFormat</code> with
|
jpayne@69
|
82 * a predefined set of plural rules. You can create one using one of
|
jpayne@69
|
83 * the constructors that takes a <code>locale</code> object. To
|
jpayne@69
|
84 * specify the message pattern, you can either pass it to the
|
jpayne@69
|
85 * constructor or set it explicitly using the
|
jpayne@69
|
86 * <code>applyPattern()</code> method. The <code>format()</code>
|
jpayne@69
|
87 * method takes a number object and selects the message of the
|
jpayne@69
|
88 * matching plural case. This message will be returned.
|
jpayne@69
|
89 * </p>
|
jpayne@69
|
90 * <h5>Patterns and Their Interpretation</h5>
|
jpayne@69
|
91 * <p>
|
jpayne@69
|
92 * The pattern text defines the message output for each plural case of the
|
jpayne@69
|
93 * specified locale. Syntax:
|
jpayne@69
|
94 * <pre>
|
jpayne@69
|
95 * pluralStyle = [offsetValue] (selector '{' message '}')+
|
jpayne@69
|
96 * offsetValue = "offset:" number
|
jpayne@69
|
97 * selector = explicitValue | keyword
|
jpayne@69
|
98 * explicitValue = '=' number // adjacent, no white space in between
|
jpayne@69
|
99 * keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
|
jpayne@69
|
100 * message: see {@link MessageFormat}
|
jpayne@69
|
101 * </pre>
|
jpayne@69
|
102 * Pattern_White_Space between syntax elements is ignored, except
|
jpayne@69
|
103 * between the {curly braces} and their sub-message,
|
jpayne@69
|
104 * and between the '=' and the number of an explicitValue.
|
jpayne@69
|
105 *
|
jpayne@69
|
106 * </p><p>
|
jpayne@69
|
107 * There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and
|
jpayne@69
|
108 * 'other'. You always have to define a message text for the default plural case
|
jpayne@69
|
109 * <code>other</code> which is contained in every rule set.
|
jpayne@69
|
110 * If you do not specify a message text for a particular plural case, the
|
jpayne@69
|
111 * message text of the plural case <code>other</code> gets assigned to this
|
jpayne@69
|
112 * plural case.
|
jpayne@69
|
113 * </p><p>
|
jpayne@69
|
114 * When formatting, the input number is first matched against the explicitValue clauses.
|
jpayne@69
|
115 * If there is no exact-number match, then a keyword is selected by calling
|
jpayne@69
|
116 * the <code>PluralRules</code> with the input number <em>minus the offset</em>.
|
jpayne@69
|
117 * (The offset defaults to 0 if it is omitted from the pattern string.)
|
jpayne@69
|
118 * If there is no clause with that keyword, then the "other" clauses is returned.
|
jpayne@69
|
119 * </p><p>
|
jpayne@69
|
120 * An unquoted pound sign (<code>#</code>) in the selected sub-message
|
jpayne@69
|
121 * itself (i.e., outside of arguments nested in the sub-message)
|
jpayne@69
|
122 * is replaced by the input number minus the offset.
|
jpayne@69
|
123 * The number-minus-offset value is formatted using a
|
jpayne@69
|
124 * <code>NumberFormat</code> for the <code>PluralFormat</code>'s locale. If you
|
jpayne@69
|
125 * need special number formatting, you have to use a <code>MessageFormat</code>
|
jpayne@69
|
126 * and explicitly specify a <code>NumberFormat</code> argument.
|
jpayne@69
|
127 * <strong>Note:</strong> That argument is formatting without subtracting the offset!
|
jpayne@69
|
128 * If you need a custom format and have a non-zero offset, then you need to pass the
|
jpayne@69
|
129 * number-minus-offset value as a separate parameter.
|
jpayne@69
|
130 * </p>
|
jpayne@69
|
131 * For a usage example, see the {@link MessageFormat} class documentation.
|
jpayne@69
|
132 *
|
jpayne@69
|
133 * <h4>Defining Custom Plural Rules</h4>
|
jpayne@69
|
134 * <p>If you need to use <code>PluralFormat</code> with custom rules, you can
|
jpayne@69
|
135 * create a <code>PluralRules</code> object and pass it to
|
jpayne@69
|
136 * <code>PluralFormat</code>'s constructor. If you also specify a locale in this
|
jpayne@69
|
137 * constructor, this locale will be used to format the number in the message
|
jpayne@69
|
138 * texts.
|
jpayne@69
|
139 * </p><p>
|
jpayne@69
|
140 * For more information about <code>PluralRules</code>, see
|
jpayne@69
|
141 * {@link PluralRules}.
|
jpayne@69
|
142 * </p>
|
jpayne@69
|
143 *
|
jpayne@69
|
144 * ported from Java
|
jpayne@69
|
145 * @stable ICU 4.0
|
jpayne@69
|
146 */
|
jpayne@69
|
147
|
jpayne@69
|
148 class U_I18N_API PluralFormat : public Format {
|
jpayne@69
|
149 public:
|
jpayne@69
|
150
|
jpayne@69
|
151 /**
|
jpayne@69
|
152 * Creates a new cardinal-number <code>PluralFormat</code> for the default locale.
|
jpayne@69
|
153 * This locale will be used to get the set of plural rules and for standard
|
jpayne@69
|
154 * number formatting.
|
jpayne@69
|
155 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
156 * must not indicate a failure before the function call.
|
jpayne@69
|
157 * @stable ICU 4.0
|
jpayne@69
|
158 */
|
jpayne@69
|
159 PluralFormat(UErrorCode& status);
|
jpayne@69
|
160
|
jpayne@69
|
161 /**
|
jpayne@69
|
162 * Creates a new cardinal-number <code>PluralFormat</code> for a given locale.
|
jpayne@69
|
163 * @param locale the <code>PluralFormat</code> will be configured with
|
jpayne@69
|
164 * rules for this locale. This locale will also be used for
|
jpayne@69
|
165 * standard number formatting.
|
jpayne@69
|
166 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
167 * must not indicate a failure before the function call.
|
jpayne@69
|
168 * @stable ICU 4.0
|
jpayne@69
|
169 */
|
jpayne@69
|
170 PluralFormat(const Locale& locale, UErrorCode& status);
|
jpayne@69
|
171
|
jpayne@69
|
172 /**
|
jpayne@69
|
173 * Creates a new <code>PluralFormat</code> for a given set of rules.
|
jpayne@69
|
174 * The standard number formatting will be done using the default locale.
|
jpayne@69
|
175 * @param rules defines the behavior of the <code>PluralFormat</code>
|
jpayne@69
|
176 * object.
|
jpayne@69
|
177 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
178 * must not indicate a failure before the function call.
|
jpayne@69
|
179 * @stable ICU 4.0
|
jpayne@69
|
180 */
|
jpayne@69
|
181 PluralFormat(const PluralRules& rules, UErrorCode& status);
|
jpayne@69
|
182
|
jpayne@69
|
183 /**
|
jpayne@69
|
184 * Creates a new <code>PluralFormat</code> for a given set of rules.
|
jpayne@69
|
185 * The standard number formatting will be done using the given locale.
|
jpayne@69
|
186 * @param locale the default number formatting will be done using this
|
jpayne@69
|
187 * locale.
|
jpayne@69
|
188 * @param rules defines the behavior of the <code>PluralFormat</code>
|
jpayne@69
|
189 * object.
|
jpayne@69
|
190 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
191 * must not indicate a failure before the function call.
|
jpayne@69
|
192 * @stable ICU 4.0
|
jpayne@69
|
193 * <p>
|
jpayne@69
|
194 * <h4>Sample code</h4>
|
jpayne@69
|
195 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample1
|
jpayne@69
|
196 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample
|
jpayne@69
|
197 * <p>
|
jpayne@69
|
198 */
|
jpayne@69
|
199 PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status);
|
jpayne@69
|
200
|
jpayne@69
|
201 /**
|
jpayne@69
|
202 * Creates a new <code>PluralFormat</code> for the plural type.
|
jpayne@69
|
203 * The standard number formatting will be done using the given locale.
|
jpayne@69
|
204 * @param locale the default number formatting will be done using this
|
jpayne@69
|
205 * locale.
|
jpayne@69
|
206 * @param type The plural type (e.g., cardinal or ordinal).
|
jpayne@69
|
207 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
208 * must not indicate a failure before the function call.
|
jpayne@69
|
209 * @stable ICU 50
|
jpayne@69
|
210 */
|
jpayne@69
|
211 PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status);
|
jpayne@69
|
212
|
jpayne@69
|
213 /**
|
jpayne@69
|
214 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string.
|
jpayne@69
|
215 * The default locale will be used to get the set of plural rules and for
|
jpayne@69
|
216 * standard number formatting.
|
jpayne@69
|
217 * @param pattern the pattern for this <code>PluralFormat</code>.
|
jpayne@69
|
218 * errors are returned to status if the pattern is invalid.
|
jpayne@69
|
219 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
220 * must not indicate a failure before the function call.
|
jpayne@69
|
221 * @stable ICU 4.0
|
jpayne@69
|
222 */
|
jpayne@69
|
223 PluralFormat(const UnicodeString& pattern, UErrorCode& status);
|
jpayne@69
|
224
|
jpayne@69
|
225 /**
|
jpayne@69
|
226 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string and
|
jpayne@69
|
227 * locale.
|
jpayne@69
|
228 * The locale will be used to get the set of plural rules and for
|
jpayne@69
|
229 * standard number formatting.
|
jpayne@69
|
230 * @param locale the <code>PluralFormat</code> will be configured with
|
jpayne@69
|
231 * rules for this locale. This locale will also be used for
|
jpayne@69
|
232 * standard number formatting.
|
jpayne@69
|
233 * @param pattern the pattern for this <code>PluralFormat</code>.
|
jpayne@69
|
234 * errors are returned to status if the pattern is invalid.
|
jpayne@69
|
235 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
236 * must not indicate a failure before the function call.
|
jpayne@69
|
237 * @stable ICU 4.0
|
jpayne@69
|
238 */
|
jpayne@69
|
239 PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status);
|
jpayne@69
|
240
|
jpayne@69
|
241 /**
|
jpayne@69
|
242 * Creates a new <code>PluralFormat</code> for a given set of rules, a
|
jpayne@69
|
243 * pattern and a locale.
|
jpayne@69
|
244 * @param rules defines the behavior of the <code>PluralFormat</code>
|
jpayne@69
|
245 * object.
|
jpayne@69
|
246 * @param pattern the pattern for this <code>PluralFormat</code>.
|
jpayne@69
|
247 * errors are returned to status if the pattern is invalid.
|
jpayne@69
|
248 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
249 * must not indicate a failure before the function call.
|
jpayne@69
|
250 * @stable ICU 4.0
|
jpayne@69
|
251 */
|
jpayne@69
|
252 PluralFormat(const PluralRules& rules,
|
jpayne@69
|
253 const UnicodeString& pattern,
|
jpayne@69
|
254 UErrorCode& status);
|
jpayne@69
|
255
|
jpayne@69
|
256 /**
|
jpayne@69
|
257 * Creates a new <code>PluralFormat</code> for a given set of rules, a
|
jpayne@69
|
258 * pattern and a locale.
|
jpayne@69
|
259 * @param locale the <code>PluralFormat</code> will be configured with
|
jpayne@69
|
260 * rules for this locale. This locale will also be used for
|
jpayne@69
|
261 * standard number formatting.
|
jpayne@69
|
262 * @param rules defines the behavior of the <code>PluralFormat</code>
|
jpayne@69
|
263 * object.
|
jpayne@69
|
264 * @param pattern the pattern for this <code>PluralFormat</code>.
|
jpayne@69
|
265 * errors are returned to status if the pattern is invalid.
|
jpayne@69
|
266 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
267 * must not indicate a failure before the function call.
|
jpayne@69
|
268 * @stable ICU 4.0
|
jpayne@69
|
269 */
|
jpayne@69
|
270 PluralFormat(const Locale& locale,
|
jpayne@69
|
271 const PluralRules& rules,
|
jpayne@69
|
272 const UnicodeString& pattern,
|
jpayne@69
|
273 UErrorCode& status);
|
jpayne@69
|
274
|
jpayne@69
|
275 /**
|
jpayne@69
|
276 * Creates a new <code>PluralFormat</code> for a plural type, a
|
jpayne@69
|
277 * pattern and a locale.
|
jpayne@69
|
278 * @param locale the <code>PluralFormat</code> will be configured with
|
jpayne@69
|
279 * rules for this locale. This locale will also be used for
|
jpayne@69
|
280 * standard number formatting.
|
jpayne@69
|
281 * @param type The plural type (e.g., cardinal or ordinal).
|
jpayne@69
|
282 * @param pattern the pattern for this <code>PluralFormat</code>.
|
jpayne@69
|
283 * errors are returned to status if the pattern is invalid.
|
jpayne@69
|
284 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
285 * must not indicate a failure before the function call.
|
jpayne@69
|
286 * @stable ICU 50
|
jpayne@69
|
287 */
|
jpayne@69
|
288 PluralFormat(const Locale& locale,
|
jpayne@69
|
289 UPluralType type,
|
jpayne@69
|
290 const UnicodeString& pattern,
|
jpayne@69
|
291 UErrorCode& status);
|
jpayne@69
|
292
|
jpayne@69
|
293 /**
|
jpayne@69
|
294 * copy constructor.
|
jpayne@69
|
295 * @stable ICU 4.0
|
jpayne@69
|
296 */
|
jpayne@69
|
297 PluralFormat(const PluralFormat& other);
|
jpayne@69
|
298
|
jpayne@69
|
299 /**
|
jpayne@69
|
300 * Destructor.
|
jpayne@69
|
301 * @stable ICU 4.0
|
jpayne@69
|
302 */
|
jpayne@69
|
303 virtual ~PluralFormat();
|
jpayne@69
|
304
|
jpayne@69
|
305 /**
|
jpayne@69
|
306 * Sets the pattern used by this plural format.
|
jpayne@69
|
307 * The method parses the pattern and creates a map of format strings
|
jpayne@69
|
308 * for the plural rules.
|
jpayne@69
|
309 * Patterns and their interpretation are specified in the class description.
|
jpayne@69
|
310 *
|
jpayne@69
|
311 * @param pattern the pattern for this plural format
|
jpayne@69
|
312 * errors are returned to status if the pattern is invalid.
|
jpayne@69
|
313 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
314 * must not indicate a failure before the function call.
|
jpayne@69
|
315 * @stable ICU 4.0
|
jpayne@69
|
316 */
|
jpayne@69
|
317 void applyPattern(const UnicodeString& pattern, UErrorCode& status);
|
jpayne@69
|
318
|
jpayne@69
|
319
|
jpayne@69
|
320 using Format::format;
|
jpayne@69
|
321
|
jpayne@69
|
322 /**
|
jpayne@69
|
323 * Formats a plural message for a given number.
|
jpayne@69
|
324 *
|
jpayne@69
|
325 * @param number a number for which the plural message should be formatted
|
jpayne@69
|
326 * for. If no pattern has been applied to this
|
jpayne@69
|
327 * <code>PluralFormat</code> object yet, the formatted number
|
jpayne@69
|
328 * will be returned.
|
jpayne@69
|
329 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
330 * must not indicate a failure before the function call.
|
jpayne@69
|
331 * @return the string containing the formatted plural message.
|
jpayne@69
|
332 * @stable ICU 4.0
|
jpayne@69
|
333 */
|
jpayne@69
|
334 UnicodeString format(int32_t number, UErrorCode& status) const;
|
jpayne@69
|
335
|
jpayne@69
|
336 /**
|
jpayne@69
|
337 * Formats a plural message for a given number.
|
jpayne@69
|
338 *
|
jpayne@69
|
339 * @param number a number for which the plural message should be formatted
|
jpayne@69
|
340 * for. If no pattern has been applied to this
|
jpayne@69
|
341 * PluralFormat object yet, the formatted number
|
jpayne@69
|
342 * will be returned.
|
jpayne@69
|
343 * @param status output param set to success or failure code on exit, which
|
jpayne@69
|
344 * must not indicate a failure before the function call.
|
jpayne@69
|
345 * @return the string containing the formatted plural message.
|
jpayne@69
|
346 * @stable ICU 4.0
|
jpayne@69
|
347 */
|
jpayne@69
|
348 UnicodeString format(double number, UErrorCode& status) const;
|
jpayne@69
|
349
|
jpayne@69
|
350 /**
|
jpayne@69
|
351 * Formats a plural message for a given number.
|
jpayne@69
|
352 *
|
jpayne@69
|
353 * @param number a number for which the plural message should be formatted
|
jpayne@69
|
354 * for. If no pattern has been applied to this
|
jpayne@69
|
355 * <code>PluralFormat</code> object yet, the formatted number
|
jpayne@69
|
356 * will be returned.
|
jpayne@69
|
357 * @param appendTo output parameter to receive result.
|
jpayne@69
|
358 * result is appended to existing contents.
|
jpayne@69
|
359 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
360 * On output: the offsets of the alignment field.
|
jpayne@69
|
361 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
362 * must not indicate a failure before the function call.
|
jpayne@69
|
363 * @return the string containing the formatted plural message.
|
jpayne@69
|
364 * @stable ICU 4.0
|
jpayne@69
|
365 */
|
jpayne@69
|
366 UnicodeString& format(int32_t number,
|
jpayne@69
|
367 UnicodeString& appendTo,
|
jpayne@69
|
368 FieldPosition& pos,
|
jpayne@69
|
369 UErrorCode& status) const;
|
jpayne@69
|
370
|
jpayne@69
|
371 /**
|
jpayne@69
|
372 * Formats a plural message for a given number.
|
jpayne@69
|
373 *
|
jpayne@69
|
374 * @param number a number for which the plural message should be formatted
|
jpayne@69
|
375 * for. If no pattern has been applied to this
|
jpayne@69
|
376 * PluralFormat object yet, the formatted number
|
jpayne@69
|
377 * will be returned.
|
jpayne@69
|
378 * @param appendTo output parameter to receive result.
|
jpayne@69
|
379 * result is appended to existing contents.
|
jpayne@69
|
380 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
381 * On output: the offsets of the alignment field.
|
jpayne@69
|
382 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
383 * must not indicate a failure before the function call.
|
jpayne@69
|
384 * @return the string containing the formatted plural message.
|
jpayne@69
|
385 * @stable ICU 4.0
|
jpayne@69
|
386 */
|
jpayne@69
|
387 UnicodeString& format(double number,
|
jpayne@69
|
388 UnicodeString& appendTo,
|
jpayne@69
|
389 FieldPosition& pos,
|
jpayne@69
|
390 UErrorCode& status) const;
|
jpayne@69
|
391
|
jpayne@69
|
392 #ifndef U_HIDE_DEPRECATED_API
|
jpayne@69
|
393 /**
|
jpayne@69
|
394 * Sets the locale used by this <code>PluraFormat</code> object.
|
jpayne@69
|
395 * Note: Calling this method resets this <code>PluraFormat</code> object,
|
jpayne@69
|
396 * i.e., a pattern that was applied previously will be removed,
|
jpayne@69
|
397 * and the NumberFormat is set to the default number format for
|
jpayne@69
|
398 * the locale. The resulting format behaves the same as one
|
jpayne@69
|
399 * constructed from {@link #PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status)}
|
jpayne@69
|
400 * with UPLURAL_TYPE_CARDINAL.
|
jpayne@69
|
401 * @param locale the <code>locale</code> to use to configure the formatter.
|
jpayne@69
|
402 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
403 * must not indicate a failure before the function call.
|
jpayne@69
|
404 * @deprecated ICU 50 This method clears the pattern and might create
|
jpayne@69
|
405 * a different kind of PluralRules instance;
|
jpayne@69
|
406 * use one of the constructors to create a new instance instead.
|
jpayne@69
|
407 */
|
jpayne@69
|
408 void setLocale(const Locale& locale, UErrorCode& status);
|
jpayne@69
|
409 #endif /* U_HIDE_DEPRECATED_API */
|
jpayne@69
|
410
|
jpayne@69
|
411 /**
|
jpayne@69
|
412 * Sets the number format used by this formatter. You only need to
|
jpayne@69
|
413 * call this if you want a different number format than the default
|
jpayne@69
|
414 * formatter for the locale.
|
jpayne@69
|
415 * @param format the number format to use.
|
jpayne@69
|
416 * @param status output param set to success/failure code on exit, which
|
jpayne@69
|
417 * must not indicate a failure before the function call.
|
jpayne@69
|
418 * @stable ICU 4.0
|
jpayne@69
|
419 */
|
jpayne@69
|
420 void setNumberFormat(const NumberFormat* format, UErrorCode& status);
|
jpayne@69
|
421
|
jpayne@69
|
422 /**
|
jpayne@69
|
423 * Assignment operator
|
jpayne@69
|
424 *
|
jpayne@69
|
425 * @param other the PluralFormat object to copy from.
|
jpayne@69
|
426 * @stable ICU 4.0
|
jpayne@69
|
427 */
|
jpayne@69
|
428 PluralFormat& operator=(const PluralFormat& other);
|
jpayne@69
|
429
|
jpayne@69
|
430 /**
|
jpayne@69
|
431 * Return true if another object is semantically equal to this one.
|
jpayne@69
|
432 *
|
jpayne@69
|
433 * @param other the PluralFormat object to be compared with.
|
jpayne@69
|
434 * @return true if other is semantically equal to this.
|
jpayne@69
|
435 * @stable ICU 4.0
|
jpayne@69
|
436 */
|
jpayne@69
|
437 virtual UBool operator==(const Format& other) const;
|
jpayne@69
|
438
|
jpayne@69
|
439 /**
|
jpayne@69
|
440 * Return true if another object is semantically unequal to this one.
|
jpayne@69
|
441 *
|
jpayne@69
|
442 * @param other the PluralFormat object to be compared with.
|
jpayne@69
|
443 * @return true if other is semantically unequal to this.
|
jpayne@69
|
444 * @stable ICU 4.0
|
jpayne@69
|
445 */
|
jpayne@69
|
446 virtual UBool operator!=(const Format& other) const;
|
jpayne@69
|
447
|
jpayne@69
|
448 /**
|
jpayne@69
|
449 * Clones this Format object polymorphically. The caller owns the
|
jpayne@69
|
450 * result and should delete it when done.
|
jpayne@69
|
451 * @stable ICU 4.0
|
jpayne@69
|
452 */
|
jpayne@69
|
453 virtual PluralFormat* clone() const;
|
jpayne@69
|
454
|
jpayne@69
|
455 /**
|
jpayne@69
|
456 * Formats a plural message for a number taken from a Formattable object.
|
jpayne@69
|
457 *
|
jpayne@69
|
458 * @param obj The object containing a number for which the
|
jpayne@69
|
459 * plural message should be formatted.
|
jpayne@69
|
460 * The object must be of a numeric type.
|
jpayne@69
|
461 * @param appendTo output parameter to receive result.
|
jpayne@69
|
462 * Result is appended to existing contents.
|
jpayne@69
|
463 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
464 * On output: the offsets of the alignment field.
|
jpayne@69
|
465 * @param status output param filled with success/failure status.
|
jpayne@69
|
466 * @return Reference to 'appendTo' parameter.
|
jpayne@69
|
467 * @stable ICU 4.0
|
jpayne@69
|
468 */
|
jpayne@69
|
469 UnicodeString& format(const Formattable& obj,
|
jpayne@69
|
470 UnicodeString& appendTo,
|
jpayne@69
|
471 FieldPosition& pos,
|
jpayne@69
|
472 UErrorCode& status) const;
|
jpayne@69
|
473
|
jpayne@69
|
474 /**
|
jpayne@69
|
475 * Returns the pattern from applyPattern() or constructor().
|
jpayne@69
|
476 *
|
jpayne@69
|
477 * @param appendTo output parameter to receive result.
|
jpayne@69
|
478 * Result is appended to existing contents.
|
jpayne@69
|
479 * @return the UnicodeString with inserted pattern.
|
jpayne@69
|
480 * @stable ICU 4.0
|
jpayne@69
|
481 */
|
jpayne@69
|
482 UnicodeString& toPattern(UnicodeString& appendTo);
|
jpayne@69
|
483
|
jpayne@69
|
484 /**
|
jpayne@69
|
485 * This method is not yet supported by <code>PluralFormat</code>.
|
jpayne@69
|
486 * <P>
|
jpayne@69
|
487 * Before calling, set parse_pos.index to the offset you want to start
|
jpayne@69
|
488 * parsing at in the source. After calling, parse_pos.index is the end of
|
jpayne@69
|
489 * the text you parsed. If error occurs, index is unchanged.
|
jpayne@69
|
490 * <P>
|
jpayne@69
|
491 * When parsing, leading whitespace is discarded (with a successful parse),
|
jpayne@69
|
492 * while trailing whitespace is left as is.
|
jpayne@69
|
493 * <P>
|
jpayne@69
|
494 * See Format::parseObject() for more.
|
jpayne@69
|
495 *
|
jpayne@69
|
496 * @param source The string to be parsed into an object.
|
jpayne@69
|
497 * @param result Formattable to be set to the parse result.
|
jpayne@69
|
498 * If parse fails, return contents are undefined.
|
jpayne@69
|
499 * @param parse_pos The position to start parsing at. Upon return
|
jpayne@69
|
500 * this param is set to the position after the
|
jpayne@69
|
501 * last character successfully parsed. If the
|
jpayne@69
|
502 * source is not parsed successfully, this param
|
jpayne@69
|
503 * will remain unchanged.
|
jpayne@69
|
504 * @stable ICU 4.0
|
jpayne@69
|
505 */
|
jpayne@69
|
506 virtual void parseObject(const UnicodeString& source,
|
jpayne@69
|
507 Formattable& result,
|
jpayne@69
|
508 ParsePosition& parse_pos) const;
|
jpayne@69
|
509
|
jpayne@69
|
510 /**
|
jpayne@69
|
511 * ICU "poor man's RTTI", returns a UClassID for this class.
|
jpayne@69
|
512 *
|
jpayne@69
|
513 * @stable ICU 4.0
|
jpayne@69
|
514 *
|
jpayne@69
|
515 */
|
jpayne@69
|
516 static UClassID U_EXPORT2 getStaticClassID(void);
|
jpayne@69
|
517
|
jpayne@69
|
518 /**
|
jpayne@69
|
519 * ICU "poor man's RTTI", returns a UClassID for the actual class.
|
jpayne@69
|
520 *
|
jpayne@69
|
521 * @stable ICU 4.0
|
jpayne@69
|
522 */
|
jpayne@69
|
523 virtual UClassID getDynamicClassID() const;
|
jpayne@69
|
524
|
jpayne@69
|
525 private:
|
jpayne@69
|
526 /**
|
jpayne@69
|
527 * @internal (private)
|
jpayne@69
|
528 */
|
jpayne@69
|
529 class U_I18N_API PluralSelector : public UMemory {
|
jpayne@69
|
530 public:
|
jpayne@69
|
531 virtual ~PluralSelector();
|
jpayne@69
|
532 /**
|
jpayne@69
|
533 * Given a number, returns the appropriate PluralFormat keyword.
|
jpayne@69
|
534 *
|
jpayne@69
|
535 * @param context worker object for the selector.
|
jpayne@69
|
536 * @param number The number to be plural-formatted.
|
jpayne@69
|
537 * @param ec Error code.
|
jpayne@69
|
538 * @return The selected PluralFormat keyword.
|
jpayne@69
|
539 * @internal (private)
|
jpayne@69
|
540 */
|
jpayne@69
|
541 virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0;
|
jpayne@69
|
542 };
|
jpayne@69
|
543
|
jpayne@69
|
544 class U_I18N_API PluralSelectorAdapter : public PluralSelector {
|
jpayne@69
|
545 public:
|
jpayne@69
|
546 PluralSelectorAdapter() : pluralRules(NULL) {
|
jpayne@69
|
547 }
|
jpayne@69
|
548
|
jpayne@69
|
549 virtual ~PluralSelectorAdapter();
|
jpayne@69
|
550
|
jpayne@69
|
551 virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const;
|
jpayne@69
|
552
|
jpayne@69
|
553 void reset();
|
jpayne@69
|
554
|
jpayne@69
|
555 PluralRules* pluralRules;
|
jpayne@69
|
556 };
|
jpayne@69
|
557
|
jpayne@69
|
558 Locale locale;
|
jpayne@69
|
559 MessagePattern msgPattern;
|
jpayne@69
|
560 NumberFormat* numberFormat;
|
jpayne@69
|
561 double offset;
|
jpayne@69
|
562 PluralSelectorAdapter pluralRulesWrapper;
|
jpayne@69
|
563
|
jpayne@69
|
564 PluralFormat(); // default constructor not implemented
|
jpayne@69
|
565 void init(const PluralRules* rules, UPluralType type, UErrorCode& status);
|
jpayne@69
|
566 /**
|
jpayne@69
|
567 * Copies dynamically allocated values (pointer fields).
|
jpayne@69
|
568 * Others are copied using their copy constructors and assignment operators.
|
jpayne@69
|
569 */
|
jpayne@69
|
570 void copyObjects(const PluralFormat& other);
|
jpayne@69
|
571
|
jpayne@69
|
572 UnicodeString& format(const Formattable& numberObject, double number,
|
jpayne@69
|
573 UnicodeString& appendTo,
|
jpayne@69
|
574 FieldPosition& pos,
|
jpayne@69
|
575 UErrorCode& status) const;
|
jpayne@69
|
576
|
jpayne@69
|
577 /**
|
jpayne@69
|
578 * Finds the PluralFormat sub-message for the given number, or the "other" sub-message.
|
jpayne@69
|
579 * @param pattern A MessagePattern.
|
jpayne@69
|
580 * @param partIndex the index of the first PluralFormat argument style part.
|
jpayne@69
|
581 * @param selector the PluralSelector for mapping the number (minus offset) to a keyword.
|
jpayne@69
|
582 * @param context worker object for the selector.
|
jpayne@69
|
583 * @param number a number to be matched to one of the PluralFormat argument's explicit values,
|
jpayne@69
|
584 * or mapped via the PluralSelector.
|
jpayne@69
|
585 * @param ec ICU error code.
|
jpayne@69
|
586 * @return the sub-message start part index.
|
jpayne@69
|
587 */
|
jpayne@69
|
588 static int32_t findSubMessage(
|
jpayne@69
|
589 const MessagePattern& pattern, int32_t partIndex,
|
jpayne@69
|
590 const PluralSelector& selector, void *context, double number, UErrorCode& ec); /**< @internal */
|
jpayne@69
|
591
|
jpayne@69
|
592 void parseType(const UnicodeString& source, const NFRule *rbnfLenientScanner,
|
jpayne@69
|
593 Formattable& result, FieldPosition& pos) const;
|
jpayne@69
|
594
|
jpayne@69
|
595 friend class MessageFormat;
|
jpayne@69
|
596 friend class NFRule;
|
jpayne@69
|
597 };
|
jpayne@69
|
598
|
jpayne@69
|
599 U_NAMESPACE_END
|
jpayne@69
|
600
|
jpayne@69
|
601 #endif /* #if !UCONFIG_NO_FORMATTING */
|
jpayne@69
|
602
|
jpayne@69
|
603 #endif /* U_SHOW_CPLUSPLUS_API */
|
jpayne@69
|
604
|
jpayne@69
|
605 #endif // _PLURFMT
|
jpayne@69
|
606 //eof
|