michael@0: /******************************************************************** michael@0: * COPYRIGHT: michael@0: * Copyright (c) 1997-2011, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: * Copyright (C) 2010 , Yahoo! Inc. michael@0: ******************************************************************** michael@0: * michael@0: * File SELFMT.H michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 11/11/09 kirtig Finished first cut of implementation. michael@0: ********************************************************************/ michael@0: michael@0: #ifndef SELFMT michael@0: #define SELFMT michael@0: michael@0: #include "unicode/messagepattern.h" michael@0: #include "unicode/numfmt.h" michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: SelectFormat object michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class MessageFormat; michael@0: michael@0: /** michael@0: *

SelectFormat supports the creation of internationalized michael@0: * messages by selecting phrases based on keywords. The pattern specifies michael@0: * how to map keywords to phrases and provides a default phrase. The michael@0: * object provided to the format method is a string that's matched michael@0: * against the keywords. If there is a match, the corresponding phrase michael@0: * is selected; otherwise, the default phrase is used.

michael@0: * michael@0: *

Using SelectFormat for Gender Agreement

michael@0: * michael@0: *

Note: Typically, select formatting is done via MessageFormat michael@0: * with a select argument type, michael@0: * rather than using a stand-alone SelectFormat.

michael@0: * michael@0: *

The main use case for the select format is gender based inflection. michael@0: * When names or nouns are inserted into sentences, their gender can affect pronouns, michael@0: * verb forms, articles, and adjectives. Special care needs to be michael@0: * taken for the case where the gender cannot be determined. michael@0: * The impact varies between languages:

michael@0: * \htmlonly michael@0: * michael@0: * \endhtmlonly michael@0: *

Some other languages have noun classes that are not related to gender, michael@0: * but similar in grammatical use. michael@0: * Some African languages have around 20 noun classes.

michael@0: * michael@0: *

Note:For the gender of a person in a given sentence, michael@0: * we usually need to distinguish only between female, male and other/unknown.

michael@0: * michael@0: *

To enable localizers to create sentence patterns that take their michael@0: * language's gender dependencies into consideration, software has to provide michael@0: * information about the gender associated with a noun or name to michael@0: * MessageFormat. michael@0: * Two main cases can be distinguished:

michael@0: * michael@0: * michael@0: * michael@0: *

The resulting keyword is provided to MessageFormat as a michael@0: * parameter separate from the name or noun it's associated with. For example, michael@0: * to generate a message such as "Jean went to Paris", three separate arguments michael@0: * would be provided: The name of the person as argument 0, the gender of michael@0: * the person as argument 1, and the name of the city as argument 2. michael@0: * The sentence pattern for English, where the gender of the person has michael@0: * no impact on this simple sentence, would not refer to argument 1 at all:

michael@0: * michael@0: *
{0} went to {2}.
michael@0: * michael@0: *

Note: The entire sentence should be included (and partially repeated) michael@0: * inside each phrase. Otherwise translators would have to be trained on how to michael@0: * move bits of the sentence in and out of the select argument of a message. michael@0: * (The examples below do not follow this recommendation!)

michael@0: * michael@0: *

The sentence pattern for French, where the gender of the person affects michael@0: * the form of the participle, uses a select format based on argument 1:

michael@0: * michael@0: * \htmlonly
{0} est {1, select, female {allée} other {allé}} à {2}.
\endhtmlonly michael@0: * michael@0: *

Patterns can be nested, so that it's possible to handle interactions of michael@0: * number and gender where necessary. For example, if the above sentence should michael@0: * allow for the names of several people to be inserted, the following sentence michael@0: * pattern can be used (with argument 0 the list of people's names, michael@0: * argument 1 the number of people, argument 2 their combined gender, and michael@0: * argument 3 the city name):

michael@0: * michael@0: * \htmlonly michael@0: *
{0} {1, plural,
michael@0:   *                 one {est {2, select, female {allée} other  {allé}}}
michael@0:   *                 other {sont {2, select, female {allées} other {allés}}}
michael@0:   *          }à {3}.
michael@0: * \endhtmlonly michael@0: * michael@0: *

Patterns and Their Interpretation

michael@0: * michael@0: *

