Movatterモバイル変換


[0]ホーム

URL:


ICU 78.1  78.1
Data Structures |Public Types |Public Member Functions |Static Public Member Functions |Friends
icu::MessageFormat Class Reference

#include <msgfmt.h>

Inheritance diagram for icu::MessageFormat:

Public Types

enum  EFormatNumber {kMaxFormat = 10 }
 Enum type for kMaxFormat.More...
 

Public Member Functions

U_I18N_API MessageFormat (constUnicodeString &pattern,UErrorCode &status)
 Constructs a newMessageFormat using the given pattern and the default locale.More...
 
U_I18N_API MessageFormat (constUnicodeString &pattern, constLocale &newLocale,UErrorCode &status)
 Constructs a newMessageFormat using the given pattern and locale.More...
 
U_I18N_API MessageFormat (constUnicodeString &pattern, constLocale &newLocale,UParseError &parseError,UErrorCode &status)
 Constructs a newMessageFormat using the given pattern and locale.More...
 
U_I18N_API MessageFormat (constMessageFormat &)
 Constructs a newMessageFormat from an existing one.More...
 
U_I18N_API constMessageFormatoperator= (constMessageFormat &)
 Assignment operator.More...
 
virtualU_I18N_API ~MessageFormat ()
 Destructor.More...
 
virtualU_I18N_APIMessageFormatclone () const override
 Clones thisFormat object polymorphically.More...
 
virtualU_I18N_API bool operator== (constFormat &other) const override
 Returns true if the givenFormat objects are semantically equal.More...
 
virtualU_I18N_API void setLocale (constLocale &theLocale)
 Sets the locale to be used for creating argumentFormat objects.More...
 
virtualU_I18N_API constLocalegetLocale () const
 Gets the locale used for creating argumentFormat objects.More...
 
virtualU_I18N_API void applyPattern (constUnicodeString &pattern,UErrorCode &status)
 Applies the given pattern string to this message format.More...
 
virtualU_I18N_API void applyPattern (constUnicodeString &pattern,UParseError &parseError,UErrorCode &status)
 Applies the given pattern string to this message format.More...
 
virtualU_I18N_API void applyPattern (constUnicodeString &pattern,UMessagePatternApostropheMode aposMode,UParseError *parseError,UErrorCode &status)
 Sets the UMessagePatternApostropheMode and the pattern used by this message format.More...
 
U_I18N_APIUMessagePatternApostropheMode getApostropheMode () const
 
virtualU_I18N_APIUnicodeStringtoPattern (UnicodeString &appendTo) const
 Returns a pattern that can be used to recreate this object.More...
 
virtualU_I18N_API void adoptFormats (Format **formatsToAdopt, int32_t count)
 Sets subformats.More...
 
virtualU_I18N_API void setFormats (constFormat **newFormats, int32_t cnt)
 Sets subformats.More...
 
virtualU_I18N_API void adoptFormat (int32_t formatNumber,Format *formatToAdopt)
 Sets one subformat.More...
 
virtualU_I18N_API void setFormat (int32_t formatNumber, constFormat &format)
 Sets one subformat.More...
 
virtualU_I18N_APIStringEnumerationgetFormatNames (UErrorCode &status)
 Gets format names.More...
 
virtualU_I18N_APIFormatgetFormat (constUnicodeString &formatName,UErrorCode &status)
 Gets subformat pointer for given format name.More...
 
virtualU_I18N_API void setFormat (constUnicodeString &formatName, constFormat &format,UErrorCode &status)
 Sets one subformat for given format name.More...
 
virtualU_I18N_API void adoptFormat (constUnicodeString &formatName,Format *formatToAdopt,UErrorCode &status)
 Sets one subformat for given format name.More...
 
virtualU_I18N_API constFormat ** getFormats (int32_t &count) const
 Gets an array of subformats of this object.More...
 
U_I18N_APIUnicodeStringformat (constFormattable *source, int32_t count,UnicodeString &appendTo,FieldPosition &ignore,UErrorCode &status) const
 Formats the given array of arguments into a user-readable string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const override
 Formats the given array of arguments into a user-readable string.More...
 
U_I18N_APIUnicodeStringformat (constUnicodeString *argumentNames, constFormattable *arguments, int32_t count,UnicodeString &appendTo,UErrorCode &status) const
 Formats the given array of arguments into a user-defined argument name array.More...
 
