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