Logo Search packages:      
Sourcecode: icu version File versions  Download package

umsg.h File Reference

C API: MessageFormat. More...

#include "unicode/utypes.h"
#include "unicode/localpointer.h"
#include "unicode/uloc.h"
#include "unicode/parseerr.h"
#include <stdarg.h>
Include dependency graph for umsg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef voidUMessageFormat

Functions

U_STABLE int32_t U_EXPORT2 u_formatMessage (const char *locale, const UChar *pattern, int32_t patternLength, UChar *result, int32_t resultLength, UErrorCode *status,...)
U_STABLE int32_t U_EXPORT2 u_formatMessageWithError (const char *locale, const UChar *pattern, int32_t patternLength, UChar *result, int32_t resultLength, UParseError *parseError, UErrorCode *status,...)
U_STABLE void U_EXPORT2 u_parseMessage (const char *locale, const UChar *pattern, int32_t patternLength, const UChar *source, int32_t sourceLength, UErrorCode *status,...)
U_STABLE void U_EXPORT2 u_parseMessageWithError (const char *locale, const UChar *pattern, int32_t patternLength, const UChar *source, int32_t sourceLength, UParseError *parseError, UErrorCode *status,...)
U_STABLE int32_t U_EXPORT2 u_vformatMessage (const char *locale, const UChar *pattern, int32_t patternLength, UChar *result, int32_t resultLength, va_list ap, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 u_vformatMessageWithError (const char *locale, const UChar *pattern, int32_t patternLength, UChar *result, int32_t resultLength, UParseError *parseError, va_list ap, UErrorCode *status)
U_STABLE void U_EXPORT2 u_vparseMessage (const char *locale, const UChar *pattern, int32_t patternLength, const UChar *source, int32_t sourceLength, va_list ap, UErrorCode *status)
U_STABLE void U_EXPORT2 u_vparseMessageWithError (const char *locale, const UChar *pattern, int32_t patternLength, const UChar *source, int32_t sourceLength, va_list ap, UParseError *parseError, UErrorCode *status)
U_STABLE void U_EXPORT2 umsg_applyPattern (UMessageFormat *fmt, const UChar *pattern, int32_t patternLength, UParseError *parseError, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 umsg_autoQuoteApostrophe (const UChar *pattern, int32_t patternLength, UChar *dest, int32_t destCapacity, UErrorCode *ec)
U_STABLE UMessageFormat U_EXPORT2 umsg_clone (const UMessageFormat *fmt, UErrorCode *status)
U_STABLE void U_EXPORT2 umsg_close (UMessageFormat *format)
U_STABLE int32_t U_EXPORT2 umsg_format (const UMessageFormat *fmt, UChar *result, int32_t resultLength, UErrorCode *status,...)
U_STABLE const char *U_EXPORT2 umsg_getLocale (const UMessageFormat *fmt)
U_STABLE UMessageFormat *U_EXPORT2 umsg_open (const UChar *pattern, int32_t patternLength, const char *locale, UParseError *parseError, UErrorCode *status)
U_STABLE void U_EXPORT2 umsg_parse (const UMessageFormat *fmt, const UChar *source, int32_t sourceLength, int32_t *count, UErrorCode *status,...)
U_STABLE void U_EXPORT2 umsg_setLocale (UMessageFormat *fmt, const char *locale)
U_STABLE int32_t U_EXPORT2 umsg_toPattern (const UMessageFormat *fmt, UChar *result, int32_t resultLength, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 umsg_vformat (const UMessageFormat *fmt, UChar *result, int32_t resultLength, va_list ap, UErrorCode *status)
U_STABLE void U_EXPORT2 umsg_vparse (const UMessageFormat *fmt, const UChar *source, int32_t sourceLength, int32_t *count, va_list ap, UErrorCode *status)

Detailed Description

C API: MessageFormat.

MessageFormat C API

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.

The opaque UMessageFormat type is a thin C wrapper around a C++ MessageFormat. It is constructed from a pattern string with arguments in {curly braces} which will be replaced by formatted values.

Currently, the C API supports only numbered arguments.

For details about the pattern syntax and behavior, especially about the ASCII apostrophe vs. the real apostrophe (single quote) character ’ (U+2019), see the C++ MessageFormat class documentation.

Here are some examples of C API usage: Example 1:

 
     UChar *result, *tzID, *str;
     UChar pattern[100];
     int32_t resultLengthOut, resultlength;
     UCalendar *cal;
     UDate d1;
     UDateFormat *def1;
     UErrorCode status = U_ZERO_ERROR;

     str=(UChar*)malloc(sizeof(UChar) * (strlen("disturbance in force") +1));
     u_uastrcpy(str, "disturbance in force");
     tzID=(UChar*)malloc(sizeof(UChar) * 4);
     u_uastrcpy(tzID, "PST");
     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
     d1=ucal_getMillis(cal, &status);
     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
     resultlength=0;
     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, d1, str, 7);
     if(status==U_BUFFER_OVERFLOW_ERROR){
         status=U_ZERO_ERROR;
         resultlength=resultLengthOut+1;
         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
     }
     printf("%s\n", austrdup(result) );//austrdup( a function used to convert UChar* to char*)
     //output>: "On March 18, 1999, there was a disturbance in force on planet 7

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

Example 2:

 
     UChar* str;
     UErrorCode status = U_ZERO_ERROR;
     UChar *result;
     UChar pattern[100];
     int32_t resultlength, resultLengthOut, i;
     double testArgs= { 100.0, 1.0, 0.0};

     str=(UChar*)malloc(sizeof(UChar) * 10);
     u_uastrcpy(str, "MyDisk");
     u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
     for(i=0; i<3; i++){
       resultlength=0; 
       resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, testArgs[i], str); 
       if(status==U_BUFFER_OVERFLOW_ERROR){
         status=U_ZERO_ERROR;
         resultlength=resultLengthOut+1;
         result=(UChar*)malloc(sizeof(UChar) * resultlength);
         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, testArgs[i], str);
       }
       printf("%s\n", austrdup(result) );  //austrdup( a function used to convert UChar* to char*)
       free(result);
     }
     // output, with different testArgs:
     // output: The disk "MyDisk" contains 100 files.
     // output: The disk "MyDisk" contains one file.
     // output: The disk "MyDisk" contains no files.

