Tizen Native API  6.5
PluralFormat

The Plural Format is a formatter for Plural objects.

Required Header

#include <utils_i18n.h>

Overview

Plural Format supports the creation of internationalized messages with plural inflection. It is based on plural selection, i.e. the caller specifies messages for each plural case that can appear in the user's language and the Plural Format selects the appropriate message based on the number.

The Problem of Plural Forms in Internationalized Messages:

Different languages have different ways to inflect plurals. Creating internationalized messages that include plural forms is only feasible when the framework is able to handle plural forms of all languages correctly. In some languages, like Polish, one plural case applies to infinitely many intervals (e.g., the plural case applies to numbers ending with 2, 3, or 4 except those ending with 12, 13, or 14).

Plural Format deals with this by breaking the problem into two parts:

  • It uses Plural Rules that can define more complex conditions for a plural case than just a single interval. These plural rules define both what plural cases exist in a language, and to which numbers these cases apply.
  • It provides predefined plural rules for many languages. Thus, the programmer need not worry about the plural cases of a language and does not have to define the plural cases; they can simply use the predefined keywords. The whole plural formatting of messages can be done using localized patterns from resource bundles. For predefined plural rules, see the CLDR Language Plural Rules page at http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html

Usage of Plural Format:

This discussion assumes that you use Plural Format with a predefined set of plural rules. You can create one using one of the constructors that takes a locale object. To specify the message pattern, you can either pass it to the constructor or set it explicitly using the i18n_plural_format_apply_pattern() function. The i18n_plural_format_format_* functions takes a number object and selects the message of the matching plural case. This message will be returned.

Patterns and Their Interpretation:

The pattern text defines the message output for each plural case of the specified locale. Syntax:

          pluralStyle = [offsetValue] (selector '{' message '}')+
          offsetValue = "offset:" number
          selector = explicitValue | keyword
          explicitValue = '=' number  // adjacent, no white space in between
          keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
          message: message = messageText (argument messageText)*
          argument = noneArg | simpleArg | complexArg
          complexArg = pluralArg | selectordinalArg
          noneArg = '{' argNameOrNumber '}'
          simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'
          pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'
          selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}'
          argNameOrNumber = argName | argNumber
          argName = [^[[: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

Examples:

          one{dog} other{dogs}                        // English plural format for dog
          one{pes} two{psa} few{psi} other{psov}      //Slovenian translation of dog in plural format

Pattern_White_Space between syntax elements is ignored, except between the {curly braces} and their sub-message, and between the '=' and the number of an explicitValue.

There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and 'other'. You always have to define a message text for the default plural case other which is contained in every rule set. If you do not specify a message text for a particular plural case, the message text of the plural case other gets assigned to this plural case.

When formatting, the input number is first matched against the explicitValue clauses. If there is no exact-number match, then a keyword is selected by calling the Plural Rules with the input number minus the offset. (The offset defaults to 0 if it is omitted from the pattern string.) If there is no clause with that keyword, then the "other" clauses is returned.

An unquoted pound sign (#) in the selected sub-message itself (i.e., outside of arguments nested in the sub-message) is replaced by the input number minus the offset. The number-minus-offset value is formatted using a Number Format for the Plural Format's locale.

Note: That argument is formatting without subtracting the offset! If you need a custom format and have a non-zero offset, then you need to pass the number-minus-offset value as a separate parameter.

Defining Custom Plural Rules:

If you need to use Plural Format with custom rules, you can create a Plural Rules object and pass it to Plural Format's constructor. If you also specify a locale in this constructor, this locale will be used to format the number in the message texts.

Functions

int i18n_plural_format_create (i18n_plural_format_h *plural_format)
 Creates a new cardinal-number plural format object for the default locale.
int i18n_plural_format_create_from_locale (const char *locale, i18n_plural_format_h *plural_format)
 Creates a new cardinal-number plural format object for a given locale.
int i18n_plural_format_create_from_rules_pattern (i18n_plural_rules_h plural_rules, const char *pattern, i18n_plural_format_h *plural_format)
 Creates a new plural format object for a given set of rules, a pattern and a default locale.
int i18n_plural_format_create_from_locale_type_pattern (const char *locale, i18n_uplural_type_e type, const char *pattern, i18n_plural_format_h *plural_format)
 Creates a new plural format object for a plural type, a pattern and a locale.
int i18n_plural_format_create_from_locale_type (const char *locale, i18n_uplural_type_e type, i18n_plural_format_h *plural_format)
 Creates a new plural format object for the plural type.
int i18n_plural_format_create_from_locale_pattern (const char *locale, const char *pattern, i18n_plural_format_h *plural_format)
 Creates a new cardinal-number plural format object for a given pattern string and locale.
int i18n_plural_format_create_from_locale_rules_pattern (const char *locale, i18n_plural_rules_h plural_rules, const char *pattern, i18n_plural_format_h *plural_format)
 Creates a new plural format object for a given set of rules, a pattern and a locale.
int i18n_plural_format_destroy (i18n_plural_format_h plural_format)
 Destroys the plural format object.
int i18n_plural_format_clone (i18n_plural_format_h plural_format, i18n_format_h *clone)
 Creates a polymorphic clone of the given plural_format object.
int i18n_plural_format_apply_pattern (i18n_plural_format_h plural_format, const char *pattern)
 Sets the pattern used by this plural format.
int i18n_plural_format_format_int32 (i18n_plural_format_h plural_format, int32_t number, i18n_field_position_h field_position, int32_t append_to_size, i18n_uchar *append_to, int32_t *output_length)
 Formats a plural message for a given int32_t number.
int i18n_plural_format_format_double (i18n_plural_format_h plural_format, double number, i18n_field_position_h field_position, int32_t append_to_size, i18n_uchar *append_to, int32_t *output_length)
 Formats a plural message for a given double number.
int i18n_plural_format_format_formattable (i18n_plural_format_h plural_format, i18n_formattable_h formattable, i18n_field_position_h field_position, int32_t append_to_size, i18n_uchar *append_to, int32_t *output_length)
 Formats a plural message for a number taken from a i18n_formattable_h object.
int i18n_plural_format_set_number_format (i18n_plural_format_h plural_format, i18n_unumber_format_h number_format)
 Sets the number format used by this formatter.
int i18n_plural_format_to_pattern (i18n_plural_format_h plural_format, i18n_field_position_h field_position, int32_t append_to_size, i18n_uchar *append_to, int32_t *output_length)
 Returns the pattern from i18n_plural_format_apply_pattern() or i18n_plural_format_create_pattern().

Typedefs

typedef void * i18n_plural_format_h
 An i18n_plural_format_h handle.

Typedef Documentation

typedef void* i18n_plural_format_h

An i18n_plural_format_h handle.

Use i18n_plural_format_* functions to operate on i18n_plural_format_h objects.

Since :
5.0

Function Documentation

int i18n_plural_format_apply_pattern ( i18n_plural_format_h  plural_format,
const char *  pattern 
)

Sets the pattern used by this plural format.

The method parses the pattern and creates a map of format strings for the plural rules. Patterns and their interpretation are specified in the module overview.

Since :
5.0
Parameters:
[in]plural_formatThe plural format object to set the pattern
[in]patternThe pattern to set in plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
int i18n_plural_format_clone ( i18n_plural_format_h  plural_format,
i18n_format_h clone 
)

Creates a polymorphic clone of the given plural_format object.

Since :
5.0
Remarks:
The clone object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]plural_formatThe plural format object to be cloned
[out]cloneThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory

Creates a new cardinal-number plural format object for the default locale.

This locale will be used to get the set of plural rules and for standard number formatting.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory
int i18n_plural_format_create_from_locale ( const char *  locale,
i18n_plural_format_h plural_format 
)

Creates a new cardinal-number plural format object for a given locale.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]localeThe locale string containing language and country code
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory
int i18n_plural_format_create_from_locale_pattern ( const char *  locale,
const char *  pattern,
i18n_plural_format_h plural_format 
)

Creates a new cardinal-number plural format object for a given pattern string and locale.

The locale will be used to get the set of plural rules and for standard number formatting.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]localeThe locale string containing language and country code
[in]patternThe pattern for this plural format object
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory
int i18n_plural_format_create_from_locale_rules_pattern ( const char *  locale,
i18n_plural_rules_h  plural_rules,
const char *  pattern,
i18n_plural_format_h plural_format 
)

Creates a new plural format object for a given set of rules, a pattern and a locale.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]localeThe locale string containing language and country code
[in]plural_rulesPlural rules which defines the behavior of the plural format object
[in]patternThe pattern for this plural format object
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory
int i18n_plural_format_create_from_locale_type ( const char *  locale,
i18n_uplural_type_e  type,
i18n_plural_format_h plural_format 
)

Creates a new plural format object for the plural type.

The standard number formatting will be done using the given locale.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]localeThe locale string containing language and country code
[in]typeThe plural type
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory
int i18n_plural_format_create_from_locale_type_pattern ( const char *  locale,
i18n_uplural_type_e  type,
const char *  pattern,
i18n_plural_format_h plural_format 
)

Creates a new plural format object for a plural type, a pattern and a locale.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]localeThe locale string containing language and country code
[in]typeThe plural type
[in]patternThe pattern for this plural format object
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory
int i18n_plural_format_create_from_rules_pattern ( i18n_plural_rules_h  plural_rules,
const char *  pattern,
i18n_plural_format_h plural_format 
)

