]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/umsg.h
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / umsg.h
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
374ca955 3* Copyright (C) 1996-2004, International Business Machines Corporation and others. All Rights Reserved.
b75a7d8f
A
4*******************************************************************************
5*
6* file name: umsg.h
7* encoding: US-ASCII
8* tab size: 8 (not used)
9* indentation:4
10*
11* Change history:
12*
13* 08/5/2001 Ram Added C wrappers for C++ API.
14*
15*
16*/
17
18#ifndef UMSG_H
19#define UMSG_H
20
21#include "unicode/utypes.h"
22
23#if !UCONFIG_NO_FORMATTING
24
374ca955 25#include "unicode/uloc.h"
b75a7d8f
A
26#include "unicode/parseerr.h"
27#include <stdarg.h>
28/**
29 * \file
30 * \brief C API: MessageFormat
31 *
32 * <h2>Message Format C API </h2>
33 *
34 * Provides means to produce concatenated messages in language-neutral way.
35 * Use this for all concatenations that show up to end users.
36 * <P>
37 * Takes a set of objects, formats them, then inserts the formatted
38 * strings into the pattern at the appropriate places.
39 * <P>
40 * Here are some examples of usage:
41 * Example 1:
42 * <pre>
43 * \code
44 * UChar *result, *tzID, *str;
45 * UChar pattern[100];
46 * int32_t resultLengthOut, resultlength;
47 * UCalendar *cal;
48 * UDate d1;
49 * UDateFormat *def1;
50 * UErrorCode status = U_ZERO_ERROR;
51 *
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}");
60 * resultlength=0;
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);
67 * }
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
70 * \endcode
71 * </pre>
72 * Typically, the message format will come from resources, and the
73 * arguments will be dynamically set at runtime.
74 * <P>
75 * Example 2:
76 * <pre>
77 * \code
78 * UChar* str;
79 * UErrorCode status = U_ZERO_ERROR;
80 * UChar *result;
81 * UChar pattern[100];
82 * int32_t resultlength, resultLengthOut, i;
83 * double testArgs= { 100.0, 1.0, 0.0};
84 *
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}");
88 * for(i=0; i<3; i++){
89 * resultlength=0;
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);
96 * }
97 * printf("%s\n", austrdup(result) ); //austrdup( a function used to convert UChar* to char*)
98 * free(result);
99 * }
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.
104 * \endcode
105 * </pre>
106 *
107 * The pattern is of the following form. Legend:
108 * <pre>
109 * \code
110 * {optional item}
111 * (group that may be repeated)*
112 * \endcode
113 * </pre>
114 * Do not confuse optional items with items inside quotes braces, such
115 * as this: "{". Quoted braces are literals.
116 * <pre>
117 * \code
118 * messageFormatPattern := string ( "{" messageFormatElement "}" string )*
119 *
120 * messageFormatElement := argument { "," elementFormat }
121 *
122 * elementFormat := "time" { "," datetimeStyle }
123 * | "date" { "," datetimeStyle }
124 * | "number" { "," numberStyle }
125 * | "choice" "," choiceStyle
126 *
127 * datetimeStyle := "short"
128 * | "medium"
129 * | "long"
130 * | "full"
131 * | dateFormatPattern
132 *
133 * numberStyle := "currency"
134 * | "percent"
135 * | "integer"
136 * | numberFormatPattern
137 *
138 * choiceStyle := choiceFormatPattern
139 * \endcode
140 * </pre>
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
146 * is no default.
147 * <P>
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".
153 * <P>
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.
157 * <P>
158 * The argument is a number from 0 to 9, which corresponds to the
159 * arguments presented in an array to be formatted.
160 * <P>
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.
164 * <P>
165
166 * <P>
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.
170 * <P>
171 * [Note:] Formats are numbered by order of variable in the string.
172 * This is [not] the same as the argument numbering!
173 * <pre>
174 * \code
175 * For example: with "abc{2}def{3}ghi{0}...",
176 *
177 * format0 affects the first variable {2}
178 * format1 affects the second variable {3}
179 * format2 affects the second variable {0}
180 * \endcode
181 * </pre>
182 * and so on.
183 */
184
185/**
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
197 * in pattern.
198 * @return The total buffer size needed; if greater than resultLength, the
199 * output was truncated.
200 * @see u_parseMessage
201 * @stable ICU 2.0
202 */
374ca955 203U_STABLE int32_t U_EXPORT2
b75a7d8f
A
204u_formatMessage(const char *locale,
205 const UChar *pattern,
206 int32_t patternLength,
207 UChar *result,
208 int32_t resultLength,
209 UErrorCode *status,
210 ...);
211
212/**
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
224 * in pattern.
225 * @return The total buffer size needed; if greater than resultLength, the
226 * output was truncated.
227 * @see u_parseMessage
228 * @stable ICU 2.0
229 */
374ca955 230U_STABLE int32_t U_EXPORT2
b75a7d8f
A
231u_vformatMessage( const char *locale,
232 const UChar *pattern,
233 int32_t patternLength,
234 UChar *result,
235 int32_t resultLength,
236 va_list ap,
237 UErrorCode *status);
238
239/**
240 * Parse a message.
241 * For numeric arguments, this function will always use doubles. Integer types
242 * should not be passed.
374ca955 243 * This function is not able to parse all output from {@link #u_formatMessage }.
b75a7d8f
A
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
253 * @stable ICU 2.0
254 */
374ca955 255U_STABLE void U_EXPORT2
b75a7d8f
A
256u_parseMessage( const char *locale,
257 const UChar *pattern,
258 int32_t patternLength,
259 const UChar *source,
260 int32_t sourceLength,
261 UErrorCode *status,
262 ...);
263
264/**
265 * Parse a message.
266 * For numeric arguments, this function will always use doubles. Integer types
267 * should not be passed.
374ca955 268 * This function is not able to parse all output from {@link #u_formatMessage }.
b75a7d8f
A
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
278 * @stable ICU 2.0
279 */
374ca955 280U_STABLE void U_EXPORT2
b75a7d8f
A
281u_vparseMessage(const char *locale,
282 const UChar *pattern,
283 int32_t patternLength,
284 const UChar *source,
285 int32_t sourceLength,
286 va_list ap,
287 UErrorCode *status);
288
289/**
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
301 * in pattern.
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
307 * @stable ICU 2.0
308 */
374ca955 309U_STABLE int32_t U_EXPORT2
b75a7d8f
A
310u_formatMessageWithError( const char *locale,
311 const UChar *pattern,
312 int32_t patternLength,
313 UChar *result,
314 int32_t resultLength,
315 UParseError *parseError,
316 UErrorCode *status,
317 ...);
318
319/**
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
333 * in pattern.
334 * @return The total buffer size needed; if greater than resultLength, the
335 * output was truncated.
336 * @stable ICU 2.0
337 */
374ca955 338U_STABLE int32_t U_EXPORT2
b75a7d8f
A
339u_vformatMessageWithError( const char *locale,
340 const UChar *pattern,
341 int32_t patternLength,
342 UChar *result,
343 int32_t resultLength,
344 UParseError* parseError,
345 va_list ap,
346 UErrorCode *status);
347
348/**
349 * Parse a message.
350 * For numeric arguments, this function will always use doubles. Integer types
351 * should not be passed.
374ca955 352 * This function is not able to parse all output from {@link #u_formatMessage }.
b75a7d8f
A
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
364 * @stable ICU 2.0
365 */
374ca955 366U_STABLE void U_EXPORT2
b75a7d8f
A
367u_parseMessageWithError(const char *locale,
368 const UChar *pattern,
369 int32_t patternLength,
370 const UChar *source,
371 int32_t sourceLength,
372 UParseError *parseError,
373 UErrorCode *status,
374 ...);
375
376/**
377 * Parse a message.
378 * For numeric arguments, this function will always use doubles. Integer types
379 * should not be passed.
374ca955 380 * This function is not able to parse all output from {@link #u_formatMessage }.
b75a7d8f
A
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
392 * @stable ICU 2.0
393 */
374ca955 394U_STABLE void U_EXPORT2
b75a7d8f
A
395u_vparseMessageWithError(const char *locale,
396 const UChar *pattern,
397 int32_t patternLength,
398 const UChar *source,
399 int32_t sourceLength,
400 va_list ap,
401 UParseError *parseError,
402 UErrorCode* status);
403
404/*----------------------- New experimental API --------------------------- */
405/**
406 * The message format object
407 * @stable ICU 2.0
408 */
409typedef void* UMessageFormat;
410
411
412/**
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.
422 * @stable ICU 2.0
423 */
374ca955 424U_STABLE UMessageFormat* U_EXPORT2
b75a7d8f
A
425umsg_open( const UChar *pattern,
426 int32_t patternLength,
427 const char *locale,
428 UParseError *parseError,
429 UErrorCode *status);
430
431/**
432 * Close a UMessageFormat.
433 * Once closed, a UMessageFormat may no longer be used.
434 * @param format The formatter to close.
435 * @stable ICU 2.0
436 */
374ca955 437U_STABLE void U_EXPORT2
b75a7d8f
A
438umsg_close(UMessageFormat* format);
439
440/**
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.
446 * @stable ICU 2.0
447 */
374ca955 448U_STABLE UMessageFormat U_EXPORT2
b75a7d8f
A
449umsg_clone(const UMessageFormat *fmt,
450 UErrorCode *status);
451
452/**
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.
457 * @stable ICU 2.0
458 */
374ca955 459U_STABLE void U_EXPORT2
b75a7d8f
A
460umsg_setLocale(UMessageFormat *fmt,
461 const char* locale);
462
463/**
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.
468 * @stable ICU 2.0
469 */
374ca955
A
470U_STABLE const char* U_EXPORT2
471umsg_getLocale(const UMessageFormat *fmt);
b75a7d8f
A
472
473/**
474 * Sets the pattern.
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.
483 * @stable ICU 2.0
484 */
374ca955 485U_STABLE void U_EXPORT2
b75a7d8f
A
486umsg_applyPattern( UMessageFormat *fmt,
487 const UChar* pattern,
488 int32_t patternLength,
489 UParseError* parseError,
490 UErrorCode* status);
491
492/**
493 * Gets the pattern.
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
501 * @stable ICU 2.0
502 */
374ca955
A
503U_STABLE int32_t U_EXPORT2
504umsg_toPattern(const UMessageFormat *fmt,
b75a7d8f
A
505 UChar* result,
506 int32_t resultLength,
507 UErrorCode* status);
508
509/**
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.
522 * @stable ICU 2.0
523 */
374ca955
A
524U_STABLE int32_t U_EXPORT2
525umsg_format( const UMessageFormat *fmt,
b75a7d8f
A
526 UChar *result,
527 int32_t resultLength,
528 UErrorCode *status,
529 ...);
530
531/**
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.
544 * @stable ICU 2.0
545 */
374ca955
A
546U_STABLE int32_t U_EXPORT2
547umsg_vformat( const UMessageFormat *fmt,
b75a7d8f
A
548 UChar *result,
549 int32_t resultLength,
550 va_list ap,
551 UErrorCode *status);
552
553/**
554 * Parse a message.
555 * For numeric arguments, this function will always use doubles. Integer types
556 * should not be passed.
374ca955 557 * This function is not able to parse all output from {@link #umsg_format }.
b75a7d8f
A
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.
565 * @stable ICU 2.0
566 */
374ca955
A
567U_STABLE void U_EXPORT2
568umsg_parse( const UMessageFormat *fmt,
b75a7d8f
A
569 const UChar *source,
570 int32_t sourceLength,
571 int32_t *count,
572 UErrorCode *status,
573 ...);
574
575/**
576 * Parse a message.
577 * For numeric arguments, this function will always use doubles. Integer types
578 * should not be passed.
374ca955 579 * This function is not able to parse all output from {@link #umsg_format }.
b75a7d8f
A
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
588 * @stable ICU 2.0
589 */
374ca955
A
590U_STABLE void U_EXPORT2
591umsg_vparse(const UMessageFormat *fmt,
b75a7d8f
A
592 const UChar *source,
593 int32_t sourceLength,
594 int32_t *count,
595 va_list ap,
596 UErrorCode *status);
597
374ca955
A
598
599/**
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
607 */
608U_STABLE const char* U_EXPORT2
609umsg_getLocaleByType(const UMessageFormat *fmt,
610 ULocDataLocaleType type,
611 UErrorCode* status);
612
b75a7d8f
A
613#endif /* #if !UCONFIG_NO_FORMATTING */
614
615#endif