Example 3:

 
 UChar* str;
 UChar* str1;
 UErrorCode status = U_ZERO_ERROR;
 UChar *result;
 UChar pattern[100];
 UChar expected[100];
 int32_t resultlength,resultLengthOut;

 str=(UChar*)malloc(sizeof(UChar) * 25);
 u_uastrcpy(str, "Kirti");
 str1=(UChar*)malloc(sizeof(UChar) * 25);
 u_uastrcpy(str1, "female");
 log_verbose("Testing message format with Select test #1\n:");
 u_uastrcpy(pattern, "{0} est {1, select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
 u_uastrcpy(expected, "Kirti est all\\u00E9e \\u00E0 Paris.");
 resultlength=0;
 resultLengthOut=u_formatMessage( "fr", pattern, u_strlen(pattern), NULL, resultlength, &status, str , str1);
 if(status==U_BUFFER_OVERFLOW_ERROR)
  {
      status=U_ZERO_ERROR;
      resultlength=resultLengthOut+1;
      result=(UChar*)malloc(sizeof(UChar) * resultlength);
      u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1);
      if(u_strcmp(result, expected)==0)
          log_verbose("PASS: MessagFormat successful on Select test#1\n");
      else{
          log_err("FAIL: Error in MessageFormat on Select test#1\n GOT %s EXPECTED %s\n", austrdup(result),
          austrdup(expected) );
      }
      free(result);
 }

Definition in file umsg.h.


Generated by  Doxygen 1.6.0   Back to index