The SelectFormat pattern string defines the phrase output michael@0: * for each user-defined keyword. michael@0: * The pattern is a sequence of (keyword, message) pairs. michael@0: * A keyword is a "pattern identifier": [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+

michael@0: * michael@0: *

Each message is a MessageFormat pattern string enclosed in {curly braces}.

michael@0: * michael@0: *

You always have to define a phrase for the default keyword michael@0: * other; this phrase is returned when the keyword michael@0: * provided to michael@0: * the format method matches no other keyword. michael@0: * If a pattern does not provide a phrase for other, the method michael@0: * it's provided to returns the error U_DEFAULT_KEYWORD_MISSING. michael@0: *
michael@0: * Pattern_White_Space between keywords and messages is ignored. michael@0: * Pattern_White_Space within a message is preserved and output.

michael@0: * michael@0: *

Example:
michael@0:   * \htmlonly
michael@0:   *
michael@0:   * UErrorCode status = U_ZERO_ERROR;
michael@0:   * MessageFormat *msgFmt = new MessageFormat(UnicodeString("{0} est  {1, select, female {allée} other {allé}} à Paris."), Locale("fr"),  status);
michael@0:   * if (U_FAILURE(status)) {
michael@0:   *       return;
michael@0:   * }
michael@0:   * FieldPosition ignore(FieldPosition::DONT_CARE);
michael@0:   * UnicodeString result;
michael@0:   *
michael@0:   * char* str1= "Kirti,female";
michael@0:   * Formattable args1[] = {"Kirti","female"};
michael@0:   * msgFmt->format(args1, 2, result, ignore, status);
michael@0:   * cout << "Input is " << str1 << " and result is: " << result << endl;
michael@0:   * delete msgFmt;
michael@0:   *
michael@0:   * \endhtmlonly
michael@0:   * 
michael@0: *

michael@0: * michael@0: * Produces the output:
michael@0: * \htmlonly michael@0: * Kirti est allée à Paris. michael@0: * \endhtmlonly michael@0: * michael@0: * @stable ICU 4.4 michael@0: */ michael@0: michael@0: class U_I18N_API SelectFormat : public Format { michael@0: public: michael@0: michael@0: /** michael@0: * Creates a new SelectFormat for a given pattern string. michael@0: * @param pattern the pattern for this SelectFormat. michael@0: * errors are returned to status if the pattern is invalid. michael@0: * @param status output param set to success/failure code on exit, which michael@0: * must not indicate a failure before the function call. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: SelectFormat(const UnicodeString& pattern, UErrorCode& status); michael@0: michael@0: /** michael@0: * copy constructor. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: SelectFormat(const SelectFormat& other); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: virtual ~SelectFormat(); michael@0: michael@0: /** michael@0: * Sets the pattern used by this select format. michael@0: * for the keyword rules. michael@0: * Patterns and their interpretation are specified in the class description. michael@0: * michael@0: * @param pattern the pattern for this select format michael@0: * errors are returned to status if the pattern is invalid. michael@0: * @param status output param set to success/failure code on exit, which michael@0: * must not indicate a failure before the function call. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: void applyPattern(const UnicodeString& pattern, UErrorCode& status); michael@0: michael@0: michael@0: using Format::format; michael@0: michael@0: /** michael@0: * Selects the phrase for the given keyword michael@0: * michael@0: * @param keyword The keyword that is used to select an alternative. michael@0: * @param appendTo output parameter to receive result. michael@0: * result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status output param set to success/failure code on exit, which michael@0: * must not indicate a failure before the function call. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: UnicodeString& format(const UnicodeString& keyword, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Assignment operator michael@0: * michael@0: * @param other the SelectFormat object to copy from. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: SelectFormat& operator=(const SelectFormat& other); michael@0: michael@0: /** michael@0: * Return true if another object is semantically equal to this one. michael@0: * michael@0: * @param other the SelectFormat object to be compared with. michael@0: * @return true if other is semantically equal to this. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: virtual UBool operator==(const Format& other) const; michael@0: michael@0: /** michael@0: * Return true if another object is semantically unequal to this one. michael@0: * michael@0: * @param other the SelectFormat object to be compared with. michael@0: * @return true if other is semantically unequal to this. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: virtual UBool operator!=(const Format& other) const; michael@0: michael@0: /** michael@0: * Clones this Format object polymorphically. The caller owns the michael@0: * result and should delete it when done. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: virtual Format* clone(void) const; michael@0: michael@0: /** michael@0: * Format an object to produce a string. michael@0: * This method handles keyword strings. michael@0: * If the Formattable object is not a UnicodeString, michael@0: * then it returns a failing UErrorCode. michael@0: * michael@0: * @param obj A keyword string that is used to select an alternative. michael@0: * @param appendTo output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: UnicodeString& format(const Formattable& obj, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Returns the pattern from applyPattern() or constructor. michael@0: * michael@0: * @param appendTo output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @return the UnicodeString with inserted pattern. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: UnicodeString& toPattern(UnicodeString& appendTo); michael@0: michael@0: /** michael@0: * This method is not yet supported by SelectFormat. michael@0: *

michael@0: * Before calling, set parse_pos.index to the offset you want to start michael@0: * parsing at in the source. After calling, parse_pos.index is the end of michael@0: * the text you parsed. If error occurs, index is unchanged. michael@0: *

michael@0: * When parsing, leading whitespace is discarded (with a successful parse), michael@0: * while trailing whitespace is left as is. michael@0: *

michael@0: * See Format::parseObject() for more. michael@0: * michael@0: * @param source The string to be parsed into an object. michael@0: * @param result Formattable to be set to the parse result. michael@0: * If parse fails, return contents are undefined. michael@0: * @param parse_pos The position to start parsing at. Upon return michael@0: * this param is set to the position after the michael@0: * last character successfully parsed. If the michael@0: * source is not parsed successfully, this param michael@0: * will remain unchanged. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: virtual void parseObject(const UnicodeString& source, michael@0: Formattable& result, michael@0: ParsePosition& parse_pos) const; michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for this class. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for the actual class. michael@0: * @stable ICU 4.4 michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: private: michael@0: friend class MessageFormat; michael@0: michael@0: SelectFormat(); // default constructor not implemented. michael@0: michael@0: /** michael@0: * Finds the SelectFormat sub-message for the given keyword, or the "other" sub-message. michael@0: * @param pattern A MessagePattern. michael@0: * @param partIndex the index of the first SelectFormat argument style part. michael@0: * @param keyword a keyword to be matched to one of the SelectFormat argument's keywords. michael@0: * @param ec Error code. michael@0: * @return the sub-message start part index. michael@0: */ michael@0: static int32_t findSubMessage(const MessagePattern& pattern, int32_t partIndex, michael@0: const UnicodeString& keyword, UErrorCode& ec); michael@0: michael@0: MessagePattern msgPattern; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // _SELFMT michael@0: //eof