upluralrules.h 7.8 KB

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