summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-07-14 17:35:00 -0400
committerChris McDonough <chrism@plope.com>2011-07-14 17:35:00 -0400
commit5b5cd6fa80421b594fa14a75f3daf6a5703f1898 (patch)
treea390f0e30c36e58ae04748de745cdba1fd5602e3
parent10408c799257d6727c968d63cf439b1ebfbfd335 (diff)
downloadpyramid-5b5cd6fa80421b594fa14a75f3daf6a5703f1898.tar.gz
pyramid-5b5cd6fa80421b594fa14a75f3daf6a5703f1898.tar.bz2
pyramid-5b5cd6fa80421b594fa14a75f3daf6a5703f1898.zip
- Without a mo-file loaded for the combination of domain/locale,
``pyramid.i18n.Localizer.pluralize`` run using that domain/locale combination raised an inscrutable "translations object has no attr 'plural' error. Now, instead it "works" (it uses a germanic pluralization by default). This is not the "right" thing really (it's nonsensical to try to pluralize something without translations for that locale/domain available), but it matches the behavior of ``pyramid.i18n.Localizer.translate`` so it's at least consistent; see https://github.com/Pylons/pyramid/issues/235. Closes #235.
-rw-r--r--CHANGES.txt12
-rw-r--r--pyramid/i18n.py5
-rw-r--r--pyramid/tests/test_i18n.py19
3 files changed, 36 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 1a0573278..02c2da33d 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -24,6 +24,18 @@ Deprecations
it is used. You should replace it with a reference to
``pyramid.static.static_view`` with the ``use_subpath=True`` argument.
+Bug Fixes
+---------
+
+- Without a mo-file loaded for the combination of domain/locale,
+ ``pyramid.i18n.Localizer.pluralize`` run using that domain/locale
+ combination raised an inscrutable "translations object has no attr 'plural'
+ error. Now, instead it "works" (it uses a germanic pluralization by
+ default). This is not the "right" thing really (it's nonsensical to try to
+ pluralize something without translations for that locale/domain available),
+ but it matches the behavior of ``pyramid.i18n.Localizer.translate`` so it's
+ at least consistent; see https://github.com/Pylons/pyramid/issues/235.
+
1.1b2 (2011-07-13)
==================
diff --git a/pyramid/i18n.py b/pyramid/i18n.py
index 7bc096bb1..4b34534af 100644
--- a/pyramid/i18n.py
+++ b/pyramid/i18n.py
@@ -214,6 +214,11 @@ class Translations(gettext.GNUTranslations, object):
:param fileobj: the file-like object the translation should be read
from
"""
+ # germanic plural by default; self.plural will be overwritten by
+ # GNUTranslations._parse (called as a side effect if fileobj is
+ # passed to GNUTranslations.__init__) with a "real" self.plural for
+ # this domain; see https://github.com/Pylons/pyramid/issues/235
+ self.plural = lambda n: int(n != 1)
gettext.GNUTranslations.__init__(self, fp=fileobj)
self.files = filter(None, [getattr(fileobj, 'name', None)])
self.domain = domain
diff --git a/pyramid/tests/test_i18n.py b/pyramid/tests/test_i18n.py
index 97117a8cd..fcc41b08e 100644
--- a/pyramid/tests/test_i18n.py
+++ b/pyramid/tests/test_i18n.py
@@ -65,6 +65,19 @@ class TestLocalizer(unittest.TestCase):
)
self.assertTrue(localizer.pluralizer is pluralizer)
+ def test_pluralize_default_translations(self):
+ # test that even without message ids loaded that
+ # "localizer.pluralize" "works" instead of raising an inscrutable
+ # "translations object has no attr 'plural' error; see
+ # see https://github.com/Pylons/pyramid/issues/235
+ from pyramid.i18n import Translations
+ translations = Translations()
+ translations._catalog = {}
+ localizer = self._makeOne(None, translations)
+ result = localizer.pluralize('singular', 'plural', 2, domain='1',
+ mapping={})
+ self.assertEqual(result, 'plural')
+
class Test_negotiate_locale_name(unittest.TestCase):
def setUp(self):
cleanUp()
@@ -469,6 +482,12 @@ class TestTranslations(unittest.TestCase):
self.assertEqual(t.dungettext('messages', 'foo1', 'foos1', 1), 'Voh1')
self.assertEqual(t.dungettext('messages1', 'foo1', 'foos1', 1), 'VohD1')
+ def test_default_germanic_pluralization(self):
+ t = self._getTargetClass()()
+ t._catalog = {}
+ result = t.dungettext('messages', 'foo1', 'foos1', 2)
+ self.assertEqual(result, 'foos1')
+
class DummyRequest(object):
def __init__(self):