Opened 8 years ago

Closed 8 years ago

#26705 closed Bug (fixed)

DjangoTranslation instance has no attribute 'plural'

Reported by: Thejaswi Puthraya Owned by: nobody
Component: Internationalization Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you activate a language that is not supported by django, an error is raised during the internationalization. Here is a test to reproduce the error:

import datetime

from django.utils.translation import activate
from django.utils.timesince import timesince
from django.test import TestCase
from django.conf import settings

# Create your tests here.
class TmpTestCase(TestCase):
    def test_lang(self):
        today = datetime.datetime.now()
        yesterday = today - datetime.timedelta(minutes=5)

        tt = timesince(yesterday, now=today)
        self.assertEqual(tt, u"5\xa0minutes")

        activate("hi")
        tt = timesince(yesterday, now=today)
        self.assertEqual(tt, u"5\xa0minutes")

        activate("as")
        tt = timesince(yesterday, now=today)
        self.assertEqual(tt, u"5\xa0minutes")

as is Assamese, a language that is currently not in django.

The error is

AttributeError                            Traceback (most recent call last)
<ipython-input-22-e5b829f6150e> in <module>()
----> 1 capfirst(ngettext(u"test", u"tests", 2))

/home/ubuntu/wg_env/local/lib/python2.7/site-packages/django/utils/translation/__init__.pyc in ngettext(singular, plural, number)
     78 
     79 def ngettext(singular, plural, number):
---> 80     return _trans.ngettext(singular, plural, number)
     81 
     82 

/home/ubuntu/wg_env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.pyc in ngettext(singular, plural, number)
    369     Returns a string on Python 3 and an UTF-8-encoded bytestring on Python 2.
    370     """
--> 371     return do_ntranslate(singular, plural, number, 'ngettext')
    372 
    373 if six.PY3:

/home/ubuntu/wg_env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.pyc in do_ntranslate(singular, plural, number, translation_function)
    356     t = getattr(_active, "value", None)
    357     if t is not None:
--> 358         return getattr(t, translation_function)(singular, plural, number)
    359     if _default is None:
    360         _default = translation(settings.LANGUAGE_CODE)

/usr/lib/python2.7/gettext.pyc in ngettext(self, msgid1, msgid2, n)
    368     def ngettext(self, msgid1, msgid2, n):
    369         try:
--> 370             tmsg = self._catalog[(msgid1, self.plural(n))]
    371             if self._output_charset:
    372                 return tmsg.encode(self._output_charset)

AttributeError: DjangoTranslation instance has no attribute 'plural'

Ideally, there shouldn't be an error.

Change History (6)

comment:1 by Claude Paroz, 8 years ago

Triage Stage: UnreviewedAccepted
Version: 1.8master

We could copy this line from the gettext module in __init__:
self.plural = lambda n: int(n != 1) # germanic plural by default

comment:2 by Thejaswi Puthraya, 8 years ago

A patch along with test is available at https://github.com/theju/django/tree/ticket_26705
I have also attached a pull request at https://github.com/django/django/pull/6717

comment:3 by Thejaswi Puthraya, 8 years ago

Has patch: set

comment:4 by Claude Paroz, 8 years ago

Patch needs improvement: set

comment:5 by Tim Graham, 8 years ago

Patch needs improvement: unset

comment:6 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In c8d2120b:

Fixed #26705 -- Fixed plural versions of languages not supported by Django.

Note: See TracTickets for help on using tickets.
Back to Top