selfmt.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /********************************************************************
  2. * COPYRIGHT:
  3. * Copyright (c) 1997-2011, International Business Machines Corporation and
  4. * others. All Rights Reserved.
  5. * Copyright (C) 2010 , Yahoo! Inc.
  6. ********************************************************************
  7. *
  8. * File SELFMT.H
  9. *
  10. * Modification History:
  11. *
  12. * Date Name Description
  13. * 11/11/09 kirtig Finished first cut of implementation.
  14. ********************************************************************/
  15. #ifndef SELFMT
  16. #define SELFMT
  17. #include "unicode/messagepattern.h"
  18. #include "unicode/numfmt.h"
  19. #include "unicode/utypes.h"
  20. /**
  21. * \file
  22. * \brief C++ API: SelectFormat object
  23. */
  24. #if !UCONFIG_NO_FORMATTING
  25. U_NAMESPACE_BEGIN
  26. class MessageFormat;
  27. /**
  28. * <p><code>SelectFormat</code> supports the creation of internationalized
  29. * messages by selecting phrases based on keywords. The pattern specifies
  30. * how to map keywords to phrases and provides a default phrase. The
  31. * object provided to the format method is a string that's matched
  32. * against the keywords. If there is a match, the corresponding phrase
  33. * is selected; otherwise, the default phrase is used.</p>
  34. *
  35. * <h4>Using <code>SelectFormat</code> for Gender Agreement</h4>
  36. *
  37. * <p>Note: Typically, select formatting is done via <code>MessageFormat</code>
  38. * with a <code>select</code> argument type,
  39. * rather than using a stand-alone <code>SelectFormat</code>.</p>
  40. *
  41. * <p>The main use case for the select format is gender based inflection.
  42. * When names or nouns are inserted into sentences, their gender can affect pronouns,
  43. * verb forms, articles, and adjectives. Special care needs to be
  44. * taken for the case where the gender cannot be determined.
  45. * The impact varies between languages:</p>
  46. * \htmlonly
  47. * <ul>
  48. * <li>English has three genders, and unknown gender is handled as a special
  49. * case. Names use the gender of the named person (if known), nouns referring
  50. * to people use natural gender, and inanimate objects are usually neutral.
  51. * The gender only affects pronouns: "he", "she", "it", "they".
  52. *
  53. * <li>German differs from English in that the gender of nouns is rather
  54. * arbitrary, even for nouns referring to people ("M&#x00E4;dchen", girl, is neutral).
  55. * The gender affects pronouns ("er", "sie", "es"), articles ("der", "die",
  56. * "das"), and adjective forms ("guter Mann", "gute Frau", "gutes M&#x00E4;dchen").
  57. *
  58. * <li>French has only two genders; as in German the gender of nouns
  59. * is rather arbitrary - for sun and moon, the genders
  60. * are the opposite of those in German. The gender affects
  61. * pronouns ("il", "elle"), articles ("le", "la"),
  62. * adjective forms ("bon", "bonne"), and sometimes
  63. * verb forms ("all&#x00E9;", "all&#x00E9;e").
  64. *
  65. * <li>Polish distinguishes five genders (or noun classes),
  66. * human masculine, animate non-human masculine, inanimate masculine,
  67. * feminine, and neuter.
  68. * </ul>
  69. * \endhtmlonly
  70. * <p>Some other languages have noun classes that are not related to gender,
  71. * but similar in grammatical use.
  72. * Some African languages have around 20 noun classes.</p>
  73. *
  74. * <p><b>Note:</b>For the gender of a <i>person</i> in a given sentence,
  75. * we usually need to distinguish only between female, male and other/unknown.</p>
  76. *
  77. * <p>To enable localizers to create sentence patterns that take their
  78. * language's gender dependencies into consideration, software has to provide
  79. * information about the gender associated with a noun or name to
  80. * <code>MessageFormat</code>.
  81. * Two main cases can be distinguished:</p>
  82. *
  83. * <ul>
  84. * <li>For people, natural gender information should be maintained for each person.
  85. * Keywords like "male", "female", "mixed" (for groups of people)
  86. * and "unknown" could be used.
  87. *
  88. * <li>For nouns, grammatical gender information should be maintained for
  89. * each noun and per language, e.g., in resource bundles.
  90. * The keywords "masculine", "feminine", and "neuter" are commonly used,
  91. * but some languages may require other keywords.
  92. * </ul>
  93. *
  94. * <p>The resulting keyword is provided to <code>MessageFormat</code> as a
  95. * parameter separate from the name or noun it's associated with. For example,
  96. * to generate a message such as "Jean went to Paris", three separate arguments
  97. * would be provided: The name of the person as argument 0, the gender of
  98. * the person as argument 1, and the name of the city as argument 2.
  99. * The sentence pattern for English, where the gender of the person has
  100. * no impact on this simple sentence, would not refer to argument 1 at all:</p>
  101. *
  102. * <pre>{0} went to {2}.</pre>
  103. *
  104. * <p><b>Note:</b> The entire sentence should be included (and partially repeated)
  105. * inside each phrase. Otherwise translators would have to be trained on how to
  106. * move bits of the sentence in and out of the select argument of a message.
  107. * (The examples below do not follow this recommendation!)</p>
  108. *
  109. * <p>The sentence pattern for French, where the gender of the person affects
  110. * the form of the participle, uses a select format based on argument 1:</p>
  111. *
  112. * \htmlonly<pre>{0} est {1, select, female {all&#x00E9;e} other {all&#x00E9;}} &#x00E0; {2}.</pre>\endhtmlonly
  113. *
  114. * <p>Patterns can be nested, so that it's possible to handle interactions of
  115. * number and gender where necessary. For example, if the above sentence should
  116. * allow for the names of several people to be inserted, the following sentence
  117. * pattern can be used (with argument 0 the list of people's names,
  118. * argument 1 the number of people, argument 2 their combined gender, and
  119. * argument 3 the city name):</p>
  120. *
  121. * \htmlonly
  122. * <pre>{0} {1, plural,
  123. * one {est {2, select, female {all&#x00E9;e} other {all&#x00E9;}}}
  124. * other {sont {2, select, female {all&#x00E9;es} other {all&#x00E9;s}}}
  125. * }&#x00E0; {3}.</pre>
  126. * \endhtmlonly
  127. *
  128. * <h4>Patterns and Their Interpretation</h4>
  129. *
  130. * <p>The <code>SelectFormat</code> pattern string defines the phrase output
  131. * for each user-defined keyword.
  132. * The pattern is a sequence of (keyword, message) pairs.
  133. * A keyword is a "pattern identifier": [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+</p>
  134. *
  135. * <p>Each message is a MessageFormat pattern string enclosed in {curly braces}.</p>
  136. *
  137. * <p>You always have to define a phrase for the default keyword
  138. * <code>other</code>; this phrase is returned when the keyword
  139. * provided to
  140. * the <code>format</code> method matches no other keyword.
  141. * If a pattern does not provide a phrase for <code>other</code>, the method
  142. * it's provided to returns the error <code>U_DEFAULT_KEYWORD_MISSING</code>.
  143. * <br>
  144. * Pattern_White_Space between keywords and messages is ignored.
  145. * Pattern_White_Space within a message is preserved and output.</p>
  146. *
  147. * <p><pre>Example:
  148. * \htmlonly
  149. *
  150. * UErrorCode status = U_ZERO_ERROR;
  151. * MessageFormat *msgFmt = new MessageFormat(UnicodeString("{0} est {1, select, female {all&#x00E9;e} other {all&#x00E9;}} &#x00E0; Paris."), Locale("fr"), status);
  152. * if (U_FAILURE(status)) {
  153. * return;
  154. * }
  155. * FieldPosition ignore(FieldPosition::DONT_CARE);
  156. * UnicodeString result;
  157. *
  158. * char* str1= "Kirti,female";
  159. * Formattable args1[] = {"Kirti","female"};
  160. * msgFmt->format(args1, 2, result, ignore, status);
  161. * cout << "Input is " << str1 << " and result is: " << result << endl;
  162. * delete msgFmt;
  163. *
  164. * \endhtmlonly
  165. * </pre>
  166. * </p>
  167. *
  168. * Produces the output:<br>
  169. * \htmlonly
  170. * <code>Kirti est all&#x00E9;e &#x00E0; Paris.</code>
  171. * \endhtmlonly
  172. *
  173. * @stable ICU 4.4
  174. */
  175. class U_I18N_API SelectFormat : public Format {
  176. public:
  177. /**
  178. * Creates a new <code>SelectFormat</code> for a given pattern string.
  179. * @param pattern the pattern for this <code>SelectFormat</code>.
  180. * errors are returned to status if the pattern is invalid.
  181. * @param status output param set to success/failure code on exit, which
  182. * must not indicate a failure before the function call.
  183. * @stable ICU 4.4
  184. */
  185. SelectFormat(const UnicodeString& pattern, UErrorCode& status);
  186. /**
  187. * copy constructor.
  188. * @stable ICU 4.4
  189. */
  190. SelectFormat(const SelectFormat& other);
  191. /**
  192. * Destructor.
  193. * @stable ICU 4.4
  194. */
  195. virtual ~SelectFormat();
  196. /**
  197. * Sets the pattern used by this select format.
  198. * for the keyword rules.
  199. * Patterns and their interpretation are specified in the class description.
  200. *
  201. * @param pattern the pattern for this select format
  202. * errors are returned to status if the pattern is invalid.
  203. * @param status output param set to success/failure code on exit, which
  204. * must not indicate a failure before the function call.
  205. * @stable ICU 4.4
  206. */
  207. void applyPattern(const UnicodeString& pattern, UErrorCode& status);
  208. using Format::format;
  209. /**
  210. * Selects the phrase for the given keyword
  211. *
  212. * @param keyword The keyword that is used to select an alternative.
  213. * @param appendTo output parameter to receive result.
  214. * result is appended to existing contents.
  215. * @param pos On input: an alignment field, if desired.
  216. * On output: the offsets of the alignment field.
  217. * @param status output param set to success/failure code on exit, which
  218. * must not indicate a failure before the function call.
  219. * @return Reference to 'appendTo' parameter.
  220. * @stable ICU 4.4
  221. */
  222. UnicodeString& format(const UnicodeString& keyword,
  223. UnicodeString& appendTo,
  224. FieldPosition& pos,
  225. UErrorCode& status) const;
  226. /**
  227. * Assignment operator
  228. *
  229. * @param other the SelectFormat object to copy from.
  230. * @stable ICU 4.4
  231. */
  232. SelectFormat& operator=(const SelectFormat& other);
  233. /**
  234. * Return true if another object is semantically equal to this one.
  235. *
  236. * @param other the SelectFormat object to be compared with.
  237. * @return true if other is semantically equal to this.
  238. * @stable ICU 4.4
  239. */
  240. virtual UBool operator==(const Format& other) const;
  241. /**
  242. * Return true if another object is semantically unequal to this one.
  243. *
  244. * @param other the SelectFormat object to be compared with.
  245. * @return true if other is semantically unequal to this.
  246. * @stable ICU 4.4
  247. */
  248. virtual UBool operator!=(const Format& other) const;
  249. /**
  250. * Clones this Format object polymorphically. The caller owns the
  251. * result and should delete it when done.
  252. * @stable ICU 4.4
  253. */
  254. virtual Format* clone(void) const;
  255. /**
  256. * Format an object to produce a string.
  257. * This method handles keyword strings.
  258. * If the Formattable object is not a <code>UnicodeString</code>,
  259. * then it returns a failing UErrorCode.
  260. *
  261. * @param obj A keyword string that is used to select an alternative.
  262. * @param appendTo output parameter to receive result.
  263. * Result is appended to existing contents.
  264. * @param pos On input: an alignment field, if desired.
  265. * On output: the offsets of the alignment field.
  266. * @param status output param filled with success/failure status.
  267. * @return Reference to 'appendTo' parameter.
  268. * @stable ICU 4.4
  269. */
  270. UnicodeString& format(const Formattable& obj,
  271. UnicodeString& appendTo,
  272. FieldPosition& pos,
  273. UErrorCode& status) const;
  274. /**
  275. * Returns the pattern from applyPattern() or constructor.
  276. *
  277. * @param appendTo output parameter to receive result.
  278. * Result is appended to existing contents.
  279. * @return the UnicodeString with inserted pattern.
  280. * @stable ICU 4.4
  281. */
  282. UnicodeString& toPattern(UnicodeString& appendTo);
  283. /**
  284. * This method is not yet supported by <code>SelectFormat</code>.
  285. * <P>
  286. * Before calling, set parse_pos.index to the offset you want to start
  287. * parsing at in the source. After calling, parse_pos.index is the end of
  288. * the text you parsed. If error occurs, index is unchanged.
  289. * <P>
  290. * When parsing, leading whitespace is discarded (with a successful parse),
  291. * while trailing whitespace is left as is.
  292. * <P>
  293. * See Format::parseObject() for more.
  294. *
  295. * @param source The string to be parsed into an object.
  296. * @param result Formattable to be set to the parse result.
  297. * If parse fails, return contents are undefined.
  298. * @param parse_pos The position to start parsing at. Upon return
  299. * this param is set to the position after the
  300. * last character successfully parsed. If the
  301. * source is not parsed successfully, this param
  302. * will remain unchanged.
  303. * @stable ICU 4.4
  304. */
  305. virtual void parseObject(const UnicodeString& source,
  306. Formattable& result,
  307. ParsePosition& parse_pos) const;
  308. /**
  309. * ICU "poor man's RTTI", returns a UClassID for this class.
  310. * @stable ICU 4.4
  311. */
  312. static UClassID U_EXPORT2 getStaticClassID(void);
  313. /**
  314. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  315. * @stable ICU 4.4
  316. */
  317. virtual UClassID getDynamicClassID() const;
  318. private:
  319. friend class MessageFormat;
  320. SelectFormat(); // default constructor not implemented.
  321. /**
  322. * Finds the SelectFormat sub-message for the given keyword, or the "other" sub-message.
  323. * @param pattern A MessagePattern.
  324. * @param partIndex the index of the first SelectFormat argument style part.
  325. * @param keyword a keyword to be matched to one of the SelectFormat argument's keywords.
  326. * @param ec Error code.
  327. * @return the sub-message start part index.
  328. */
  329. static int32_t findSubMessage(const MessagePattern& pattern, int32_t partIndex,
  330. const UnicodeString& keyword, UErrorCode& ec);
  331. MessagePattern msgPattern;
  332. };
  333. U_NAMESPACE_END
  334. #endif /* #if !UCONFIG_NO_FORMATTING */
  335. #endif // _SELFMT
  336. //eof