Share via


PluralFormat Class

Definition

PluralFormat supports the creation of internationalized messages with plural inflection.

[Android.Runtime.Register("android/icu/text/PluralFormat", ApiSince=24, DoNotGenerateAcw=true)]
public class PluralFormat : Android.Icu.Text.UFormat
[<Android.Runtime.Register("android/icu/text/PluralFormat", ApiSince=24, DoNotGenerateAcw=true)>]
type PluralFormat = class
    inherit UFormat
Inheritance
PluralFormat
Attributes

Remarks

PluralFormat 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 PluralFormat selects the appropriate message based on the number.

<h3>The Problem of Plural Forms in Internationalized Messages</h3>

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. ChoiceFormat doesn't handle this well, because it attaches a number interval to each message and selects the message whose interval contains a given number. This can only handle a finite number of intervals. But in some languages, like Polish, one plural case applies to infinitely many intervals (e.g., the paucal case applies to numbers ending with 2, 3, or 4 except those ending with 12, 13, or 14). Thus ChoiceFormat is not adequate.

PluralFormat deals with this by breaking the problem into two parts: <ul> <li>It uses PluralRules 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. <li>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 https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html </ul>

<h4>Usage of PluralFormat</h4>

Note: Typically, plural formatting is done via MessageFormat with a plural argument type, rather than using a stand-alone PluralFormat.

This discussion assumes that you use PluralFormat with a predefined set of plural rules. You can create one using one of the constructors that takes a ULocale object. To specify the message pattern, you can either pass it to the constructor or set it explicitly using the applyPattern() method. The format() method takes a number object and selects the message of the matching plural case. This message will be returned.

<h5>Patterns and Their Interpretation</h5>

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

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: see {@link MessageFormat}

</blockquote> 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 case keywords 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 PluralRules with the input number <em>minus the offset</em>. (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 NumberFormat for the PluralFormat's locale. If you need special number formatting, you have to use a MessageFormat and explicitly specify a NumberFormat argument. <strong>Note:</strong> 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.

For a usage example, see the MessageFormat class documentation.

<h4>Defining Custom Plural Rules</h4>

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

For more information about PluralRules, see PluralRules.

Java documentation for android.icu.text.PluralFormat.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

PluralFormat()

Creates a new cardinal-number PluralFormat for the default FORMAT locale.

PluralFormat(IntPtr, JniHandleOwnership)
PluralFormat(Locale, PluralRules)

Creates a new cardinal-number PluralFormat for a given set of rules.

PluralFormat(Locale, PluralRules+PluralType)
PluralFormat(Locale)

Creates a new cardinal-number PluralFormat for a given java.util.Locale.

PluralFormat(PluralRules, String)

Creates a new cardinal-number PluralFormat for a given set of rules and a pattern.

PluralFormat(PluralRules)

Creates a new cardinal-number PluralFormat for a given set of rules.

PluralFormat(String)

Creates a new cardinal-number PluralFormat for a given pattern string.

PluralFormat(ULocale, PluralRules, String)

Creates a new cardinal-number PluralFormat for a given set of rules, a pattern and a locale.

PluralFormat(ULocale, PluralRules)

Creates a new cardinal-number PluralFormat for a given set of rules.

PluralFormat(ULocale, PluralRules+PluralType, String)
PluralFormat(ULocale, PluralRules+PluralType)
PluralFormat(ULocale, String)

Creates a new cardinal-number PluralFormat for a given pattern string and locale.

PluralFormat(ULocale)

Creates a new cardinal-number PluralFormat for a given locale.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass
ThresholdType

Methods

ApplyPattern(String)

Sets the pattern used by this plural format.

Clone()

Creates and returns a copy of this object.

(Inherited from _Format)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Equals(PluralFormat)

Returns true if this equals the provided PluralFormat.

Format(Double)

Formats a plural message for a given number.

Format(Object, StringBuffer, FieldPosition)

Formats a plural message for a given number and appends the formatted message to the given StringBuffer.

Format(Object)

Formats an object to produce a string.

(Inherited from _Format)
FormatToCharacterIterator(Object)

Formats an Object producing an AttributedCharacterIterator.

(Inherited from _Format)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Parse(String, ParsePosition)

This method is not yet supported by PluralFormat.

ParseObject(String, ParsePosition)

This method is not yet supported by PluralFormat.

ParseObject(String)

Parses text from the beginning of the given string to produce an object.

(Inherited from _Format)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetNumberFormat(NumberFormat)

Sets the number format used by this formatter.

ToArray<T>() (Inherited from Object)
ToPattern()

Returns the pattern for this PluralFormat.

ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Gets the JNI name of the type of the instance self.

JavaAs<TResult>(IJavaPeerable)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

TryJavaCast<TResult>(IJavaPeerable, TResult)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

Applies to