Creates a new plural format object for a given set of rules, a pattern and a default locale.

Since :
5.0
Remarks:
The created object should be released by the caller with the i18n_plural_format_destroy() function.
Parameters:
[in]plural_rulesPlural rules which defines the behavior of the plural format object
[in]patternThe pattern for this plural format object
[out]plural_formatThe created plural format object
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_OUT_OF_MEMORYOut of memory

Destroys the plural format object.

Since :
5.0
Parameters:
[in]plural_formatThe plural format object to destroy
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
int i18n_plural_format_format_double ( i18n_plural_format_h  plural_format,
double  number,
i18n_field_position_h  field_position,
int32_t  append_to_size,
i18n_uchar append_to,
int32_t *  output_length 
)

Formats a plural message for a given double number.

Since :
5.0
Parameters:
[in]plural_formatThe format object for which number will be formatted
[in]numberNumber to format
[in]field_positionThe i18n_field_position_h object
[in]append_to_sizeThe size of the append_to buffer. If append_to_size <= 0 then output string is not appended to the append_to buffer and I18N_ERROR_BUFFER_OVERFLOW is returned.
[in,out]append_toThe buffer to which formatted string will be appended. Can be NULL if append_to_size <= 0. Otherwise must be NULL terminated.
[out]output_lengthThe length of the output string, which is the string in append_to buffer with formatted string.
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_BUFFER_OVERFLOWBuffer overflow
int i18n_plural_format_format_formattable ( i18n_plural_format_h  plural_format,
i18n_formattable_h  formattable,
i18n_field_position_h  field_position,
int32_t  append_to_size,
i18n_uchar append_to,
int32_t *  output_length 
)

Formats a plural message for a number taken from a i18n_formattable_h object.

Since :
5.0
Parameters:
[in]plural_formatThe format object for which number will be formatted
[in]formattableThe object containing a number for which the plural message should be formatted. The object must be of a numeric type.
[in]field_positionThe i18n_field_position_h object
[in]append_to_sizeThe size of the append_to buffer. If append_to_size <= 0 then output string is not appended to the append_to buffer and I18N_ERROR_BUFFER_OVERFLOW is returned.
[in,out]append_toThe buffer to which formatted string will be appended. Can be NULL if append_to_size <= 0. Otherwise must be NULL terminated.
[out]output_lengthThe length of the output string, which is the string in append_to buffer with formatted string.
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_BUFFER_OVERFLOWBuffer overflow
int i18n_plural_format_format_int32 ( i18n_plural_format_h  plural_format,
int32_t  number,
i18n_field_position_h  field_position,
int32_t  append_to_size,
i18n_uchar append_to,
int32_t *  output_length 
)

Formats a plural message for a given int32_t number.

Since :
5.0
Parameters:
[in]plural_formatThe format object for which number will be formatted
[in]numberNumber to format
[in]field_positionThe i18n_field_position_h object
[in]append_to_sizeThe size of the append_to buffer. If append_to_size <= 0 then output string is not appended to the append_to buffer and I18N_ERROR_BUFFER_OVERFLOW is returned.
[in,out]append_toThe buffer to which formatted string will be appended. Can be NULL if append_to_size <= 0. Otherwise must be NULL terminated.
[out]output_lengthThe length of the output string, which is the string in append_to buffer with formatted string.
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_BUFFER_OVERFLOWBuffer overflow

Sets the number format used by this formatter.

You only need to call this if you want a different number format than the default formatter for the locale.

Since :
5.0
Parameters:
[in]plural_formatThe format object to set
[in]number_formatThe number format to use
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
int i18n_plural_format_to_pattern ( i18n_plural_format_h  plural_format,
i18n_field_position_h  field_position,
int32_t  append_to_size,
i18n_uchar append_to,
int32_t *  output_length 
)

Returns the pattern from i18n_plural_format_apply_pattern() or i18n_plural_format_create_pattern().

Since :
5.0
Parameters:
[in]plural_formatThe format object to get pattern from
[in]field_positionThe i18n_field_position_h object
[in]append_to_sizeThe size of the append_to buffer. If append_to_size <= 0 then output string is not appended to the append_to buffer and I18N_ERROR_BUFFER_OVERFLOW is returned.
[in,out]append_toThe buffer to which formatted string will be appended. Can be NULL if append_to_size <= 0. Otherwise must be NULL terminated.
[out]output_lengthThe length of the output string, which is the string in append_to buffer with formatted string.
Returns:
0 on success, otherwise a negative error value
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
I18N_ERROR_BUFFER_OVERFLOWBuffer overflow