virtualU_I18N_APIFormattableparse (constUnicodeString &source,ParsePosition &pos, int32_t &count) const
 Parses the given string into an array of output arguments.More...
 
virtualU_I18N_APIFormattableparse (constUnicodeString &source, int32_t &count,UErrorCode &status) const
 Parses the given string into an array of output arguments.More...
 
virtualU_I18N_API void parseObject (constUnicodeString &source,Formattable &result,ParsePosition &pos) const override
 Parses the given string into an array of output arguments stored within a singleFormattable of type kArray.More...
 
U_I18N_APIUBool usesNamedArguments () const
 Returns true if thisMessageFormat uses named arguments, and false otherwise.More...
 
U_I18N_API int32_t getArgTypeCount () const
 This API is for ICU internal use only.More...
 
virtualU_I18N_APIUClassID getDynamicClassID () const override
 Returns a unique class ID POLYMORPHICALLY.More...
 
U_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,UErrorCode &status) const
 Formats an object to produce a string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPosition &pos,UErrorCode &status) const=0
 Format an object to produce a string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPositionIterator *posIter,UErrorCode &status) const
 Format an object to produce a string.More...
 
- Public Member Functions inherited fromicu::Format
virtualU_I18N_API ~Format ()
 Destructor.More...
 
U_I18N_API bool operator!= (constFormat &other) const
 Return true if the givenFormat objects are not semantically equal.More...
 
U_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,UErrorCode &status) const
 Formats an object to produce a string.More...
 
virtualU_I18N_APIUnicodeStringformat (constFormattable &obj,UnicodeString &appendTo,FieldPositionIterator *posIter,UErrorCode &status) const
 Format an object to produce a string.More...
 
U_I18N_API void parseObject (constUnicodeString &source,Formattable &result,UErrorCode &status) const
 Parses a string to produce an object.More...
 
U_I18N_APILocale getLocale (ULocDataLocaleType type,UErrorCode &status) const
 Get the locale for this format object.More...
 
U_I18N_API const char * getLocaleID (ULocDataLocaleType type,UErrorCode &status) const
 Get the locale for this format object.More...
 
- Public Member Functions inherited fromicu::UObject
virtual ~UObject ()
 Destructor.More...
 

Static Public Member Functions

staticU_I18N_APIUnicodeStringformat (constUnicodeString &pattern, constFormattable *arguments, int32_t count,UnicodeString &appendTo,UErrorCode &status)
 Formats the given array of arguments into a user-readable string using the given pattern.More...
 
staticU_I18N_APIUnicodeString autoQuoteApostrophe (constUnicodeString &pattern,UErrorCode &status)
 Convert an 'apostrophe-friendly' pattern into a standard pattern.More...
 
staticU_I18N_APIUClassID getStaticClassID ()
 Return the class ID for this class.More...
 
staticU_I18N_APIUBool equalFormats (const void *left, const void *right)
 Compares twoFormat objects.More...
 

Friends

class MessageFormatAdapter
 

Additional Inherited Members

- Protected Member Functions inherited fromicu::Format
U_I18N_API void setLocaleIDs (const char *valid, const char *actual)
 
U_I18N_API Format ()
 Default constructor for subclass use only.More...
 
U_I18N_API Format (constFormat &)
 
U_I18N_APIFormatoperator= (constFormat &)
 
- Static Protected Member Functions inherited fromicu::Format
staticU_I18N_API void syntaxError (constUnicodeString &pattern, int32_t pos,UParseError &parseError)
 Simple function for initializing aUParseError from aUnicodeString.More...
 

Detailed Description

MessageFormat prepares strings for display to users, with optional arguments (variables/placeholders). The arguments can occur in any order, which is necessary for translation into languages with different grammars.

AMessageFormat is constructed from apattern string with arguments in {curly braces} which will be replaced by formatted values.

MessageFormat differs from the otherFormat classes in that you create aMessageFormat object with one of its constructors (not with acreateInstance style factory method). Factory methods aren't necessary becauseMessageFormat itself doesn't implement locale-specific behavior. Any locale-specific behavior is defined by the pattern that you provide and the subformats used for inserted arguments.

Arguments can be named (using identifiers) or numbered (using small ASCII-digit integers). Some of the API methods work only with argument numbers and throw an exception if the pattern has named arguments (seeusesNamedArguments()).

