selfmt.h 14 KB

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