upluralrules.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 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. /**
  15. * \file
  16. * \brief C API: Plural rules, select plural keywords for numeric values.
  17. *
  18. * A UPluralRules object defines rules for mapping non-negative numeric
  19. * values onto a small set of keywords. Rules are constructed from a text
  20. * description, consisting of a series of keywords and conditions.
  21. * The uplrules_select function examines each condition in order and
  22. * returns the keyword for the first condition that matches the number.
  23. * If none match, the default rule(other) is returned.
  24. *
  25. * For more information, see the LDML spec, C.11 Language Plural Rules:
  26. * http://www.unicode.org/reports/tr35/#Language_Plural_Rules
  27. *
  28. * Keywords: ICU locale data has 6 predefined values -
  29. * 'zero', 'one', 'two', 'few', 'many' and 'other'. Callers need to check
  30. * the value of keyword returned by the uplrules_select function.
  31. *
  32. * These are based on CLDR <i>Language Plural Rules</i>. For these
  33. * predefined rules, see the CLDR page at
  34. * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
  35. */
  36. /**
  37. * Type of plurals and PluralRules.
  38. * @stable ICU 50
  39. */
  40. enum UPluralType {
  41. /**
  42. * Plural rules for cardinal numbers: 1 file vs. 2 files.
  43. * @stable ICU 50
  44. */
  45. UPLURAL_TYPE_CARDINAL,
  46. /**
  47. * Plural rules for ordinal numbers: 1st file, 2nd file, 3rd file, 4th file, etc.
  48. * @stable ICU 50
  49. */
  50. UPLURAL_TYPE_ORDINAL,
  51. #ifndef U_HIDE_DEPRECATED_API
  52. /**
  53. * One more than the highest normal UPluralType value.
  54. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  55. */
  56. UPLURAL_TYPE_COUNT
  57. #endif // U_HIDE_DEPRECATED_API
  58. };
  59. /**
  60. * @stable ICU 50
  61. */
  62. typedef enum UPluralType UPluralType;
  63. /**
  64. * Opaque UPluralRules object for use in C programs.
  65. * @stable ICU 4.8
  66. */
  67. struct UPluralRules;
  68. typedef struct UPluralRules UPluralRules; /**< C typedef for struct UPluralRules. @stable ICU 4.8 */
  69. /**
  70. * Opens a new UPluralRules object using the predefined cardinal-number plural rules for a
  71. * given locale.
  72. * Same as uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status).
  73. * @param locale The locale for which the rules are desired.
  74. * @param status A pointer to a UErrorCode to receive any errors.
  75. * @return A UPluralRules for the specified locale, or NULL if an error occurred.
  76. * @stable ICU 4.8
  77. */
  78. U_STABLE UPluralRules* U_EXPORT2
  79. uplrules_open(const char *locale, UErrorCode *status);
  80. /**
  81. * Opens a new UPluralRules object using the predefined plural rules for a
  82. * given locale and the plural type.
  83. * @param locale The locale for which the rules are desired.
  84. * @param type The plural type (e.g., cardinal or ordinal).
  85. * @param status A pointer to a UErrorCode to receive any errors.
  86. * @return A UPluralRules for the specified locale, or NULL if an error occurred.
  87. * @stable ICU 50
  88. */
  89. U_DRAFT UPluralRules* U_EXPORT2
  90. uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status);
  91. /**
  92. * Closes a UPluralRules object. Once closed it may no longer be used.
  93. * @param uplrules The UPluralRules object to close.
  94. * @stable ICU 4.8
  95. */
  96. U_STABLE void U_EXPORT2
  97. uplrules_close(UPluralRules *uplrules);
  98. #if U_SHOW_CPLUSPLUS_API
  99. U_NAMESPACE_BEGIN
  100. /**
  101. * \class LocalUPluralRulesPointer
  102. * "Smart pointer" class, closes a UPluralRules via uplrules_close().
  103. * For most methods see the LocalPointerBase base class.
  104. *
  105. * @see LocalPointerBase
  106. * @see LocalPointer
  107. * @stable ICU 4.8
  108. */
  109. U_DEFINE_LOCAL_OPEN_POINTER(LocalUPluralRulesPointer, UPluralRules, uplrules_close);
  110. U_NAMESPACE_END
  111. #endif
  112. /**
  113. * Given a number, returns the keyword of the first rule that
  114. * applies to the number, according to the supplied UPluralRules object.
  115. * @param uplrules The UPluralRules object specifying the rules.
  116. * @param number The number for which the rule has to be determined.
  117. * @param keyword The keyword of the rule that applies to number.
  118. * @param capacity The capacity of keyword.
  119. * @param status A pointer to a UErrorCode to receive any errors.
  120. * @return The length of keyword.
  121. * @stable ICU 4.8
  122. */
  123. U_STABLE int32_t U_EXPORT2
  124. uplrules_select(const UPluralRules *uplrules,
  125. double number,
  126. UChar *keyword, int32_t capacity,
  127. UErrorCode *status);
  128. #endif /* #if !UCONFIG_NO_FORMATTING */
  129. #endif