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