• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *****************************************************************************************
3 * Copyright (C) 2010-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *****************************************************************************************
6 */
7 
8 #ifndef UPLURALRULES_H
9 #define UPLURALRULES_H
10 
11 #include "unicode/utypes.h"
12 
13 #if !UCONFIG_NO_FORMATTING
14 
15 #include "unicode/localpointer.h"
16 
17 /**
18  * \file
19  * \brief C API: Plural rules, select plural keywords for numeric values.
20  *
21  * A UPluralRules object defines rules for mapping non-negative numeric
22  * values onto a small set of keywords. Rules are constructed from a text
23  * description, consisting of a series of keywords and conditions.
24  * The uplrules_select function examines each condition in order and
25  * returns the keyword for the first condition that matches the number.
26  * If none match, the default rule(other) is returned.
27  *
28  * For more information, see the LDML spec, C.11 Language Plural Rules:
29  * http://www.unicode.org/reports/tr35/#Language_Plural_Rules
30  *
31  * Keywords: ICU locale data has 6 predefined values -
32  * 'zero', 'one', 'two', 'few', 'many' and 'other'. Callers need to check
33  * the value of keyword returned by the uplrules_select function.
34  *
35  * These are based on CLDR <i>Language Plural Rules</i>. For these
36  * predefined rules, see the CLDR page at
37  * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
38  */
39 
40 #ifndef U_HIDE_DRAFT_API
41 /**
42  * Type of plurals and PluralRules.
43  * @draft ICU 50
44  */
45 enum UPluralType {
46     /**
47      * Plural rules for cardinal numbers: 1 file vs. 2 files.
48      * @draft ICU 50
49      */
50     UPLURAL_TYPE_CARDINAL,
51     /**
52      * Plural rules for ordinal numbers: 1st file, 2nd file, 3rd file, 4th file, etc.
53      * @draft ICU 50
54      */
55     UPLURAL_TYPE_ORDINAL,
56     /**
57      * Number of Plural rules types.
58      * @draft ICU 50
59      */
60     UPLURAL_TYPE_COUNT
61 };
62 /**
63  * @draft ICU 50
64  */
65 typedef enum UPluralType UPluralType;
66 #endif /* U_HIDE_DRAFT_API */
67 
68 /**
69  * Opaque UPluralRules object for use in C programs.
70  * @stable ICU 4.8
71  */
72 struct UPluralRules;
73 typedef struct UPluralRules UPluralRules;  /**< C typedef for struct UPluralRules. @stable ICU 4.8 */
74 
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_STABLE UPluralRules* U_EXPORT2
85 uplrules_open(const char *locale, UErrorCode *status);
86 
87 #ifndef U_HIDE_DRAFT_API
88 /**
89  * Opens a new UPluralRules object using the predefined plural rules for a
90  * given locale and the plural type.
91  * @param locale The locale for which the rules are desired.
92  * @param type The plural type (e.g., cardinal or ordinal).
93  * @param status A pointer to a UErrorCode to receive any errors.
94  * @return A UPluralRules for the specified locale, or NULL if an error occurred.
95  * @draft ICU 50
96  */
97 U_DRAFT UPluralRules* U_EXPORT2
98 uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status);
99 #endif /* U_HIDE_DRAFT_API */
100 
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_STABLE void U_EXPORT2
107 uplrules_close(UPluralRules *uplrules);
108 
109 
110 #if U_SHOW_CPLUSPLUS_API
111 
112 U_NAMESPACE_BEGIN
113 
114 /**
115  * \class LocalUPluralRulesPointer
116  * "Smart pointer" class, closes a UPluralRules via uplrules_close().
117  * For most methods see the LocalPointerBase base class.
118  *
119  * @see LocalPointerBase
120  * @see LocalPointer
121  * @stable ICU 4.8
122  */
123 U_DEFINE_LOCAL_OPEN_POINTER(LocalUPluralRulesPointer, UPluralRules, uplrules_close);
124 
125 U_NAMESPACE_END
126 
127 #endif
128 
129 
130 /**
131  * Given a number, returns the keyword of the first rule that
132  * applies to the number, according to the supplied UPluralRules object.
133  * @param uplrules The UPluralRules object specifying the rules.
134  * @param number The number for which the rule has to be determined.
135  * @param keyword The keyword of the rule that applies to number.
136  * @param capacity The capacity of keyword.
137  * @param status A pointer to a UErrorCode to receive any errors.
138  * @return The length of keyword.
139  * @stable ICU 4.8
140  */
141 U_STABLE int32_t U_EXPORT2
142 uplrules_select(const UPluralRules *uplrules,
143                double number,
144                UChar *keyword, int32_t capacity,
145                UErrorCode *status);
146 
147 #endif /* #if !UCONFIG_NO_FORMATTING */
148 
149 #endif
150