upluralrules.h 8.8 KB

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