2 *******************************************************************************
3 * Copyright (C) 1996-2004, International Business Machines Corporation and others. All Rights Reserved.
4 *******************************************************************************
8 * tab size: 8 (not used)
13 * 08/5/2001 Ram Added C wrappers for C++ API.
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_FORMATTING
25 #include "unicode/uloc.h"
26 #include "unicode/parseerr.h"
30 * \brief C API: MessageFormat
32 * <h2>Message Format C API </h2>
34 * Provides means to produce concatenated messages in language-neutral way.
35 * Use this for all concatenations that show up to end users.
37 * Takes a set of objects, formats them, then inserts the formatted
38 * strings into the pattern at the appropriate places.
40 * Here are some examples of usage:
44 * UChar *result, *tzID, *str;
46 * int32_t resultLengthOut, resultlength;
50 * UErrorCode status = U_ZERO_ERROR;
52 * str=(UChar*)malloc(sizeof(UChar) * (strlen("disturbance in force") +1));
53 * u_uastrcpy(str, "disturbance in force");
54 * tzID=(UChar*)malloc(sizeof(UChar) * 4);
55 * u_uastrcpy(tzID, "PST");
56 * cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
57 * ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
58 * d1=ucal_getMillis(cal, &status);
59 * u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
61 * resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, d1, str, 7);
62 * if(status==U_BUFFER_OVERFLOW_ERROR){
63 * status=U_ZERO_ERROR;
64 * resultlength=resultLengthOut+1;
65 * result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
66 * u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
68 * printf("%s\n", austrdup(result) );//austrdup( a function used to convert UChar* to char*)
69 * //output>: "On March 18, 1999, there was a disturbance in force on planet 7
72 * Typically, the message format will come from resources, and the
73 * arguments will be dynamically set at runtime.
79 * UErrorCode status = U_ZERO_ERROR;
82 * int32_t resultlength, resultLengthOut, i;
83 * double testArgs= { 100.0, 1.0, 0.0};
85 * str=(UChar*)malloc(sizeof(UChar) * 10);
86 * u_uastrcpy(str, "MyDisk");
87 * u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
90 * resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, testArgs[i], str);
91 * if(status==U_BUFFER_OVERFLOW_ERROR){
92 * status=U_ZERO_ERROR;
93 * resultlength=resultLengthOut+1;
94 * result=(UChar*)malloc(sizeof(UChar) * resultlength);
95 * u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, testArgs[i], str);
97 * printf("%s\n", austrdup(result) ); //austrdup( a function used to convert UChar* to char*)
100 * // output, with different testArgs:
101 * // output: The disk "MyDisk" contains 100 files.
102 * // output: The disk "MyDisk" contains one file.
103 * // output: The disk "MyDisk" contains no files.
107 * The pattern is of the following form. Legend:
111 * (group that may be repeated)*
114 * Do not confuse optional items with items inside quotes braces, such
115 * as this: "{". Quoted braces are literals.
118 * messageFormatPattern := string ( "{" messageFormatElement "}" string )*
120 * messageFormatElement := argument { "," elementFormat }
122 * elementFormat := "time" { "," datetimeStyle }
123 * | "date" { "," datetimeStyle }
124 * | "number" { "," numberStyle }
125 * | "choice" "," choiceStyle
127 * datetimeStyle := "short"
131 * | dateFormatPattern
133 * numberStyle := "currency"
136 * | numberFormatPattern
138 * choiceStyle := choiceFormatPattern
141 * If there is no elementFormat, then the argument must be a string,
142 * which is substituted. If there is no dateTimeStyle or numberStyle,
143 * then the default format is used (e.g. NumberFormat.getInstance(),
144 * DateFormat.getDefaultTime() or DateFormat.getDefaultDate(). For
145 * a ChoiceFormat, the pattern must always be specified, since there
148 * In strings, single quotes can be used to quote the "{" sign if
149 * necessary. A real single quote is represented by ''. Inside a
150 * messageFormatElement, quotes are [not] removed. For example,
151 * {1,number,$'#',##} will produce a number format with the pound-sign
152 * quoted, with a result such as: "$#31,45".
154 * If a pattern is used, then unquoted braces in the pattern, if any,
155 * must match: that is, "ab {0} de" and "ab '}' de" are ok, but "ab
156 * {0'}' de" and "ab } de" are not.
158 * The argument is a number from 0 to 9, which corresponds to the
159 * arguments presented in an array to be formatted.
161 * It is ok to have unused arguments in the array. With missing
162 * arguments or arguments that are not of the right class for the
163 * specified format, a failing UErrorCode result is set.
167 * [Note:] As we see above, the string produced by a choice Format in
168 * MessageFormat is treated specially; occurances of '{' are used to
169 * indicated subformats.
171 * [Note:] Formats are numbered by order of variable in the string.
172 * This is [not] the same as the argument numbering!
175 * For example: with "abc{2}def{3}ghi{0}...",
177 * format0 affects the first variable {2}
178 * format1 affects the second variable {3}
179 * format2 affects the second variable {0}
186 * Format a message for a locale.
187 * This function may perform re-ordering of the arguments depending on the
188 * locale. For all numeric arguments, double is assumed unless the type is
189 * explicitly integer. All choice format arguments must be of type double.
190 * @param locale The locale for which the message will be formatted
191 * @param pattern The pattern specifying the message's format
192 * @param patternLength The length of pattern
193 * @param result A pointer to a buffer to receive the formatted message.
194 * @param resultLength The maximum size of result.
195 * @param status A pointer to an UErrorCode to receive any errors
196 * @param ... A variable-length argument list containing the arguments specified
198 * @return The total buffer size needed; if greater than resultLength, the
199 * output was truncated.
200 * @see u_parseMessage
203 U_STABLE
int32_t U_EXPORT2
204 u_formatMessage(const char *locale
,
205 const UChar
*pattern
,
206 int32_t patternLength
,
208 int32_t resultLength
,
213 * Format a message for a locale.
214 * This function may perform re-ordering of the arguments depending on the
215 * locale. For all numeric arguments, double is assumed unless the type is
216 * explicitly integer. All choice format arguments must be of type double.
217 * @param locale The locale for which the message will be formatted
218 * @param pattern The pattern specifying the message's format
219 * @param patternLength The length of pattern
220 * @param result A pointer to a buffer to receive the formatted message.
221 * @param resultLength The maximum size of result.
222 * @param ap A variable-length argument list containing the arguments specified
223 * @param status A pointer to an UErrorCode to receive any errors
225 * @return The total buffer size needed; if greater than resultLength, the
226 * output was truncated.
227 * @see u_parseMessage
230 U_STABLE
int32_t U_EXPORT2
231 u_vformatMessage( const char *locale
,
232 const UChar
*pattern
,
233 int32_t patternLength
,
235 int32_t resultLength
,
241 * For numeric arguments, this function will always use doubles. Integer types
242 * should not be passed.
243 * This function is not able to parse all output from {@link #u_formatMessage }.
244 * @param locale The locale for which the message is formatted
245 * @param pattern The pattern specifying the message's format
246 * @param patternLength The length of pattern
247 * @param source The text to parse.
248 * @param sourceLength The length of source, or -1 if null-terminated.
249 * @param status A pointer to an UErrorCode to receive any errors
250 * @param ... A variable-length argument list containing the arguments
251 * specified in pattern.
252 * @see u_formatMessage
255 U_STABLE
void U_EXPORT2
256 u_parseMessage( const char *locale
,
257 const UChar
*pattern
,
258 int32_t patternLength
,
260 int32_t sourceLength
,
266 * For numeric arguments, this function will always use doubles. Integer types
267 * should not be passed.
268 * This function is not able to parse all output from {@link #u_formatMessage }.
269 * @param locale The locale for which the message is formatted
270 * @param pattern The pattern specifying the message's format
271 * @param patternLength The length of pattern
272 * @param source The text to parse.
273 * @param sourceLength The length of source, or -1 if null-terminated.
274 * @param ap A variable-length argument list containing the arguments
275 * @param status A pointer to an UErrorCode to receive any errors
276 * specified in pattern.
277 * @see u_formatMessage
280 U_STABLE
void U_EXPORT2
281 u_vparseMessage(const char *locale
,
282 const UChar
*pattern
,
283 int32_t patternLength
,
285 int32_t sourceLength
,
290 * Format a message for a locale.
291 * This function may perform re-ordering of the arguments depending on the
292 * locale. For all numeric arguments, double is assumed unless the type is
293 * explicitly integer. All choice format arguments must be of type double.
294 * @param locale The locale for which the message will be formatted
295 * @param pattern The pattern specifying the message's format
296 * @param patternLength The length of pattern
297 * @param result A pointer to a buffer to receive the formatted message.
298 * @param resultLength The maximum size of result.
299 * @param status A pointer to an UErrorCode to receive any errors
300 * @param ... A variable-length argument list containing the arguments specified
302 * @param parseError A pointer to UParseError to receive information about errors
303 * occurred during parsing.
304 * @return The total buffer size needed; if greater than resultLength, the
305 * output was truncated.
306 * @see u_parseMessage
309 U_STABLE
int32_t U_EXPORT2
310 u_formatMessageWithError( const char *locale
,
311 const UChar
*pattern
,
312 int32_t patternLength
,
314 int32_t resultLength
,
315 UParseError
*parseError
,
320 * Format a message for a locale.
321 * This function may perform re-ordering of the arguments depending on the
322 * locale. For all numeric arguments, double is assumed unless the type is
323 * explicitly integer. All choice format arguments must be of type double.
324 * @param locale The locale for which the message will be formatted
325 * @param pattern The pattern specifying the message's format
326 * @param patternLength The length of pattern
327 * @param result A pointer to a buffer to receive the formatted message.
328 * @param resultLength The maximum size of result.
329 * @param parseError A pointer to UParseError to receive information about errors
330 * occurred during parsing.
331 * @param ap A variable-length argument list containing the arguments specified
332 * @param status A pointer to an UErrorCode to receive any errors
334 * @return The total buffer size needed; if greater than resultLength, the
335 * output was truncated.
338 U_STABLE
int32_t U_EXPORT2
339 u_vformatMessageWithError( const char *locale
,
340 const UChar
*pattern
,
341 int32_t patternLength
,
343 int32_t resultLength
,
344 UParseError
* parseError
,
350 * For numeric arguments, this function will always use doubles. Integer types
351 * should not be passed.
352 * This function is not able to parse all output from {@link #u_formatMessage }.
353 * @param locale The locale for which the message is formatted
354 * @param pattern The pattern specifying the message's format
355 * @param patternLength The length of pattern
356 * @param source The text to parse.
357 * @param sourceLength The length of source, or -1 if null-terminated.
358 * @param parseError A pointer to UParseError to receive information about errors
359 * occurred during parsing.
360 * @param status A pointer to an UErrorCode to receive any errors
361 * @param ... A variable-length argument list containing the arguments
362 * specified in pattern.
363 * @see u_formatMessage
366 U_STABLE
void U_EXPORT2
367 u_parseMessageWithError(const char *locale
,
368 const UChar
*pattern
,
369 int32_t patternLength
,
371 int32_t sourceLength
,
372 UParseError
*parseError
,
378 * For numeric arguments, this function will always use doubles. Integer types
379 * should not be passed.
380 * This function is not able to parse all output from {@link #u_formatMessage }.
381 * @param locale The locale for which the message is formatted
382 * @param pattern The pattern specifying the message's format
383 * @param patternLength The length of pattern
384 * @param source The text to parse.
385 * @param sourceLength The length of source, or -1 if null-terminated.
386 * @param ap A variable-length argument list containing the arguments
387 * @param parseError A pointer to UParseError to receive information about errors
388 * occurred during parsing.
389 * @param status A pointer to an UErrorCode to receive any errors
390 * specified in pattern.
391 * @see u_formatMessage
394 U_STABLE
void U_EXPORT2
395 u_vparseMessageWithError(const char *locale
,
396 const UChar
*pattern
,
397 int32_t patternLength
,
399 int32_t sourceLength
,
401 UParseError
*parseError
,
404 /*----------------------- New experimental API --------------------------- */
406 * The message format object
409 typedef void* UMessageFormat
;
413 * Open a message formatter with given pattern and for the given locale.
414 * @param pattern A pattern specifying the format to use.
415 * @param patternLength Length of the pattern to use
416 * @param locale The locale for which the messages are formatted.
417 * @param parseError A pointer to UParseError struct to receive any errors
418 * occured during parsing. Can be NULL.
419 * @param status A pointer to an UErrorCode to receive any errors.
420 * @return A pointer to a UMessageFormat to use for formatting
421 * messages, or 0 if an error occurred.
424 U_STABLE UMessageFormat
* U_EXPORT2
425 umsg_open( const UChar
*pattern
,
426 int32_t patternLength
,
428 UParseError
*parseError
,
432 * Close a UMessageFormat.
433 * Once closed, a UMessageFormat may no longer be used.
434 * @param format The formatter to close.
437 U_STABLE
void U_EXPORT2
438 umsg_close(UMessageFormat
* format
);
441 * Open a copy of a UMessageFormat.
442 * This function performs a deep copy.
443 * @param fmt The formatter to copy
444 * @param status A pointer to an UErrorCode to receive any errors.
445 * @return A pointer to a UDateFormat identical to fmt.
448 U_STABLE UMessageFormat U_EXPORT2
449 umsg_clone(const UMessageFormat
*fmt
,
453 * Sets the locale. This locale is used for fetching default number or date
454 * format information.
455 * @param fmt The formatter to set
456 * @param locale The locale the formatter should use.
459 U_STABLE
void U_EXPORT2
460 umsg_setLocale(UMessageFormat
*fmt
,
464 * Gets the locale. This locale is used for fetching default number or date
465 * format information.
466 * @param fmt The formatter to querry
467 * @return the locale.
470 U_STABLE
const char* U_EXPORT2
471 umsg_getLocale(const UMessageFormat
*fmt
);
475 * @param fmt The formatter to use
476 * @param pattern The pattern to be applied.
477 * @param patternLength Length of the pattern to use
478 * @param parseError Struct to receive information on position
479 * of error if an error is encountered.Can be NULL.
480 * @param status Output param set to success/failure code on
481 * exit. If the pattern is invalid, this will be
482 * set to a failure result.
485 U_STABLE
void U_EXPORT2
486 umsg_applyPattern( UMessageFormat
*fmt
,
487 const UChar
* pattern
,
488 int32_t patternLength
,
489 UParseError
* parseError
,
494 * @param fmt The formatter to use
495 * @param result A pointer to a buffer to receive the pattern.
496 * @param resultLength The maximum size of result.
497 * @param status Output param set to success/failure code on
498 * exit. If the pattern is invalid, this will be
499 * set to a failure result.
500 * @return the pattern of the format
503 U_STABLE
int32_t U_EXPORT2
504 umsg_toPattern(const UMessageFormat
*fmt
,
506 int32_t resultLength
,
510 * Format a message for a locale.
511 * This function may perform re-ordering of the arguments depending on the
512 * locale. For all numeric arguments, double is assumed unless the type is
513 * explicitly integer. All choice format arguments must be of type double.
514 * @param fmt The formatter to use
515 * @param result A pointer to a buffer to receive the formatted message.
516 * @param resultLength The maximum size of result.
517 * @param status A pointer to an UErrorCode to receive any errors
518 * @param ... A variable-length argument list containing the arguments
519 * specified in pattern.
520 * @return The total buffer size needed; if greater than resultLength,
521 * the output was truncated.
524 U_STABLE
int32_t U_EXPORT2
525 umsg_format( const UMessageFormat
*fmt
,
527 int32_t resultLength
,
532 * Format a message for a locale.
533 * This function may perform re-ordering of the arguments depending on the
534 * locale. For all numeric arguments, double is assumed unless the type is
535 * explicitly integer. All choice format arguments must be of type double.
536 * @param fmt The formatter to use
537 * @param result A pointer to a buffer to receive the formatted message.
538 * @param resultLength The maximum size of result.
539 * @param ap A variable-length argument list containing the arguments
540 * @param status A pointer to an UErrorCode to receive any errors
541 * specified in pattern.
542 * @return The total buffer size needed; if greater than resultLength,
543 * the output was truncated.
546 U_STABLE
int32_t U_EXPORT2
547 umsg_vformat( const UMessageFormat
*fmt
,
549 int32_t resultLength
,
555 * For numeric arguments, this function will always use doubles. Integer types
556 * should not be passed.
557 * This function is not able to parse all output from {@link #umsg_format }.
558 * @param fmt The formatter to use
559 * @param source The text to parse.
560 * @param sourceLength The length of source, or -1 if null-terminated.
561 * @param count Output param to receive number of elements returned.
562 * @param status A pointer to an UErrorCode to receive any errors
563 * @param ... A variable-length argument list containing the arguments
564 * specified in pattern.
567 U_STABLE
void U_EXPORT2
568 umsg_parse( const UMessageFormat
*fmt
,
570 int32_t sourceLength
,
577 * For numeric arguments, this function will always use doubles. Integer types
578 * should not be passed.
579 * This function is not able to parse all output from {@link #umsg_format }.
580 * @param fmt The formatter to use
581 * @param source The text to parse.
582 * @param sourceLength The length of source, or -1 if null-terminated.
583 * @param count Output param to receive number of elements returned.
584 * @param ap A variable-length argument list containing the arguments
585 * @param status A pointer to an UErrorCode to receive any errors
586 * specified in pattern.
587 * @see u_formatMessage
590 U_STABLE
void U_EXPORT2
591 umsg_vparse(const UMessageFormat
*fmt
,
593 int32_t sourceLength
,
600 * Get the locale for this message format object.
601 * You can choose between valid and actual locale.
602 * @param fmt The formatter to get the locale from
603 * @param type type of the locale we're looking for (valid or actual)
604 * @param status error code for the operation
605 * @return the locale name
606 * @draft ICU 2.8 likely to change in ICU 3.0, based on feedback
608 U_STABLE
const char* U_EXPORT2
609 umsg_getLocaleByType(const UMessageFormat
*fmt
,
610 ULocDataLocaleType type
,
613 #endif /* #if !UCONFIG_NO_FORMATTING */