1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *****************************************************************************************
5 * Copyright (C) 2010-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *****************************************************************************************
10 #ifndef UPLURALRULES_H
11 #define UPLURALRULES_H
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_FORMATTING
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 */
23 // Forward-declaration
24 struct UFormattedNumber
;
28 * \brief C API: Plural rules, select plural keywords for numeric values.
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.
37 * For more information, see the LDML spec, C.11 Language Plural Rules:
38 * http://www.unicode.org/reports/tr35/#Language_Plural_Rules
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.
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
50 * Type of plurals and PluralRules.
55 * Plural rules for cardinal numbers: 1 file vs. 2 files.
58 UPLURAL_TYPE_CARDINAL
,
60 * Plural rules for ordinal numbers: 1st file, 2nd file, 3rd file, 4th file, etc.
64 #ifndef U_HIDE_DEPRECATED_API
66 * One more than the highest normal UPluralType value.
67 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
70 #endif /* U_HIDE_DEPRECATED_API */
75 typedef enum UPluralType UPluralType
;
78 * Opaque UPluralRules object for use in C programs.
82 typedef struct UPluralRules UPluralRules
; /**< C typedef for struct UPluralRules. @stable ICU 4.8 */
85 * Opens a new UPluralRules object using the predefined cardinal-number plural rules for a
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.
93 U_CAPI UPluralRules
* U_EXPORT2
94 uplrules_open(const char *locale
, UErrorCode
*status
);
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.
105 U_CAPI UPluralRules
* U_EXPORT2
106 uplrules_openForType(const char *locale
, UPluralType type
, UErrorCode
*status
);
109 * Closes a UPluralRules object. Once closed it may no longer be used.
110 * @param uplrules The UPluralRules object to close.
113 U_CAPI
void U_EXPORT2
114 uplrules_close(UPluralRules
*uplrules
);
117 #if U_SHOW_CPLUSPLUS_API
122 * \class LocalUPluralRulesPointer
123 * "Smart pointer" class, closes a UPluralRules via uplrules_close().
124 * For most methods see the LocalPointerBase base class.
126 * @see LocalPointerBase
130 U_DEFINE_LOCAL_OPEN_POINTER(LocalUPluralRulesPointer
, UPluralRules
, uplrules_close
);
134 #endif // U_SHOW_CPLUSPLUS_API
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
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.
149 U_CAPI
int32_t U_EXPORT2
150 uplrules_select(const UPluralRules
*uplrules
,
152 UChar
*keyword
, int32_t capacity
,
155 #ifndef U_HIDE_DRAFT_API
157 * Given a formatted number, returns the keyword of the first rule
158 * that applies to the number, according to the supplied UPluralRules object.
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}.
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
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.
173 U_CAPI
int32_t U_EXPORT2
174 uplrules_selectFormatted(const UPluralRules
*uplrules
,
175 const struct UFormattedNumber
* number
,
176 UChar
*keyword
, int32_t capacity
,
178 #endif /* U_HIDE_DRAFT_API */
180 #ifndef U_HIDE_INTERNAL_API
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
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
199 U_INTERNAL
int32_t U_EXPORT2
200 uplrules_selectWithFormat(const UPluralRules
*uplrules
,
202 const UNumberFormat
*fmt
,
203 UChar
*keyword
, int32_t capacity
,
206 #endif /* U_HIDE_INTERNAL_API */
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
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.
218 U_STABLE UEnumeration
* U_EXPORT2
219 uplrules_getKeywords(const UPluralRules
*uplrules
,
222 #endif /* #if !UCONFIG_NO_FORMATTING */