module documentation

CLDR Plural support. See UTS #35.

Unknown Field: copyright
  1. 2013-2021 by the Babel Team.
Unknown Field: license
BSD, see LICENSE for more details.
Class ​Plural​Rule No summary
Function to​_gettext The plural rule as gettext expression. The gettext expression is technically limited to integers and returns indices rather than tags.
Function to​_javascript Convert a list/dict of rules or a PluralRule object into a JavaScript function. This function depends on no external library:
Function to​_python 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:
Variable compile​_zero Undocumented
Class _​Compiler The compilers are able to transform the expressions into multiple output formats.
Class _​Gettext​Compiler Compile into a gettext plural expression.
Class _​Java​Script​Compiler Compiles the expression to plain of JavaScript.
Class _​Parser Internal parser. This class can translate a single rule into an abstract tree of tuples. It implements the following grammar:
Class _​Python​Compiler Compiles an expression to Python.
Class _​Unicode​Compiler Returns a unicode pluralization rule again.
Class ​Rule​Error Raised if a rule is malformed.
Function ​_binary​_compiler Compiler factory for the _Compiler.
Function ​_unary​_compiler Compiler factory for the _Compiler.
Function cldr​_modulo Javaish modulo. This modulo operator returns the value with the sign of the dividend rather than the divisor like Python does:
Function extract​_operands Extract operands from a decimal, a float or an int, according to `CLDR rules`_.
Function ident​_node Undocumented
Function in​_range​_list Integer range list test. This is the callback for the "in" operator of the UTS #35 pluralization rule language:
Function negate Undocumented
Function range​_list​_node Undocumented
Function skip​_token Undocumented
Function test​_next​_token Undocumented
Function tokenize​_rule Undocumented
Function value​_node Undocumented
Function within​_range​_list Float range test. This is the callback for the "within" operator of the UTS #35 pluralization rule language:
Constant ​_RULES Undocumented
Constant ​_VARS Undocumented
Variable ​_fallback​_tag Undocumented
Variable ​_plural​_tags Undocumented
def 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
rulethe rules as list or dict, or a PluralRule object
Raises
RuleErrorif the expression is malformed
def 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
rulethe rules as list or dict, or a PluralRule object
Raises
RuleErrorif the expression is malformed
def 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
rulethe rules as list or dict, or a PluralRule object
Raises
RuleErrorif the expression is malformed
compile_zero =

Undocumented

def _binary_compiler(tmpl):
Compiler factory for the _Compiler.
def _unary_compiler(tmpl):
Compiler factory for the _Compiler.
def cldr_modulo(a, b):

Javaish modulo. This modulo operator returns the value with the sign of the dividend rather than the divisor like Python does:

>>> cldr_modulo(-3, 5)
-3
>>> cldr_modulo(-3, -5)
-3
>>> cldr_modulo(3, 5)
3
def extract_operands(source):

Extract operands from a decimal, a float or an int, according to CLDR rules.

The result is a 6-tuple (n, i, v, w, f, t), where those symbols are as follows:

Symbol Value
n absolute value of the source number (integer and decimals).
i integer digits of n.
v number of visible fraction digits in n, with trailing zeros.
w number of visible fraction digits in n, without trailing zeros.
f visible fractional digits in n, with trailing zeros.
t visible fractional digits in n, without trailing zeros.
Parameters
source:int|float|decimal.DecimalA real number
Returns
tuple[decimal.Decimal, int, int, int, int, int]A n-i-v-w-f-t tuple
def ident_node(name):

Undocumented

def in_range_list(num, range_list):

Integer range list test. This is the callback for the "in" operator of the UTS #35 pluralization rule language:

>>> in_range_list(1, [(1, 3)])
True
>>> in_range_list(3, [(1, 3)])
True
>>> in_range_list(3, [(1, 3), (5, 8)])
True
>>> in_range_list(1.2, [(1, 4)])
False
>>> in_range_list(10, [(1, 4)])
False
>>> in_range_list(10, [(1, 4), (6, 8)])
False
def negate(rv):

Undocumented

def range_list_node(range_list):

Undocumented

def skip_token(tokens, type_, value=None):

Undocumented

def test_next_token(tokens, type_, value=None):

Undocumented

def tokenize_rule(s):

Undocumented

def value_node(value):

Undocumented

def within_range_list(num, range_list):

Float range test. This is the callback for the "within" operator of the UTS #35 pluralization rule language:

>>> within_range_list(1, [(1, 3)])
True
>>> within_range_list(1.0, [(1, 3)])
True
>>> within_range_list(1.2, [(1, 4)])
True
>>> within_range_list(8.8, [(1, 4), (7, 15)])
True
>>> within_range_list(10, [(1, 4)])
False
>>> within_range_list(10.5, [(1, 4), (20, 30)])
False
_RULES =

Undocumented

Value
[(None, re.compile(r'\s+', re.UNICODE)),
 ('word',
  re.compile("""\\b(and|or|is|(?:with)?in|not|mod|[{0}])\\b""".format(_VARS))),
 ('value', re.compile(r'\d+')),
 ('symbol', re.compile(r'%|,|!=|=')),
 ('ellipsis', re.compile(r'\.{2,3}|\u2026', re.UNICODE))]
_VARS: str =

Undocumented

Value
'nivwft'
_fallback_tag: str =

Undocumented

_plural_tags: tuple[str, ...] =

Undocumented