| 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 | /** |
| 156 | * Given a formatted number, returns the keyword of the first rule |
| 157 | * that applies to the number, according to the supplied UPluralRules object. |
| 158 | * |
| 159 | * A UFormattedNumber allows you to specify an exponent or trailing zeros, |
| 160 | * which can affect the plural category. To get a UFormattedNumber, see |
| 161 | * {@link UNumberFormatter}. |
| 162 | * |
| 163 | * @param uplrules The UPluralRules object specifying the rules. |
| 164 | * @param number The formatted number for which the rule has to be determined. |
| 165 | * @param keyword The destination buffer for the keyword of the rule that |
| 166 | * applies to number. |
| 167 | * @param capacity The capacity of the keyword buffer. |
| 168 | * @param status A pointer to a UErrorCode to receive any errors. |
| 169 | * @return The length of the keyword. |
| 170 | * @stable ICU 64 |
| 171 | */ |
| 172 | U_CAPI int32_t U_EXPORT2 |
| 173 | uplrules_selectFormatted(const UPluralRules *uplrules, |
| 174 | const struct UFormattedNumber* number, |
| 175 | UChar *keyword, int32_t capacity, |
| 176 | UErrorCode *status); |
| 177 | |
| 178 | #ifndef U_HIDE_INTERNAL_API |
| 179 | /** |
| 180 | * Given a number, returns the keyword of the first rule that applies to the |
| 181 | * number, according to the UPluralRules object and given the number format |
| 182 | * specified by the UNumberFormat object. |
| 183 | * Note: This internal preview interface may be removed in the future if |
| 184 | * an architecturally cleaner solution reaches stable status. |
| 185 | * @param uplrules The UPluralRules object specifying the rules. |
| 186 | * @param number The number for which the rule has to be determined. |
| 187 | * @param fmt The UNumberFormat specifying how the number will be formatted |
| 188 | * (this can affect the plural form, e.g. "1 dollar" vs "1.0 dollars"). |
| 189 | * If this is NULL, the function behaves like uplrules_select. |
| 190 | * @param keyword An output buffer to write the keyword of the rule that |
| 191 | * applies to number. |
| 192 | * @param capacity The capacity of the keyword buffer. |
| 193 | * @param status A pointer to a UErrorCode to receive any errors. |
| 194 | * @return The length of keyword. |
| 195 | * @internal ICU 59 technology preview, may be removed in the future |
| 196 | */ |
| 197 | U_INTERNAL int32_t U_EXPORT2 |
| 198 | uplrules_selectWithFormat(const UPluralRules *uplrules, |
| 199 | double number, |
| 200 | const UNumberFormat *fmt, |
| 201 | UChar *keyword, int32_t capacity, |
| 202 | UErrorCode *status); |
| 203 | |
| 204 | #endif /* U_HIDE_INTERNAL_API */ |
| 205 | |
| 206 | /** |
| 207 | * Creates a string enumeration of all plural rule keywords used in this |
| 208 | * UPluralRules object. The rule "other" is always present by default. |
| 209 | * @param uplrules The UPluralRules object specifying the rules for |
| 210 | * a given locale. |
| 211 | * @param status A pointer to a UErrorCode to receive any errors. |
| 212 | * @return a string enumeration over plural rule keywords, or NULL |
| 213 | * upon error. The caller is responsible for closing the result. |
| 214 | * @stable ICU 59 |
| 215 | */ |
| 216 | U_STABLE UEnumeration* U_EXPORT2 |
| 217 | uplrules_getKeywords(const UPluralRules *uplrules, |
| 218 | UErrorCode *status); |
| 219 | |
| 220 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 221 | |
| 222 | #endif |
| 223 | |