selfmt.h 14 KB

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