An argument might not specify any format type. In this case, a numeric value is formatted with a default (for the locale)NumberFormat, and a date/time value is formatted with a default (for the locale)DateFormat.

An argument might specify a "simple" type for which the specifiedFormat object is created, cached and used.

An argument might have a "complex" type with nestedMessageFormat sub-patterns. During formatting, one of these sub-messages is selected according to the argument value and recursively formatted.

After construction, a customFormat object can be set for a top-level argument, overriding the default formatting and parsing behavior for that argument. However, custom formatting can be achieved more simply by writing a typeless argument in the pattern string and supplying it with a preformatted string value.

When formatting,MessageFormat takes a collection of argument values and writes an output string. The argument values may be passed as an array (when the pattern contains only numbered arguments) or as an array of names and and an array of arguments (which works for both named and numbered arguments).

Each argument is matched with one of the input values by array index or argument name and formatted according to its pattern specification (or using a customFormat object if one was set). A numbered pattern argument is matched with an argument name that contains that number as an ASCII-decimal-digit string (without leading zero).

Patterns and Their Interpretation

MessageFormat uses patterns of the following form:

message = messageText (argument messageText)*argument = noneArg | simpleArg | complexArgcomplexArg = choiceArg | pluralArg | selectArg | selectordinalArgnoneArg = '{' argNameOrNumber '}'simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}'pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}'selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}'choiceStyle: seeChoiceFormatpluralStyle: seePluralFormatselectStyle: seeSelectFormatargNameOrNumber = argName | argNumberargName = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+argNumber = '0' | ('1'..'9' ('0'..'9')*)argType = "number" | "date" | "time" | "spellout" | "ordinal" | "duration"argStyle = "short" | "medium" | "long" | "full" | "integer" | "currency" | "percent" | argStyleText | "::" argSkeletonText

Recommendation: Use the real apostrophe (single quote) character ’ (U+2019) for human-readable text, and use the ASCII apostrophe ' (U+0027) only in program syntax, like quoting inMessageFormat. See the annotations for U+0027 Apostrophe in The Unicode Standard.

Thechoice argument type is deprecated. Useplural arguments for proper plural selection, andselect arguments for simple selection among a fixed set of choices.

TheargType andargStyle values are used to create aFormat instance for the format element. The following table shows how the values map toFormat instances. Combinations not shown in the table are illegal. AnyargStyleText must be a valid pattern string for theFormat subclass used.

argTypeargStyleresultingFormat object
(none)null
number(none)NumberFormat.createInstance(getLocale(), status)
integerNumberFormat.createInstance(getLocale(), kNumberStyle, status)
currencyNumberFormat.createCurrencyInstance(getLocale(), status)
percentNumberFormat.createPercentInstance(getLocale(), status)
argStyleTextnewDecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status)
argSkeletonTextNumberFormatter::forSkeleton(argSkeletonText, status).locale(getLocale()).toFormat(status)
date(none)DateFormat.createDateInstance(kDefault, getLocale(), status)
shortDateFormat.createDateInstance(kShort, getLocale(), status)
mediumDateFormat.createDateInstance(kDefault, getLocale(), status)
longDateFormat.createDateInstance(kLong, getLocale(), status)
fullDateFormat.createDateInstance(kFull, getLocale(), status)
argStyleTextnew SimpleDateFormat(argStyleText, getLocale(), status)
argSkeletonTextDateFormat::createInstanceForSkeleton(argSkeletonText, getLocale(), status)
time(none)DateFormat.createTimeInstance(kDefault, getLocale(), status)
shortDateFormat.createTimeInstance(kShort, getLocale(), status)
mediumDateFormat.createTimeInstance(kDefault, getLocale(), status)
longDateFormat.createTimeInstance(kLong, getLocale(), status)
fullDateFormat.createTimeInstance(kFull, getLocale(), status)
argStyleTextnew SimpleDateFormat(argStyleText, getLocale(), status)
spelloutargStyleText (optional)new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status)
    .setDefaultRuleset(argStyleText, status);
ordinalargStyleText (optional)new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status)
    .setDefaultRuleset(argStyleText, status);
durationargStyleText (optional)new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status)
    .setDefaultRuleset(argStyleText, status);

Argument formatting

Arguments are formatted according to their type, using the default ICU formatters for those types, unless otherwise specified.

