Skip to content

Commit

Permalink
Compile noninteger TR35 operands to zeroes when emitting Gettext
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 22, 2016
1 parent a4ed4e0 commit e31156f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions babel/plural.py
Expand Up @@ -534,6 +534,12 @@ def compile_relation(self, method, expr, range_list):
class _GettextCompiler(_Compiler):
"""Compile into a gettext plural expression."""

compile_i = _Compiler.compile_n
compile_v = compile_zero
compile_w = compile_zero
compile_f = compile_zero
compile_t = compile_zero

def compile_relation(self, method, expr, range_list):
rv = []
expr = self.compile(expr)
Expand Down
16 changes: 15 additions & 1 deletion tests/test_plural.py
Expand Up @@ -13,7 +13,7 @@
import unittest
import pytest

from babel import plural
from babel import plural, localedata
from babel._compat import Decimal


Expand Down Expand Up @@ -254,3 +254,17 @@ def test_extract_operands(source, n, i, v, w, f, t):
source = Decimal(source) if isinstance(source, str) else source
assert (plural.extract_operands(source) ==
Decimal(n), i, v, w, f, t)


@pytest.mark.parametrize('locale', ('ru', 'pl'))
def test_gettext_compilation(locale):
# Test that new plural form elements introduced in recent CLDR versions
# are compiled "down" to `n` when emitting Gettext rules.
ru_rules = localedata.load(locale)['plural_form'].rules
chars = 'ivwft'
# Test that these rules are valid for this test; i.e. that they contain at least one
# of the gettext-unsupported characters.
assert any((" " + ch + " ") in rule for ch in chars for rule in ru_rules.values())
# Then test that the generated value indeed does not contain these.
ru_rules_gettext = plural.to_gettext(ru_rules)
assert not any(ch in ru_rules_gettext for ch in chars)

0 comments on commit e31156f

Please sign in to comment.