]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/umsg.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / umsg.h
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3* Copyright (C) 1996-2003, International Business Machines Corporation and others. All Rights Reserved.
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
25#include "unicode/parseerr.h"
26#include <stdarg.h>
27/**
28 * \file
29 * \brief C API: MessageFormat
30 *
31 * <h2>Message Format C API </h2>
32 *
33 * Provides means to produce concatenated messages in language-neutral way.
34 * Use this for all concatenations that show up to end users.
35 * <P>
36 * Takes a set of objects, formats them, then inserts the formatted
37 * strings into the pattern at the appropriate places.
38 * <P>
39 * Here are some examples of usage:
40 * Example 1:
41 * <pre>
42 * \code
43 * UChar *result, *tzID, *str;
44 * UChar pattern[100];
45 * int32_t resultLengthOut, resultlength;
46 * UCalendar *cal;
47 * UDate d1;
48 * UDateFormat *def1;
49 * UErrorCode status = U_ZERO_ERROR;
50 *
51 * str=(UChar*)malloc(sizeof(UChar) * (strlen("disturbance in force") +1));
52 * u_uastrcpy(str, "disturbance in force");
53 * tzID=(UChar*)malloc(sizeof(UChar) * 4);
54 * u_uastrcpy(tzID, "PST");
55 * cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
56 * ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
57 * d1=ucal_getMillis(cal, &status);
58 * u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
59 * resultlength=0;
60 * resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, d1, str, 7);
61 * if(status==U_BUFFER_OVERFLOW_ERROR){
62 * status=U_ZERO_ERROR;
63 * resultlength=resultLengthOut+1;
64 * result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
65 * u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
66 * }
67 * printf("%s\n", austrdup(result) );//austrdup( a function used to convert UChar* to char*)
68 * //output>: "On March 18, 1999, there was a disturbance in force on planet 7
69 * \endcode
70 * </pre>
71 * Typically, the message format will come from resources, and the
72 * arguments will be dynamically set at runtime.
73 * <P>
74 * Example 2:
75 * <pre>
76 * \code
77 * UChar* str;
78 * UErrorCode status = U_ZERO_ERROR;
79 * UChar *result;
80 * UChar pattern[100];
81 * int32_t resultlength, resultLengthOut, i;
82 * double testArgs= { 100.0, 1.0, 0.0};
83 *
84 * str=(UChar*)malloc(sizeof(UChar) * 10);
85 * u_uastrcpy(str, "MyDisk");
86 * u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
87 * for(i=0; i<3; i++){
88 * resultlength=0;
89 * resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, testArgs[i], str);
90 * if(status==U_BUFFER_OVERFLOW_ERROR){
91 * status=U_ZERO_ERROR;
92 * resultlength=resultLengthOut+1;
93 * result=(UChar*)malloc(sizeof(UChar) * resultlength);
94 * u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, testArgs[i], str);
95 * }
96 * printf("%s\n", austrdup(result) ); //austrdup( a function used to convert UChar* to char*)
97 * free(result);
98 * }
99 * // output, with different testArgs:
100 * // output: The disk "MyDisk" contains 100 files.
101 * // output: The disk "MyDisk" contains one file.
102 * // output: The disk "MyDisk" contains no files.
103 * \endcode
104 * </pre>
105 *
106 * The pattern is of the following form. Legend:
107 * <pre>
108 * \code
109 * {optional item}
110 * (group that may be repeated)*
111 * \endcode
112 * </pre>
113 * Do not confuse optional items with items inside quotes braces, such
114 * as this: "{". Quoted braces are literals.
115 * <pre>
116 * \code
117 * messageFormatPattern := string ( "{" messageFormatElement "}" string )*
118 *
119 * messageFormatElement := argument { "," elementFormat }
120 *
121 * elementFormat := "time" { "," datetimeStyle }
122 * | "date" { "," datetimeStyle }
123 * | "number" { "," numberStyle }
124 * | "choice" "," choiceStyle
125 *
126 * datetimeStyle := "short"
127 * | "medium"
128 * | "long"
129 * | "full"
130 * | dateFormatPattern
131 *
132 * numberStyle := "currency"
133 * | "percent"
134 * | "integer"
135 * | numberFormatPattern
136 *
137 * choiceStyle := choiceFormatPattern
138 * \endcode
139 * </pre>
140 * If there is no elementFormat, then the argument must be a string,
141 * which is substituted. If there is no dateTimeStyle or numberStyle,
142 * then the default format is used (e.g. NumberFormat.getInstance(),
143 * DateFormat.getDefaultTime() or DateFormat.getDefaultDate(). For
144 * a ChoiceFormat, the pattern must always be specified, since there
145 * is no default.
146 * <P>
147 * In strings, single quotes can be used to quote the "{" sign if
148 * necessary. A real single quote is represented by ''. Inside a
149 * messageFormatElement, quotes are [not] removed. For example,
150 * {1,number,$'#',##} will produce a number format with the pound-sign
151 * quoted, with a result such as: "$#31,45".
152 * <P>
153 * If a pattern is used, then unquoted braces in the pattern, if any,
154 * must match: that is, "ab {0} de" and "ab '}' de" are ok, but "ab
155 * {0'}' de" and "ab } de" are not.
156 * <P>
157 * The argument is a number from 0 to 9, which corresponds to the
158 * arguments presented in an array to be formatted.
159 * <P>
160 * It is ok to have unused arguments in the array. With missing
161 * arguments or arguments that are not of the right class for the
162 * specified format, a failing UErrorCode result is set.
163 * <P>
164
165 * <P>
166 * [Note:] As we see above, the string produced by a choice Format in
167 * MessageFormat is treated specially; occurances of '{' are used to
168 * indicated subformats.
169 * <P>
170 * [Note:] Formats are numbered by order of variable in the string.
171 * This is [not] the same as the argument numbering!
172 * <pre>
173 * \code
174 * For example: with "abc{2}def{3}ghi{0}...",
175 *
176 * format0 affects the first variable {2}
177 * format1 affects the second variable {3}
178 * format2 affects the second variable {0}
179 * \endcode
180 * </pre>
181 * and so on.
182 */
183
184/**
185 * Format a message for a locale.
186 * This function may perform re-ordering of the arguments depending on the
187 * locale. For all numeric arguments, double is assumed unless the type is
188 * explicitly integer. All choice format arguments must be of type double.
189 * @param locale The locale for which the message will be formatted
190 * @param pattern The pattern specifying the message's format
191 * @param patternLength The length of pattern
192 * @param result A pointer to a buffer to receive the formatted message.
193 * @param resultLength The maximum size of result.
194 * @param status A pointer to an UErrorCode to receive any errors
195 * @param ... A variable-length argument list containing the arguments specified
196 * in pattern.
197 * @return The total buffer size needed; if greater than resultLength, the
198 * output was truncated.
199 * @see u_parseMessage
200 * @stable ICU 2.0
201 */
202U_CAPI int32_t U_EXPORT2
203u_formatMessage(const char *locale,
204 const UChar *pattern,
205 int32_t patternLength,
206 UChar *result,
207 int32_t resultLength,
208 UErrorCode *status,
209 ...);
210
211/**
212 * Format a message for a locale.
213 * This function may perform re-ordering of the arguments depending on the
214 * locale. For all numeric arguments, double is assumed unless the type is
215 * explicitly integer. All choice format arguments must be of type double.
216 * @param locale The locale for which the message will be formatted
217 * @param pattern The pattern specifying the message's format
218 * @param patternLength The length of pattern
219 * @param result A pointer to a buffer to receive the formatted message.
220 * @param resultLength The maximum size of result.
221 * @param ap A variable-length argument list containing the arguments specified
222 * @param status A pointer to an UErrorCode to receive any errors
223 * in pattern.
224 * @return The total buffer size needed; if greater than resultLength, the
225 * output was truncated.
226 * @see u_parseMessage
227 * @stable ICU 2.0
228 */
229U_CAPI int32_t U_EXPORT2
230u_vformatMessage( const char *locale,
231 const UChar *pattern,
232 int32_t patternLength,
233 UChar *result,
234 int32_t resultLength,
235 va_list ap,
236 UErrorCode *status);
237
238/**
239 * Parse a message.
240 * For numeric arguments, this function will always use doubles. Integer types
241 * should not be passed.
242 * This function is not able to parse all output from \Ref{u_formatMessage}.
243 * @param locale The locale for which the message is formatted
244 * @param pattern The pattern specifying the message's format
245 * @param patternLength The length of pattern
246 * @param source The text to parse.
247 * @param sourceLength The length of source, or -1 if null-terminated.
248 * @param status A pointer to an UErrorCode to receive any errors
249 * @param ... A variable-length argument list containing the arguments
250 * specified in pattern.
251 * @see u_formatMessage
252 * @stable ICU 2.0
253 */
254U_CAPI void U_EXPORT2
255u_parseMessage( const char *locale,
256 const UChar *pattern,
257 int32_t patternLength,
258 const UChar *source,
259 int32_t sourceLength,
260 UErrorCode *status,
261 ...);
262
263/**
264 * Parse a message.
265 * For numeric arguments, this function will always use doubles. Integer types
266 * should not be passed.
267 * This function is not able to parse all output from \Ref{u_formatMessage}.
268 * @param locale The locale for which the message is formatted
269 * @param pattern The pattern specifying the message's format
270 * @param patternLength The length of pattern
271 * @param source The text to parse.
272 * @param sourceLength The length of source, or -1 if null-terminated.
273 * @param ap A variable-length argument list containing the arguments
274 * @param status A pointer to an UErrorCode to receive any errors
275 * specified in pattern.
276 * @see u_formatMessage
277 * @stable ICU 2.0
278 */
279U_CAPI void U_EXPORT2
280u_vparseMessage(const char *locale,
281 const UChar *pattern,
282 int32_t patternLength,
283 const UChar *source,
284 int32_t sourceLength,
285 va_list ap,
286 UErrorCode *status);
287
288/**
289 * Format a message for a locale.
290 * This function may perform re-ordering of the arguments depending on the
291 * locale. For all numeric arguments, double is assumed unless the type is
292 * explicitly integer. All choice format arguments must be of type double.
293 * @param locale The locale for which the message will be formatted
294 * @param pattern The pattern specifying the message's format
295 * @param patternLength The length of pattern
296 * @param result A pointer to a buffer to receive the formatted message.
297 * @param resultLength The maximum size of result.
298 * @param status A pointer to an UErrorCode to receive any errors
299 * @param ... A variable-length argument list containing the arguments specified
300 * in pattern.
301 * @param parseError A pointer to UParseError to receive information about errors
302 * occurred during parsing.
303 * @return The total buffer size needed; if greater than resultLength, the
304 * output was truncated.
305 * @see u_parseMessage
306 * @stable ICU 2.0
307 */
308U_CAPI int32_t U_EXPORT2
309u_formatMessageWithError( const char *locale,
310 const UChar *pattern,
311 int32_t patternLength,
312 UChar *result,
313 int32_t resultLength,
314 UParseError *parseError,
315 UErrorCode *status,
316 ...);
317
318/**
319 * Format a message for a locale.
320 * This function may perform re-ordering of the arguments depending on the
321 * locale. For all numeric arguments, double is assumed unless the type is
322 * explicitly integer. All choice format arguments must be of type double.
323 * @param locale The locale for which the message will be formatted
324 * @param pattern The pattern specifying the message's format
325 * @param patternLength The length of pattern
326 * @param result A pointer to a buffer to receive the formatted message.
327 * @param resultLength The maximum size of result.
328 * @param parseError A pointer to UParseError to receive information about errors
329 * occurred during parsing.
330 * @param ap A variable-length argument list containing the arguments specified
331 * @param status A pointer to an UErrorCode to receive any errors
332 * in pattern.
333 * @return The total buffer size needed; if greater than resultLength, the
334 * output was truncated.
335 * @stable ICU 2.0
336 */
337U_CAPI int32_t U_EXPORT2
338u_vformatMessageWithError( const char *locale,
339 const UChar *pattern,
340 int32_t patternLength,
341 UChar *result,
342 int32_t resultLength,
343 UParseError* parseError,
344 va_list ap,
345 UErrorCode *status);
346
347/**
348 * Parse a message.
349 * For numeric arguments, this function will always use doubles. Integer types
350 * should not be passed.
351 * This function is not able to parse all output from \Ref{u_formatMessage}.
352 * @param locale The locale for which the message is formatted
353 * @param pattern The pattern specifying the message's format
354 * @param patternLength The length of pattern
355 * @param source The text to parse.
356 * @param sourceLength The length of source, or -1 if null-terminated.
357 * @param parseError A pointer to UParseError to receive information about errors
358 * occurred during parsing.
359 * @param status A pointer to an UErrorCode to receive any errors
360 * @param ... A variable-length argument list containing the arguments
361 * specified in pattern.
362 * @see u_formatMessage
363 * @stable ICU 2.0
364 */
365U_CAPI void U_EXPORT2
366u_parseMessageWithError(const char *locale,
367 const UChar *pattern,
368 int32_t patternLength,
369 const UChar *source,
370 int32_t sourceLength,
371 UParseError *parseError,
372 UErrorCode *status,
373 ...);
374
375/**
376 * Parse a message.
377 * For numeric arguments, this function will always use doubles. Integer types
378 * should not be passed.
379 * This function is not able to parse all output from \Ref{u_formatMessage}.
380 * @param locale The locale for which the message is formatted
381 * @param pattern The pattern specifying the message's format
382 * @param patternLength The length of pattern
383 * @param source The text to parse.
384 * @param sourceLength The length of source, or -1 if null-terminated.
385 * @param ap A variable-length argument list containing the arguments
386 * @param parseError A pointer to UParseError to receive information about errors
387 * occurred during parsing.
388 * @param status A pointer to an UErrorCode to receive any errors
389 * specified in pattern.
390 * @see u_formatMessage
391 * @stable ICU 2.0
392 */
393U_CAPI void U_EXPORT2
394u_vparseMessageWithError(const char *locale,
395 const UChar *pattern,
396 int32_t patternLength,
397 const UChar *source,
398 int32_t sourceLength,
399 va_list ap,
400 UParseError *parseError,
401 UErrorCode* status);
402
403/*----------------------- New experimental API --------------------------- */
404/**
405 * The message format object
406 * @stable ICU 2.0
407 */
408typedef void* UMessageFormat;
409
410
411/**
412 * Open a message formatter with given pattern and for the given locale.
413 * @param pattern A pattern specifying the format to use.
414 * @param patternLength Length of the pattern to use
415 * @param locale The locale for which the messages are formatted.
416 * @param parseError A pointer to UParseError struct to receive any errors
417 * occured during parsing. Can be NULL.
418 * @param status A pointer to an UErrorCode to receive any errors.
419 * @return A pointer to a UMessageFormat to use for formatting
420 * messages, or 0 if an error occurred.
421 * @stable ICU 2.0
422 */
423U_CAPI UMessageFormat* U_EXPORT2
424umsg_open( const UChar *pattern,
425 int32_t patternLength,
426 const char *locale,
427 UParseError *parseError,
428 UErrorCode *status);
429
430/**
431 * Close a UMessageFormat.
432 * Once closed, a UMessageFormat may no longer be used.
433 * @param format The formatter to close.
434 * @stable ICU 2.0
435 */
436U_CAPI void U_EXPORT2
437umsg_close(UMessageFormat* format);
438
439/**
440 * Open a copy of a UMessageFormat.
441 * This function performs a deep copy.
442 * @param fmt The formatter to copy
443 * @param status A pointer to an UErrorCode to receive any errors.
444 * @return A pointer to a UDateFormat identical to fmt.
445 * @stable ICU 2.0
446 */
447U_CAPI UMessageFormat U_EXPORT2
448umsg_clone(const UMessageFormat *fmt,
449 UErrorCode *status);
450
451/**
452 * Sets the locale. This locale is used for fetching default number or date
453 * format information.
454 * @param fmt The formatter to set
455 * @param locale The locale the formatter should use.
456 * @stable ICU 2.0
457 */
458U_CAPI void U_EXPORT2
459umsg_setLocale(UMessageFormat *fmt,
460 const char* locale);
461
462/**
463 * Gets the locale. This locale is used for fetching default number or date
464 * format information.
465 * @param fmt The formatter to querry
466 * @return the locale.
467 * @stable ICU 2.0
468 */
469U_CAPI const char* U_EXPORT2
470umsg_getLocale(UMessageFormat *fmt);
471
472/**
473 * Sets the pattern.
474 * @param fmt The formatter to use
475 * @param pattern The pattern to be applied.
476 * @param patternLength Length of the pattern to use
477 * @param parseError Struct to receive information on position
478 * of error if an error is encountered.Can be NULL.
479 * @param status Output param set to success/failure code on
480 * exit. If the pattern is invalid, this will be
481 * set to a failure result.
482 * @stable ICU 2.0
483 */
484U_CAPI void U_EXPORT2
485umsg_applyPattern( UMessageFormat *fmt,
486 const UChar* pattern,
487 int32_t patternLength,
488 UParseError* parseError,
489 UErrorCode* status);
490
491/**
492 * Gets the pattern.
493 * @param fmt The formatter to use
494 * @param result A pointer to a buffer to receive the pattern.
495 * @param resultLength The maximum size of result.
496 * @param status Output param set to success/failure code on
497 * exit. If the pattern is invalid, this will be
498 * set to a failure result.
499 * @return the pattern of the format
500 * @stable ICU 2.0
501 */
502U_CAPI int32_t U_EXPORT2
503umsg_toPattern(UMessageFormat *fmt,
504 UChar* result,
505 int32_t resultLength,
506 UErrorCode* status);
507
508/**
509 * Format a message for a locale.
510 * This function may perform re-ordering of the arguments depending on the
511 * locale. For all numeric arguments, double is assumed unless the type is
512 * explicitly integer. All choice format arguments must be of type double.
513 * @param fmt The formatter to use
514 * @param result A pointer to a buffer to receive the formatted message.
515 * @param resultLength The maximum size of result.
516 * @param status A pointer to an UErrorCode to receive any errors
517 * @param ... A variable-length argument list containing the arguments
518 * specified in pattern.
519 * @return The total buffer size needed; if greater than resultLength,
520 * the output was truncated.
521 * @stable ICU 2.0
522 */
523U_CAPI int32_t U_EXPORT2
524umsg_format( UMessageFormat *fmt,
525 UChar *result,
526 int32_t resultLength,
527 UErrorCode *status,
528 ...);
529
530/**
531 * Format a message for a locale.
532 * This function may perform re-ordering of the arguments depending on the
533 * locale. For all numeric arguments, double is assumed unless the type is
534 * explicitly integer. All choice format arguments must be of type double.
535 * @param fmt The formatter to use
536 * @param result A pointer to a buffer to receive the formatted message.
537 * @param resultLength The maximum size of result.
538 * @param ap A variable-length argument list containing the arguments
539 * @param status A pointer to an UErrorCode to receive any errors
540 * specified in pattern.
541 * @return The total buffer size needed; if greater than resultLength,
542 * the output was truncated.
543 * @stable ICU 2.0
544 */
545U_CAPI int32_t U_EXPORT2
546umsg_vformat( UMessageFormat *fmt,
547 UChar *result,
548 int32_t resultLength,
549 va_list ap,
550 UErrorCode *status);
551
552/**
553 * Parse a message.
554 * For numeric arguments, this function will always use doubles. Integer types
555 * should not be passed.
556 * This function is not able to parse all output from \Ref{umsg_format}.
557 * @param fmt The formatter to use
558 * @param source The text to parse.
559 * @param sourceLength The length of source, or -1 if null-terminated.
560 * @param count Output param to receive number of elements returned.
561 * @param status A pointer to an UErrorCode to receive any errors
562 * @param ... A variable-length argument list containing the arguments
563 * specified in pattern.
564 * @stable ICU 2.0
565 */
566U_CAPI void U_EXPORT2
567umsg_parse( UMessageFormat *fmt,
568 const UChar *source,
569 int32_t sourceLength,
570 int32_t *count,
571 UErrorCode *status,
572 ...);
573
574/**
575 * Parse a message.
576 * For numeric arguments, this function will always use doubles. Integer types
577 * should not be passed.
578 * This function is not able to parse all output from \Ref{umsg_format}.
579 * @param fmt The formatter to use
580 * @param source The text to parse.
581 * @param sourceLength The length of source, or -1 if null-terminated.
582 * @param count Output param to receive number of elements returned.
583 * @param ap A variable-length argument list containing the arguments
584 * @param status A pointer to an UErrorCode to receive any errors
585 * specified in pattern.
586 * @see u_formatMessage
587 * @stable ICU 2.0
588 */
589U_CAPI void U_EXPORT2
590umsg_vparse(UMessageFormat *fmt,
591 const UChar *source,
592 int32_t sourceLength,
593 int32_t *count,
594 va_list ap,
595 UErrorCode *status);
596
597#endif /* #if !UCONFIG_NO_FORMATTING */
598
599#endif