xref: /aosp_15_r20/external/icu/libandroidicu/include/unicode/upluralrules.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker *****************************************************************************************
5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2010-2013, International Business Machines
6*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker *****************************************************************************************
8*0e209d39SAndroid Build Coastguard Worker */
9*0e209d39SAndroid Build Coastguard Worker 
10*0e209d39SAndroid Build Coastguard Worker #ifndef UPLURALRULES_H
11*0e209d39SAndroid Build Coastguard Worker #define UPLURALRULES_H
12*0e209d39SAndroid Build Coastguard Worker 
13*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
14*0e209d39SAndroid Build Coastguard Worker 
15*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING
16*0e209d39SAndroid Build Coastguard Worker 
17*0e209d39SAndroid Build Coastguard Worker #include "unicode/uenum.h"
18*0e209d39SAndroid Build Coastguard Worker 
19*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API
20*0e209d39SAndroid Build Coastguard Worker #include "unicode/localpointer.h"
21*0e209d39SAndroid Build Coastguard Worker #endif   // U_SHOW_CPLUSPLUS_API
22*0e209d39SAndroid Build Coastguard Worker 
23*0e209d39SAndroid Build Coastguard Worker #ifndef U_HIDE_INTERNAL_API
24*0e209d39SAndroid Build Coastguard Worker #include "unicode/unum.h"
25*0e209d39SAndroid Build Coastguard Worker #endif  /* U_HIDE_INTERNAL_API */
26*0e209d39SAndroid Build Coastguard Worker 
27*0e209d39SAndroid Build Coastguard Worker // Forward-declaration
28*0e209d39SAndroid Build Coastguard Worker struct UFormattedNumber;
29*0e209d39SAndroid Build Coastguard Worker struct UFormattedNumberRange;
30*0e209d39SAndroid Build Coastguard Worker 
31*0e209d39SAndroid Build Coastguard Worker /**
32*0e209d39SAndroid Build Coastguard Worker  * \file
33*0e209d39SAndroid Build Coastguard Worker  * \brief C API: Plural rules, select plural keywords for numeric values.
34*0e209d39SAndroid Build Coastguard Worker  *
35*0e209d39SAndroid Build Coastguard Worker  * A UPluralRules object defines rules for mapping non-negative numeric
36*0e209d39SAndroid Build Coastguard Worker  * values onto a small set of keywords. Rules are constructed from a text
37*0e209d39SAndroid Build Coastguard Worker  * description, consisting of a series of keywords and conditions.
38*0e209d39SAndroid Build Coastguard Worker  * The uplrules_select function examines each condition in order and
39*0e209d39SAndroid Build Coastguard Worker  * returns the keyword for the first condition that matches the number.
40*0e209d39SAndroid Build Coastguard Worker  * If none match, the default rule(other) is returned.
41*0e209d39SAndroid Build Coastguard Worker  *
42*0e209d39SAndroid Build Coastguard Worker  * For more information, see the
43*0e209d39SAndroid Build Coastguard Worker  * LDML spec, Part 3.5 Language Plural Rules:
44*0e209d39SAndroid Build Coastguard Worker  * https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
45*0e209d39SAndroid Build Coastguard Worker  *
46*0e209d39SAndroid Build Coastguard Worker  * Keywords: ICU locale data has 6 predefined values -
47*0e209d39SAndroid Build Coastguard Worker  * 'zero', 'one', 'two', 'few', 'many' and 'other'. Callers need to check
48*0e209d39SAndroid Build Coastguard Worker  * the value of keyword returned by the uplrules_select function.
49*0e209d39SAndroid Build Coastguard Worker  *
50*0e209d39SAndroid Build Coastguard Worker  * These are based on CLDR <i>Language Plural Rules</i>. For these
51*0e209d39SAndroid Build Coastguard Worker  * predefined rules, see the CLDR page at
52*0e209d39SAndroid Build Coastguard Worker  * https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html
53*0e209d39SAndroid Build Coastguard Worker  */
54*0e209d39SAndroid Build Coastguard Worker 
55*0e209d39SAndroid Build Coastguard Worker /**
56*0e209d39SAndroid Build Coastguard Worker  * Type of plurals and PluralRules.
57*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 50
58*0e209d39SAndroid Build Coastguard Worker  */
59*0e209d39SAndroid Build Coastguard Worker enum UPluralType {
60*0e209d39SAndroid Build Coastguard Worker     /**
61*0e209d39SAndroid Build Coastguard Worker      * Plural rules for cardinal numbers: 1 file vs. 2 files.
62*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 50
63*0e209d39SAndroid Build Coastguard Worker      */
64*0e209d39SAndroid Build Coastguard Worker     UPLURAL_TYPE_CARDINAL,
65*0e209d39SAndroid Build Coastguard Worker     /**
66*0e209d39SAndroid Build Coastguard Worker      * Plural rules for ordinal numbers: 1st file, 2nd file, 3rd file, 4th file, etc.
67*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 50
68*0e209d39SAndroid Build Coastguard Worker      */
69*0e209d39SAndroid Build Coastguard Worker     UPLURAL_TYPE_ORDINAL,
70*0e209d39SAndroid Build Coastguard Worker #ifndef U_HIDE_DEPRECATED_API
71*0e209d39SAndroid Build Coastguard Worker     /**
72*0e209d39SAndroid Build Coastguard Worker      * One more than the highest normal UPluralType value.
73*0e209d39SAndroid Build Coastguard Worker      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
74*0e209d39SAndroid Build Coastguard Worker      */
75*0e209d39SAndroid Build Coastguard Worker     UPLURAL_TYPE_COUNT
76*0e209d39SAndroid Build Coastguard Worker #endif  /* U_HIDE_DEPRECATED_API */
77*0e209d39SAndroid Build Coastguard Worker };
78*0e209d39SAndroid Build Coastguard Worker /**
79*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 50
80*0e209d39SAndroid Build Coastguard Worker  */
81*0e209d39SAndroid Build Coastguard Worker typedef enum UPluralType UPluralType;
82*0e209d39SAndroid Build Coastguard Worker 
83*0e209d39SAndroid Build Coastguard Worker /**
84*0e209d39SAndroid Build Coastguard Worker  * Opaque UPluralRules object for use in C programs.
85*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.8
86*0e209d39SAndroid Build Coastguard Worker  */
87*0e209d39SAndroid Build Coastguard Worker struct UPluralRules;
88*0e209d39SAndroid Build Coastguard Worker typedef struct UPluralRules UPluralRules;  /**< C typedef for struct UPluralRules. @stable ICU 4.8 */
89*0e209d39SAndroid Build Coastguard Worker 
90*0e209d39SAndroid Build Coastguard Worker /**
91*0e209d39SAndroid Build Coastguard Worker  * Opens a new UPluralRules object using the predefined cardinal-number plural rules for a
92*0e209d39SAndroid Build Coastguard Worker  * given locale.
93*0e209d39SAndroid Build Coastguard Worker  * Same as uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status).
94*0e209d39SAndroid Build Coastguard Worker  * @param locale The locale for which the rules are desired.
95*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
96*0e209d39SAndroid Build Coastguard Worker  * @return A UPluralRules for the specified locale, or NULL if an error occurred.
97*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.8
98*0e209d39SAndroid Build Coastguard Worker  */
99*0e209d39SAndroid Build Coastguard Worker U_CAPI UPluralRules* U_EXPORT2
100*0e209d39SAndroid Build Coastguard Worker uplrules_open(const char *locale, UErrorCode *status);
101*0e209d39SAndroid Build Coastguard Worker 
102*0e209d39SAndroid Build Coastguard Worker /**
103*0e209d39SAndroid Build Coastguard Worker  * Opens a new UPluralRules object using the predefined plural rules for a
104*0e209d39SAndroid Build Coastguard Worker  * given locale and the plural type.
105*0e209d39SAndroid Build Coastguard Worker  * @param locale The locale for which the rules are desired.
106*0e209d39SAndroid Build Coastguard Worker  * @param type The plural type (e.g., cardinal or ordinal).
107*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
108*0e209d39SAndroid Build Coastguard Worker  * @return A UPluralRules for the specified locale, or NULL if an error occurred.
109*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 50
110*0e209d39SAndroid Build Coastguard Worker  */
111*0e209d39SAndroid Build Coastguard Worker U_CAPI UPluralRules* U_EXPORT2
112*0e209d39SAndroid Build Coastguard Worker uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status);
113*0e209d39SAndroid Build Coastguard Worker 
114*0e209d39SAndroid Build Coastguard Worker /**
115*0e209d39SAndroid Build Coastguard Worker  * Closes a UPluralRules object. Once closed it may no longer be used.
116*0e209d39SAndroid Build Coastguard Worker  * @param uplrules The UPluralRules object to close.
117*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.8
118*0e209d39SAndroid Build Coastguard Worker  */
119*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
120*0e209d39SAndroid Build Coastguard Worker uplrules_close(UPluralRules *uplrules);
121*0e209d39SAndroid Build Coastguard Worker 
122*0e209d39SAndroid Build Coastguard Worker 
123*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API
124*0e209d39SAndroid Build Coastguard Worker 
125*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
126*0e209d39SAndroid Build Coastguard Worker 
127*0e209d39SAndroid Build Coastguard Worker /**
128*0e209d39SAndroid Build Coastguard Worker  * \class LocalUPluralRulesPointer
129*0e209d39SAndroid Build Coastguard Worker  * "Smart pointer" class, closes a UPluralRules via uplrules_close().
130*0e209d39SAndroid Build Coastguard Worker  * For most methods see the LocalPointerBase base class.
131*0e209d39SAndroid Build Coastguard Worker  *
132*0e209d39SAndroid Build Coastguard Worker  * @see LocalPointerBase
133*0e209d39SAndroid Build Coastguard Worker  * @see LocalPointer
134*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.8
135*0e209d39SAndroid Build Coastguard Worker  */
136*0e209d39SAndroid Build Coastguard Worker U_DEFINE_LOCAL_OPEN_POINTER(LocalUPluralRulesPointer, UPluralRules, uplrules_close);
137*0e209d39SAndroid Build Coastguard Worker 
138*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
139*0e209d39SAndroid Build Coastguard Worker 
140*0e209d39SAndroid Build Coastguard Worker #endif
141*0e209d39SAndroid Build Coastguard Worker 
142*0e209d39SAndroid Build Coastguard Worker 
143*0e209d39SAndroid Build Coastguard Worker /**
144*0e209d39SAndroid Build Coastguard Worker  * Given a floating-point number, returns the keyword of the first rule that
145*0e209d39SAndroid Build Coastguard Worker  * applies to the number, according to the supplied UPluralRules object.
146*0e209d39SAndroid Build Coastguard Worker  * @param uplrules The UPluralRules object specifying the rules.
147*0e209d39SAndroid Build Coastguard Worker  * @param number The number for which the rule has to be determined.
148*0e209d39SAndroid Build Coastguard Worker  * @param keyword An output buffer to write the keyword of the rule that
149*0e209d39SAndroid Build Coastguard Worker  *         applies to number.
150*0e209d39SAndroid Build Coastguard Worker  * @param capacity The capacity of the keyword buffer.
151*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
152*0e209d39SAndroid Build Coastguard Worker  * @return The length of the keyword.
153*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.8
154*0e209d39SAndroid Build Coastguard Worker  */
155*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
156*0e209d39SAndroid Build Coastguard Worker uplrules_select(const UPluralRules *uplrules,
157*0e209d39SAndroid Build Coastguard Worker                double number,
158*0e209d39SAndroid Build Coastguard Worker                UChar *keyword, int32_t capacity,
159*0e209d39SAndroid Build Coastguard Worker                UErrorCode *status);
160*0e209d39SAndroid Build Coastguard Worker 
161*0e209d39SAndroid Build Coastguard Worker /**
162*0e209d39SAndroid Build Coastguard Worker  * Given a formatted number, returns the keyword of the first rule
163*0e209d39SAndroid Build Coastguard Worker  * that applies to the number, according to the supplied UPluralRules object.
164*0e209d39SAndroid Build Coastguard Worker  *
165*0e209d39SAndroid Build Coastguard Worker  * A UFormattedNumber allows you to specify an exponent or trailing zeros,
166*0e209d39SAndroid Build Coastguard Worker  * which can affect the plural category. To get a UFormattedNumber, see
167*0e209d39SAndroid Build Coastguard Worker  * {@link UNumberFormatter}.
168*0e209d39SAndroid Build Coastguard Worker  *
169*0e209d39SAndroid Build Coastguard Worker  * @param uplrules The UPluralRules object specifying the rules.
170*0e209d39SAndroid Build Coastguard Worker  * @param number The formatted number for which the rule has to be determined.
171*0e209d39SAndroid Build Coastguard Worker  * @param keyword The destination buffer for the keyword of the rule that
172*0e209d39SAndroid Build Coastguard Worker  *         applies to the number.
173*0e209d39SAndroid Build Coastguard Worker  * @param capacity The capacity of the keyword buffer.
174*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
175*0e209d39SAndroid Build Coastguard Worker  * @return The length of the keyword.
176*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 64
177*0e209d39SAndroid Build Coastguard Worker  */
178*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
179*0e209d39SAndroid Build Coastguard Worker uplrules_selectFormatted(const UPluralRules *uplrules,
180*0e209d39SAndroid Build Coastguard Worker                const struct UFormattedNumber* number,
181*0e209d39SAndroid Build Coastguard Worker                UChar *keyword, int32_t capacity,
182*0e209d39SAndroid Build Coastguard Worker                UErrorCode *status);
183*0e209d39SAndroid Build Coastguard Worker 
184*0e209d39SAndroid Build Coastguard Worker /**
185*0e209d39SAndroid Build Coastguard Worker  * Given a formatted number range, returns the overall plural form of the
186*0e209d39SAndroid Build Coastguard Worker  * range. For example, "3-5" returns "other" in English.
187*0e209d39SAndroid Build Coastguard Worker  *
188*0e209d39SAndroid Build Coastguard Worker  * To get a UFormattedNumberRange, see UNumberRangeFormatter.
189*0e209d39SAndroid Build Coastguard Worker  *
190*0e209d39SAndroid Build Coastguard Worker  * @param uplrules The UPluralRules object specifying the rules.
191*0e209d39SAndroid Build Coastguard Worker  * @param urange The number range onto which the rules will be applied.
192*0e209d39SAndroid Build Coastguard Worker  * @param keyword The destination buffer for the keyword of the rule that
193*0e209d39SAndroid Build Coastguard Worker  *         applies to the number range.
194*0e209d39SAndroid Build Coastguard Worker  * @param capacity The capacity of the keyword buffer.
195*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
196*0e209d39SAndroid Build Coastguard Worker  * @return The length of the keyword.
197*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 68
198*0e209d39SAndroid Build Coastguard Worker  */
199*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
200*0e209d39SAndroid Build Coastguard Worker uplrules_selectForRange(const UPluralRules *uplrules,
201*0e209d39SAndroid Build Coastguard Worker                const struct UFormattedNumberRange* urange,
202*0e209d39SAndroid Build Coastguard Worker                UChar *keyword, int32_t capacity,
203*0e209d39SAndroid Build Coastguard Worker                UErrorCode *status);
204*0e209d39SAndroid Build Coastguard Worker 
205*0e209d39SAndroid Build Coastguard Worker #ifndef U_HIDE_INTERNAL_API
206*0e209d39SAndroid Build Coastguard Worker /**
207*0e209d39SAndroid Build Coastguard Worker  * Given a number, returns the keyword of the first rule that applies to the
208*0e209d39SAndroid Build Coastguard Worker  * number, according to the UPluralRules object and given the number format
209*0e209d39SAndroid Build Coastguard Worker  * specified by the UNumberFormat object.
210*0e209d39SAndroid Build Coastguard Worker  * Note: This internal preview interface may be removed in the future if
211*0e209d39SAndroid Build Coastguard Worker  * an architecturally cleaner solution reaches stable status.
212*0e209d39SAndroid Build Coastguard Worker  * @param uplrules The UPluralRules object specifying the rules.
213*0e209d39SAndroid Build Coastguard Worker  * @param number The number for which the rule has to be determined.
214*0e209d39SAndroid Build Coastguard Worker  * @param fmt The UNumberFormat specifying how the number will be formatted
215*0e209d39SAndroid Build Coastguard Worker  *        (this can affect the plural form, e.g. "1 dollar" vs "1.0 dollars").
216*0e209d39SAndroid Build Coastguard Worker  *        If this is NULL, the function behaves like uplrules_select.
217*0e209d39SAndroid Build Coastguard Worker  * @param keyword An output buffer to write the keyword of the rule that
218*0e209d39SAndroid Build Coastguard Worker  *         applies to number.
219*0e209d39SAndroid Build Coastguard Worker  * @param capacity The capacity of the keyword buffer.
220*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
221*0e209d39SAndroid Build Coastguard Worker  * @return The length of keyword.
222*0e209d39SAndroid Build Coastguard Worker  * @internal ICU 59 technology preview, may be removed in the future
223*0e209d39SAndroid Build Coastguard Worker  */
224*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
225*0e209d39SAndroid Build Coastguard Worker uplrules_selectWithFormat(const UPluralRules *uplrules,
226*0e209d39SAndroid Build Coastguard Worker                           double number,
227*0e209d39SAndroid Build Coastguard Worker                           const UNumberFormat *fmt,
228*0e209d39SAndroid Build Coastguard Worker                           UChar *keyword, int32_t capacity,
229*0e209d39SAndroid Build Coastguard Worker                           UErrorCode *status);
230*0e209d39SAndroid Build Coastguard Worker 
231*0e209d39SAndroid Build Coastguard Worker #endif  /* U_HIDE_INTERNAL_API */
232*0e209d39SAndroid Build Coastguard Worker 
233*0e209d39SAndroid Build Coastguard Worker /**
234*0e209d39SAndroid Build Coastguard Worker  * Creates a string enumeration of all plural rule keywords used in this
235*0e209d39SAndroid Build Coastguard Worker  * UPluralRules object. The rule "other" is always present by default.
236*0e209d39SAndroid Build Coastguard Worker  * @param uplrules The UPluralRules object specifying the rules for
237*0e209d39SAndroid Build Coastguard Worker  *        a given locale.
238*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
239*0e209d39SAndroid Build Coastguard Worker  * @return a string enumeration over plural rule keywords, or NULL
240*0e209d39SAndroid Build Coastguard Worker  * upon error. The caller is responsible for closing the result.
241*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 59
242*0e209d39SAndroid Build Coastguard Worker  */
243*0e209d39SAndroid Build Coastguard Worker U_CAPI UEnumeration* U_EXPORT2
244*0e209d39SAndroid Build Coastguard Worker uplrules_getKeywords(const UPluralRules *uplrules,
245*0e209d39SAndroid Build Coastguard Worker                      UErrorCode *status);
246*0e209d39SAndroid Build Coastguard Worker 
247*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_FORMATTING */
248*0e209d39SAndroid Build Coastguard Worker 
249*0e209d39SAndroid Build Coastguard Worker #endif
250