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 */
25 * \brief C API: Plural rules, select plural keywords for numeric values.
27 * A UPluralRules object defines rules for mapping non-negative numeric
28 * values onto a small set of keywords. Rules are constructed from a text
29 * description, consisting of a series of keywords and conditions.
30 * The uplrules_select function examines each condition in order and
31 * returns the keyword for the first condition that matches the number.
32 * If none match, the default rule(other) is returned.
34 * For more information, see the LDML spec, C.11 Language Plural Rules:
35 * http://www.unicode.org/reports/tr35/#Language_Plural_Rules
37 * Keywords: ICU locale data has 6 predefined values -
38 * 'zero', 'one', 'two', 'few', 'many' and 'other'. Callers need to check
39 * the value of keyword returned by the uplrules_select function.
41 * These are based on CLDR <i>Language Plural Rules</i>. For these
42 * predefined rules, see the CLDR page at
43 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
47 * Type of plurals and PluralRules.
52 * Plural rules for cardinal numbers: 1 file vs. 2 files.
55 UPLURAL_TYPE_CARDINAL
,
57 * Plural rules for ordinal numbers: 1st file, 2nd file, 3rd file, 4th file, etc.
61 #ifndef U_HIDE_DEPRECATED_API
63 * One more than the highest normal UPluralType value.
64 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
67 #endif /* U_HIDE_DEPRECATED_API */
72 typedef enum UPluralType UPluralType
;
75 * Opaque UPluralRules object for use in C programs.
79 typedef struct UPluralRules UPluralRules
; /**< C typedef for struct UPluralRules. @stable ICU 4.8 */
82 * Opens a new UPluralRules object using the predefined cardinal-number plural rules for a
84 * Same as uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status).
85 * @param locale The locale for which the rules are desired.
86 * @param status A pointer to a UErrorCode to receive any errors.
87 * @return A UPluralRules for the specified locale, or NULL if an error occurred.
90 U_CAPI UPluralRules
* U_EXPORT2
91 uplrules_open(const char *locale
, UErrorCode
*status
);
94 * Opens a new UPluralRules object using the predefined plural rules for a
95 * given locale and the plural type.
96 * @param locale The locale for which the rules are desired.
97 * @param type The plural type (e.g., cardinal or ordinal).
98 * @param status A pointer to a UErrorCode to receive any errors.
99 * @return A UPluralRules for the specified locale, or NULL if an error occurred.
102 U_CAPI UPluralRules
* U_EXPORT2
103 uplrules_openForType(const char *locale
, UPluralType type
, UErrorCode
*status
);
106 * Closes a UPluralRules object. Once closed it may no longer be used.
107 * @param uplrules The UPluralRules object to close.
110 U_CAPI
void U_EXPORT2
111 uplrules_close(UPluralRules
*uplrules
);
114 #if U_SHOW_CPLUSPLUS_API
119 * \class LocalUPluralRulesPointer
120 * "Smart pointer" class, closes a UPluralRules via uplrules_close().
121 * For most methods see the LocalPointerBase base class.
123 * @see LocalPointerBase
127 U_DEFINE_LOCAL_OPEN_POINTER(LocalUPluralRulesPointer
, UPluralRules
, uplrules_close
);
131 #endif // U_SHOW_CPLUSPLUS_API
135 * Given a number, returns the keyword of the first rule that
136 * applies to the number, according to the supplied UPluralRules object.
137 * @param uplrules The UPluralRules object specifying the rules.
138 * @param number The number for which the rule has to be determined.
139 * @param keyword The keyword of the rule that applies to number.
140 * @param capacity The capacity of keyword.
141 * @param status A pointer to a UErrorCode to receive any errors.
142 * @return The length of keyword.
145 U_CAPI
int32_t U_EXPORT2
146 uplrules_select(const UPluralRules
*uplrules
,
148 UChar
*keyword
, int32_t capacity
,
151 #ifndef U_HIDE_INTERNAL_API
153 * Given a number, returns the keyword of the first rule that applies to the
154 * number, according to the UPluralRules object and given the number format
155 * specified by the UNumberFormat object.
156 * Note: This internal preview interface may be removed in the future if
157 * an architecturally cleaner solution reaches stable status.
158 * @param uplrules The UPluralRules object specifying the rules.
159 * @param number The number for which the rule has to be determined.
160 * @param fmt The UNumberFormat specifying how the number will be formatted
161 * (this can affect the plural form, e.g. "1 dollar" vs "1.0 dollars").
162 * If this is NULL, the function behaves like uplrules_select.
163 * @param keyword The keyword of the rule that applies to number.
164 * @param capacity The capacity of the keyword buffer.
165 * @param status A pointer to a UErrorCode to receive any errors.
166 * @return The length of keyword.
167 * @internal ICU 59 technology preview, may be removed in the future
169 U_INTERNAL
int32_t U_EXPORT2
170 uplrules_selectWithFormat(const UPluralRules
*uplrules
,
172 const UNumberFormat
*fmt
,
173 UChar
*keyword
, int32_t capacity
,
176 #endif /* U_HIDE_INTERNAL_API */
179 * Creates a string enumeration of all plural rule keywords used in this
180 * UPluralRules object. The rule "other" is always present by default.
181 * @param uplrules The UPluralRules object specifying the rules for
183 * @param status A pointer to a UErrorCode to receive any errors.
184 * @return a string enumeration over plural rule keywords, or NULL
185 * upon error. The caller is responsible for closing the result.
188 U_STABLE UEnumeration
* U_EXPORT2
189 uplrules_getKeywords(const UPluralRules
*uplrules
,
192 #endif /* #if !UCONFIG_NO_FORMATTING */