class PluralsReader (View source)

A reader for data placed in "plurals" tag in CLDR.

There are a few similar words used in plurals.xml file of CLDR used by this class. Following naming convention is used in the code (a name of corresponding tag from xml file is provided in brackets, if any):

  • ruleset: a set of plural rules for a locale [pluralRules]
  • rule: a rule for one of the forms: zero, one, two, few, many [pluralRule]
  • subrule: one of the conditions of rule. One rule can have many conditions joined with "and" or "or" logical operator.

Constants

PATTERN_MATCH_SUBRULE

An expression to catch one plural subrule. One rule consists of one or more subrules.

RULE_ZERO

Constants for every plural rule form defined in CLDR.

RULE_ONE

RULE_TWO

RULE_FEW

RULE_MANY

RULE_OTHER

Properties

protected CldrRepository $cldrRepository
protected VariableFrontend $cache
protected array $rulesets

An array of rulesets, indexed numerically.

protected array $rulesetsIndices

An associative array holding information which ruleset is used by given locale. One or more locales can use the same ruleset.

Methods

void
injectCldrRepository(CldrRepository $repository)

No description

void
injectCache(VariableFrontend $cache)

Injects the Flow_I18n_Cldr_Reader_PluralsReader cache

void
initializeObject()

Constructs the reader, loading parsed data from cache if available.

string
getPluralForm(mixed $quantity, Locale $locale)

Returns matching plural form based on $quantity and $locale provided.

array
getPluralForms(Locale $locale)

Returns array of plural forms available for particular locale.

void
generateRulesets()

Generates an internal representation of plural rules which can be found in plurals.xml CLDR file.

array
parseRule(string $rule)

Parses a plural rule from CLDR.

Details

void injectCldrRepository(CldrRepository $repository)

No description

Parameters

CldrRepository $repository

Return Value

void

void injectCache(VariableFrontend $cache)

Injects the Flow_I18n_Cldr_Reader_PluralsReader cache

Parameters

VariableFrontend $cache

Return Value

void

void initializeObject()

Constructs the reader, loading parsed data from cache if available.

Return Value

void

Exceptions

Exception

string getPluralForm(mixed $quantity, Locale $locale)

Returns matching plural form based on $quantity and $locale provided.

Plural form is one of following: zero, one, two, few, many, other. Last one (other) is returned when number provided doesn't match any of the rules, or there is no rules for given locale.

Parameters

mixed $quantity

A number to find plural form for (float or int)

Locale $locale

Return Value

string

One of plural form constants

array getPluralForms(Locale $locale)

Returns array of plural forms available for particular locale.

Parameters

Locale $locale

Locale to return plural forms for

Return Value

array

Plural forms' names (one, zero, two, few, many, other) available for language set in this model

protected void generateRulesets()

Generates an internal representation of plural rules which can be found in plurals.xml CLDR file.

The properties $rulesets and $rulesetsIndices should be empty before running this method.

Return Value

void

Exceptions

InvalidPluralRuleException

See also

PluralsReader::$rulesets

protected array parseRule(string $rule)

Parses a plural rule from CLDR.

A plural rule in CLDR is a string with one or more test conditions, with 'and' or 'or' logical operators between them. Whole expression can look like this:

n is 0 OR n is not 1 AND n mod 100 in 1..19

As CLDR documentation says, following test conditions can be used:

  • is x, is not x: $n is (not) equal $x
  • in x..y, not in x..y: $n is (not) one of integers from range <$x, $y>
  • within x..y, not within x..y: $n is (not) any number from range <$x, $y>

Where $n can be a number (also float) as is, or a result of $n mod $x.

Array returned follows simple internal format (see documentation for $rulesets property for details).

Parameters

string $rule

Return Value

array

Parsed rule

Exceptions

InvalidPluralRuleException