]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Compile noninteger TR35 operands to zeroes when emitting Gettext 295/head
authorAarni Koskela <akx@iki.fi>
Sun, 20 Dec 2015 21:28:00 +0000 (23:28 +0200)
committerAarni Koskela <akx@iki.fi>
Fri, 22 Jan 2016 18:43:20 +0000 (20:43 +0200)
See http://www.unicode.org/reports/tr35/tr35-numbers.html#Operands
See https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html

Fixes #287

babel/plural.py
tests/test_plural.py

index f2e9f4808c218de618441d3e08cd8df47f2ddacc..9cb1d5cc60a74e686f7470a6b8a8cef7683cc957 100644 (file)
@@ -534,6 +534,12 @@ class _PythonCompiler(_Compiler):
 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)
index fce1b8e0e5a0e628976dfdb62a37093a2c5b49f4..d51efef1e427d82ad2cae9ab30bc182eb635a17d 100644 (file)
@@ -13,7 +13,7 @@
 import unittest
 import pytest
 
-from babel import plural
+from babel import plural, localedata
 from babel._compat import Decimal
 
 
@@ -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)