There are also several ways to control the formatting.

We recommend you use default styles, predefined style values, skeletons, or preformatted values, but not pattern strings or custom format objects.

For more details, see theICU User Guide.

Usage Information

Here are some examples of usage: Example 1:

UErrorCode success =U_ZERO_ERROR;
GregorianCalendar cal(success);
Formattable arguments[] = {
7L,
Formattable( (Date) cal.getTime(success), Formattable::kIsDate),
"a disturbance in the Force"
};
UnicodeString result;
MessageFormat::format(
"At {1,time,::jmm} on {1,date,::dMMMM}, there was {2} on planet {0,number}.",
arguments, 3, result, success );
cout <<"result: " << result << endl;
//<output>: At 4:34 PM on March 23, there was a disturbance
// in the Force on planet 7.
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition:utypes.h:509
@ U_ZERO_ERROR
No error, no warning.
Definition:utypes.h:544

Typically, the message format will come from resources, and the arguments will be dynamically set at runtime.

Example 2:

success =U_ZERO_ERROR;
Formattable testArgs[] = {3L,"MyDisk"};
MessageFormat form(
"The disk \"{1}\" contains {0} file(s).", success );
UnicodeString string;
FieldPosition fpos = 0;
cout <<"format: " << form.format(testArgs, 2,string, fpos, success ) << endl;
// output, with different testArgs:
// output: The disk "MyDisk" contains 0 file(s).
// output: The disk "MyDisk" contains 1 file(s).
// output: The disk "MyDisk" contains 1,273 file(s).
U_I18N_API MessageFormat(const UnicodeString &pattern, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and the default locale.

For messages that include plural forms, you can use a plural argument:

success =U_ZERO_ERROR;
MessageFormat msgFmt(
"{num_files, plural, "
"=0{There are no files on disk \"{disk_name}\".}"
"=1{There is one file on disk \"{disk_name}\".}"
"other{There are # files on disk \"{disk_name}\".}}",
Locale("en"),
success);
FieldPosition fpos = 0;
Formattable testArgs[] = {0L,"MyDisk"};
UnicodeString testArgsNames[] = {"num_files","disk_name"};
UnicodeString result;
cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
testArgs[0] = 3L;
cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
output:There are no files on disk "MyDisk".There are 3 files on "MyDisk".

SeePluralFormat andPluralRules for details.

Synchronization

MessageFormats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Stable:
ICU 2.0

Definition at line345 of filemsgfmt.h.

Member Enumeration Documentation

◆ EFormatNumber

enumicu::MessageFormat::EFormatNumber

Enum type for kMaxFormat.

Obsolete:
ICU 3.0. The 10-argument limit was removed as of ICU 2.6, rendering this enum type obsolete.
Enumerator
kMaxFormat 

The maximum number of arguments.

Obsolete:
ICU 3.0. The 10-argument limit was removed as of ICU 2.6, rendering this constant obsolete.

Definition at line353 of filemsgfmt.h.

Constructor & Destructor Documentation

◆ MessageFormat()[1/4]

U_I18N_API icu::MessageFormat::MessageFormat(constUnicodeStringpattern,
UErrorCodestatus 
)

Constructs a newMessageFormat using the given pattern and the default locale.

Parameters
patternPattern used to construct object.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Stable:
ICU 2.0

◆ MessageFormat()[2/4]

U_I18N_API icu::MessageFormat::MessageFormat(constUnicodeStringpattern,
constLocalenewLocale,
UErrorCodestatus 
)

Constructs a newMessageFormat using the given pattern and locale.

Parameters
patternPattern used to construct object.
newLocaleThe locale to use for formatting dates and numbers.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Stable:
ICU 2.0

◆ MessageFormat()[3/4]

U_I18N_API icu::MessageFormat::MessageFormat(constUnicodeStringpattern,
constLocalenewLocale,
UParseErrorparseError,
UErrorCodestatus 
)

Constructs a newMessageFormat using the given pattern and locale.

Parameters
patternPattern used to construct object.
newLocaleThe locale to use for formatting dates and numbers.
parseErrorStruct to receive information on the position of an error within the pattern.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Stable:
ICU 2.0

◆ MessageFormat()[4/4]

U_I18N_API icu::MessageFormat::MessageFormat(constMessageFormat)

Constructs a newMessageFormat from an existing one.

Stable:
ICU 2.0

◆ ~MessageFormat()

virtualU_I18N_API icu::MessageFormat::~MessageFormat()
virtual

Destructor.

Stable:
ICU 2.0

Member Function Documentation

◆ adoptFormat()[1/2]

virtualU_I18N_API void icu::MessageFormat::adoptFormat(constUnicodeStringformatName,
FormatformatToAdopt,
UErrorCodestatus 
)
virtual

Sets one subformat for given format name.

See the class description about format name. This function supports both named and numbered arguments– if numbered, the formatName is the corresponding UnicodeStrings (e.g. "0", "1", "2"...). If there is no matched formatName or wrong type, the item will be ignored. The caller should not delete theFormat object after this call.

Parameters
formatNameName of the subformat.
formatToAdoptFormat to be adopted.
statusoutput param set to success/failure code.
Stable:
ICU 4.0

◆ adoptFormat()[2/2]

virtualU_I18N_API void icu::MessageFormat::adoptFormat(int32_t formatNumber,
FormatformatToAdopt 
)
virtual

Sets one subformat.

See the class description about format numbering. The caller should not delete theFormat object after this call. If the number is over the number of formats already set, the item will be deleted and ignored.

If this format uses named arguments, the new format is discarded and this format remains unchanged.

Stable:
ICU 2.0
Parameters
formatNumberindex of the subformat.
formatToAdoptthe format to be adopted.

◆ adoptFormats()

virtualU_I18N_API void icu::MessageFormat::adoptFormats(Format ** formatsToAdopt,
int32_t count 
)
virtual

Sets subformats.

See the class description about format numbering. The caller should not delete theFormat objects after this call.The array formatsToAdopt is not itself adopted. Its ownership is retained by the caller. If the call fails because memory cannot be allocated, then the formats will be deleted by this method, and this object will remain unchanged.

If this format uses named arguments, the new formats are discarded and this format remains unchanged.

Stable:
ICU 2.0
Parameters
formatsToAdoptthe format to be adopted.
countthe size of the array.

◆ applyPattern()[1/3]

virtualU_I18N_API void icu::MessageFormat::applyPattern(constUnicodeStringpattern,
UErrorCodestatus 
)
virtual

Applies the given pattern string to this message format.

Parameters
patternThe pattern to be applied.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Stable:
ICU 2.0

◆ applyPattern()[2/3]

virtualU_I18N_API void icu::MessageFormat::applyPattern(constUnicodeStringpattern,
UMessagePatternApostropheMode aposMode,
UParseErrorparseError,
UErrorCodestatus 
)
virtual

Sets the UMessagePatternApostropheMode and the pattern used by this message format.

Parses the pattern and cachesFormat objects for simple argument types. Patterns and their interpretation are specified in theclass description.

This method is best used only once on a given object to avoid confusion about the mode, and after constructing the object with an empty pattern string to minimize overhead.

Parameters
patternThe pattern to be applied.
aposModeThe new apostrophe mode.
parseErrorStruct to receive information on the position of an error within the pattern. Can be nullptr.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Stable:
ICU 4.8

◆ applyPattern()[3/3]

virtualU_I18N_API void icu::MessageFormat::applyPattern(constUnicodeStringpattern,
UParseErrorparseError,
UErrorCodestatus 
)
virtual

Applies the given pattern string to this message format.

Parameters
patternThe pattern to be applied.
parseErrorStruct to receive information on the position of an error within the pattern.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Stable:
ICU 2.0

◆ autoQuoteApostrophe()

staticU_I18N_APIUnicodeString icu::MessageFormat::autoQuoteApostrophe(constUnicodeStringpattern,
UErrorCodestatus 
)
static

Convert an 'apostrophe-friendly' pattern into a standard pattern.

Standard patterns treat all apostrophes as quotes, which is problematic in some languages, e.g. French, where apostrophe is commonly used. This utility assumes that only an unpaired apostrophe immediately before a brace is a true quote. Other unpaired apostrophes are paired, and the resulting standard pattern string is returned.

Note it is not guaranteed that the returned pattern is indeed a valid pattern. The only effect is to convert between patterns having different quoting semantics.

Parameters
patternthe 'apostrophe-friendly' patttern to convert
statusInput/output error code. If the pattern cannot be parsed, the failure code is set.
Returns
the standard equivalent of the original pattern
Stable:
ICU 3.4

◆ clone()

virtualU_I18N_APIMessageFormat* icu::MessageFormat::clone() const
overridevirtual

Clones thisFormat object polymorphically.

The caller owns the result and should delete it when done.

Stable:
ICU 2.0

Implementsicu::Format.

◆ equalFormats()

staticU_I18N_APIUBool icu::MessageFormat::equalFormats(const void * left,
const void * right 
)
static

Compares twoFormat objects.

This is used for constructing the hash tables.

Parameters
leftpointer to aFormat object. Must not be nullptr.
rightpointer to aFormat object. Must not be nullptr.
Returns
whether the two objects are the same
Internal:
Do not use. This API is for internal use only.

◆ format()[1/7]

virtualU_I18N_APIUnicodeString& icu::MessageFormat::format(constFormattableobj,
UnicodeStringappendTo,
FieldPositionpos,
UErrorCodestatus 
) const
overridevirtual

Formats the given array of arguments into a user-readable string.

The array must be stored within a singleFormattable object of type kArray. If theFormattable object type is not of type kArray, then returns a failing UErrorCode.

If this format uses named arguments, appendTo is unchanged and status is set to U_ILLEGAL_ARGUMENT_ERROR.

Parameters
objAFormattable of type kArray containing arguments to be formatted.
appendToOutput parameter to receive result. Result is appended to existing contents.
posOn input: an alignment field, if desired. On output: the offsets of the alignment field.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

Implementsicu::Format.

◆ format()[2/7]

virtualU_I18N_APIUnicodeString& icu::Format::format

Format an object to produce a string.

This is a pure virtual method which subclasses must implement. This method allows polymorphic formatting ofFormattable objects. If a subclass ofFormat receives aFormattable object type it doesn't handle (e.g., if a numericFormattable is passed to aDateFormat object) then it returns a failing UErrorCode.

Parameters
objThe object to format.
appendToOutput parameter to receive result. Result is appended to existing contents.
posOn input: an alignment field, if desired. On output: the offsets of the alignment field.
statusOutput param filled with success/failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ format()[3/7]

virtualU_I18N_APIUnicodeString& icu::Format::format

Format an object to produce a string.

Subclasses should override this method. This method allows polymorphic formatting ofFormattable objects. If a subclass ofFormat receives aFormattable object type it doesn't handle (e.g., if a numericFormattable is passed to aDateFormat object) then it returns a failing UErrorCode.

Parameters
objThe object to format.
appendToOutput parameter to receive result. Result is appended to existing contents.
posIterOn return, can be used to iterate over positions of fields generated by this format call.
statusOutput param filled with success/failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 4.4

◆ format()[4/7]

U_I18N_APIUnicodeString& icu::Format::format

Formats an object to produce a string.

Parameters
objThe object to format.
appendToOutput parameter to receive result. Result is appended to existing contents.
statusOutput parameter filled in with success or failure status.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ format()[5/7]

U_I18N_APIUnicodeString& icu::MessageFormat::format(constFormattablesource,
int32_t count,
UnicodeStringappendTo,
FieldPositionignore,
UErrorCodestatus 
) const

Formats the given array of arguments into a user-readable string.

Does not take ownership of the Formattable* array or its contents.

If this format uses named arguments, appendTo is unchanged and status is set to U_ILLEGAL_ARGUMENT_ERROR.

Parameters
sourceAn array of objects to be formatted.
countThe number of elements of 'source'.
appendToOutput parameter to receive result. Result is appended to existing contents.
ignoreNot used; inherited from base class API.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ format()[6/7]

staticU_I18N_APIUnicodeString& icu::MessageFormat::format(constUnicodeStringpattern,
constFormattablearguments,
int32_t count,
UnicodeStringappendTo,
UErrorCodestatus 
)
static

Formats the given array of arguments into a user-readable string using the given pattern.

If this format uses named arguments, appendTo is unchanged and status is set to U_ILLEGAL_ARGUMENT_ERROR.

Parameters
patternThe pattern.
argumentsAn array of objects to be formatted.
countThe number of elements of 'source'.
appendToOutput parameter to receive result. Result is appended to existing contents.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ format()[7/7]

U_I18N_APIUnicodeString& icu::MessageFormat::format(constUnicodeStringargumentNames,
constFormattablearguments,
int32_t count,
UnicodeStringappendTo,
UErrorCodestatus 
) const

Formats the given array of arguments into a user-defined argument name array.

This function supports both named and numbered arguments– if numbered, the formatName is the corresponding UnicodeStrings (e.g. "0", "1", "2"...).

Parameters
argumentNamesargument name array
argumentsAn array of objects to be formatted.
countThe number of elements of 'argumentNames' and arguments. The number of argumentNames and arguments must be the same.
appendToOutput parameter to receive result. Result is appended to existing contents.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 4.0

◆ getApostropheMode()

U_I18N_APIUMessagePatternApostropheMode icu::MessageFormat::getApostropheMode() const
inline
Returns
this instance's UMessagePatternApostropheMode.
Stable:
ICU 4.8

Definition at line496 of filemsgfmt.h.

◆ getArgTypeCount()

U_I18N_API int32_t icu::MessageFormat::getArgTypeCount() const

This API is for ICU internal use only.

Please do not use it.

Returns argument types count in the parsed pattern. Used to distinguish pattern "{0} d" and "d".

Returns
The number of formattable types in the pattern
Internal:
Do not use. This API is for internal use only.

◆ getDynamicClassID()

virtualU_I18N_APIUClassID icu::MessageFormat::getDynamicClassID() const
overridevirtual

Returns a unique class ID POLYMORPHICALLY.

Pure virtual override. This method is to implement a simple version of RTTI, since not all C++ compilers support genuine RTTI. Polymorphicoperator==() andclone() methods call this method.

Returns
The class ID for this object. All objects of a given class have the same class ID. Objects of other classes have different class IDs.
Stable:
ICU 2.0

Reimplemented fromicu::UObject.

◆ getFormat()

virtualU_I18N_APIFormat* icu::MessageFormat::getFormat(constUnicodeStringformatName,
UErrorCodestatus 
)
virtual

Gets subformat pointer for given format name.

This function supports both named and numbered arguments. If numbered, the formatName is the corresponding UnicodeStrings (e.g. "0", "1", "2"...). The returnedFormat object should not be deleted by the caller, nor should the pointer of other object . The pointer and its contents remain valid only until the next call to any method of this class is made with this object.

Parameters
formatNamethe name or number specifying a format
statusoutput param set to success/failure code.
Stable:
ICU 4.0

◆ getFormatNames()

virtualU_I18N_APIStringEnumeration* icu::MessageFormat::getFormatNames(UErrorCodestatus)
virtual

Gets format names.

This function returns formatNames in StringEnumerations which can be used withgetFormat() andsetFormat() to export formattable array from currentMessageFormat to another. It is the caller's responsibility to delete the returned formatNames.

Parameters
statusoutput param set to success/failure code.
Stable:
ICU 4.0

◆ getFormats()

virtualU_I18N_API constFormat** icu::MessageFormat::getFormats(int32_t & count) const
virtual

Gets an array of subformats of this object.

The returned array should not be deleted by the caller, nor should the pointers within the array. The array and its contents remain valid only until the next call to this format. See the class description about format numbering.

Parameters
countoutput parameter to receive the size of the array
Returns
an array of count Format* objects, or nullptr if out of memory. Any or all of the array elements may be nullptr.
Stable:
ICU 2.0

◆ getLocale()

virtualU_I18N_API constLocale& icu::MessageFormat::getLocale() const
virtual

Gets the locale used for creating argumentFormat objects.

format information.

Returns
the locale of the object.
Stable:
ICU 2.0

◆ getStaticClassID()

staticU_I18N_APIUClassID icu::MessageFormat::getStaticClassID()
static

Return the class ID for this class.

This is useful only for comparing to a return value fromgetDynamicClassID(). For example:

.   Base* polymorphic_pointer = createPolymorphicObject();.   if (polymorphic_pointer->getDynamicClassID() ==.      Derived::getStaticClassID()) ...
Returns
The class ID for all objects of this class.
Stable:
ICU 2.0

◆ operator=()

U_I18N_API constMessageFormat& icu::MessageFormat::operator=(constMessageFormat)

Assignment operator.

Stable:
ICU 2.0

◆ operator==()

virtualU_I18N_API bool icu::MessageFormat::operator==(constFormatother) const
overridevirtual

Returns true if the givenFormat objects are semantically equal.

Objects of different subclasses are considered unequal.

Parameters
otherthe object to be compared with.
Returns
true if the givenFormat objects are semantically equal.
Stable:
ICU 2.0

Implementsicu::Format.

◆ parse()[1/2]

virtualU_I18N_APIFormattable* icu::MessageFormat::parse(constUnicodeStringsource,
int32_t & count,
UErrorCodestatus 
) const
virtual

Parses the given string into an array of output arguments.

If this format uses named arguments, status is set to U_ARGUMENT_TYPE_MISMATCH.

Parameters
sourceString to be parsed.
countOutput param to receive size of returned array.
statusInput/output error code. If the pattern cannot be parsed, set to failure code.
Returns
an array of parsed arguments. The caller owns both the array and its contents. Returns nullptr if status is not U_ZERO_ERROR.
Stable:
ICU 2.0

◆ parse()[2/2]

virtualU_I18N_APIFormattable* icu::MessageFormat::parse(constUnicodeStringsource,
ParsePositionpos,
int32_t & count 
) const
virtual

Parses the given string into an array of output arguments.

Parameters
sourceString to be parsed.
posOn input, starting position for parse. On output, final position after parse. Unchanged if parse fails.
countOutput parameter to receive the number of arguments parsed.
Returns
an array of parsed arguments. The caller owns both the array and its contents.
Stable:
ICU 2.0

◆ parseObject()

virtualU_I18N_API void icu::MessageFormat::parseObject(constUnicodeStringsource,
Formattableresult,
ParsePositionpos 
) const
overridevirtual

Parses the given string into an array of output arguments stored within a singleFormattable of type kArray.

Parameters
sourceThe string to be parsed into an object.
resultFormattable to be set to the parse result. If parse fails, return contents are undefined.
posOn input, starting position for parse. On output, final position after parse. Unchanged if parse fails.
Stable:
ICU 2.0

Implementsicu::Format.

◆ setFormat()[1/2]

virtualU_I18N_API void icu::MessageFormat::setFormat(constUnicodeStringformatName,
constFormatformat,
UErrorCodestatus 
)
virtual

Sets one subformat for given format name.

See the class description about format name. This function supports both named and numbered arguments– if numbered, the formatName is the corresponding UnicodeStrings (e.g. "0", "1", "2"...). If there is no matched formatName or wrong type, the item will be ignored.

Parameters
formatNameName of the subformat.
formatthe format to be set.
statusoutput param set to success/failure code.
Stable:
ICU 4.0

◆ setFormat()[2/2]

virtualU_I18N_API void icu::MessageFormat::setFormat(int32_t formatNumber,
constFormatformat 
)
virtual

Sets one subformat.

See the class description about format numbering. If the number is over the number of formats already set, the item will be ignored.

Parameters
formatNumberindex of the subformat.
formatthe format to be set.
Stable:
ICU 2.0

◆ setFormats()

virtualU_I18N_API void icu::MessageFormat::setFormats(constFormat ** newFormats,
int32_t cnt 
)
virtual

Sets subformats.

See the class description about format numbering. Each item in the array is cloned into the internal array. If the call fails because memory cannot be allocated, then this object will remain unchanged.

If this format uses named arguments, the new formats are discarded and this format remains unchanged.

Stable:
ICU 2.0
Parameters
newFormatsthe new format to be set.
cntthe size of the array.

◆ setLocale()

virtualU_I18N_API void icu::MessageFormat::setLocale(constLocaletheLocale)
virtual

Sets the locale to be used for creating argumentFormat objects.

Parameters
theLocalethe new locale value to be set.
Stable:
ICU 2.0

◆ toPattern()

virtualU_I18N_APIUnicodeString& icu::MessageFormat::toPattern(UnicodeStringappendTo) const
virtual

Returns a pattern that can be used to recreate this object.

Parameters
appendToOutput parameter to receive the pattern. Result is appended to existing contents.
Returns
Reference to 'appendTo' parameter.
Stable:
ICU 2.0

◆ usesNamedArguments()

U_I18N_APIUBool icu::MessageFormat::usesNamedArguments() const

Returns true if thisMessageFormat uses named arguments, and false otherwise.

See class description.

Returns
true if named arguments are used.
Stable:
ICU 4.0

The documentation for this class was generated from the following file:

Generated by doxygen 1.9.1
[8]ページ先頭

©2009-2025 Movatter.jp