Pluralization Support

The pluralization support provides functionality around the CLDR pluralization rules. It can parse and evaluate pluralization rules, as well as convert them to other formats such as gettext.

Basic Interface

class babel.plural.PluralRule(rules)

Represents a set of language pluralization rules. The constructor accepts a list of (tag, expr) tuples or a dict of CLDR rules. The resulting object is callable and accepts one parameter with a positive or negative number (both integer and float) for the number that indicates the plural form for a string and returns the tag for the format:

>>> rule = PluralRule({'one': 'n is 1'})
>>> rule(1)
'one'
>>> rule(2)
'other'

Currently the CLDR defines these tags: zero, one, two, few, many and other where other is an implicit default. Rules should be mutually exclusive; for a given numeric value, only one rule should apply (i.e. the condition should only be true for one of the plural rule elements.

classmethod parse(rules)

Create a PluralRule instance for the given rules. If the rules are a PluralRule object, that object is returned.

Parameters

rules – the rules as list or dict, or a PluralRule object

Raises

RuleError – if the expression is malformed

property rules

The PluralRule as a dict of unicode plural rules.

>>> rule = PluralRule({'one': 'n is 1'})
>>> rule.rules
{'one': 'n is 1'}
property tags

A set of explicitly defined tags in this rule. The implicit default 'other' rules is not part of this set unless there is an explicit rule for it.

Conversion Functionality

babel.plural.to_javascript(rule)

Convert a list/dict of rules or a PluralRule object into a JavaScript function. This function depends on no external library:

>>> to_javascript({'one': 'n is 1'})
"(function(n) { return (n == 1) ? 'one' : 'other'; })"

Implementation detail: The function generated will probably evaluate expressions involved into range operations multiple times. This has the advantage that external helper functions are not required and is not a big performance hit for these simple calculations.

Parameters

rule – the rules as list or dict, or a PluralRule object

Raises

RuleError – if the expression is malformed

babel.plural.to_python(rule)

Convert a list/dict of rules or a PluralRule object into a regular Python function. This is useful in situations where you need a real function and don’t are about the actual rule object:

>>> func = to_python({'one': 'n is 1', 'few': 'n in 2..4'})
>>> func(1)
'one'
>>> func(3)
'few'
>>> func = to_python({'one': 'n in 1,11', 'few': 'n in 3..10,13..19'})
>>> func(11)
'one'
>>> func(15)
'few'
Parameters

rule – the rules as list or dict, or a PluralRule object

Raises

RuleError – if the expression is malformed

babel.plural.to_gettext(rule)

The plural rule as gettext expression. The gettext expression is technically limited to integers and returns indices rather than tags.

>>> to_gettext({'one': 'n is 1', 'two': 'n is 2'})
'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);'
Parameters

rule – the rules as list or dict, or a PluralRule object

Raises

RuleError – if the expression is malformed