Ev.
Ev.

Reputation: 1567

compile messages django <string> in <lambda>, line 1 Django internationalization

I am having the following error when I try to access a certain page of my project. I believe it is due to the plural I have implemented on that line. The puzzle is that it doesn't happen to all languages I have in the project, only one.

Console

  File "\lib\site-packages\django\template\base.py", line 959, in render_annotated
    return self.render(context)
  File "\lib\site-packages\django\templatetags\i18n.py", line 149, in render
    result = translation.ungettext(singular, plural, count)
  File "\lib\site-packages\django\utils\translation\__init__.py", line 89, in ungettext
    return _trans.ungettext(singular, plural, number)
  File "\lib\site-packages\django\utils\translation\trans_real.py", line 362, in ngettext
    return do_ntranslate(singular, plural, number, 'ngettext')
  File "\lib\site-packages\django\utils\translation\trans_real.py", line 349, in do_ntranslate
    return getattr(t, translation_function)(singular, plural, number)
  File "C:\Python34\Lib\gettext.py", line 344, in ngettext
    tmsg = self._catalog[(msgid1, self.plural(n))]
  File "<string>", line 1, in <lambda>
TypeError: unorderable types: str() > int()

Template.py

{% cache cache_hourly_timeout visualizations %}
    {% blocktrans count number_views=advertisement.get_views|intcomma %}<strong>{{ number_views }}</strong> view{% plural %}<strong>{{ number_views }}</strong> views{% endblocktrans %}
{% endcache %}

Upvotes: 0

Views: 224

Answers (1)

Michel Rugenbrink
Michel Rugenbrink

Reputation: 446

We actually had the same problem:

Template

{% blocktrans count count=paginator.count %}
    single text
{% plural %}
    plural text
{% endblocktrans %}

This actually triggers the error you're describing. To us it only happened in French and not in any other language.

Reproducing in Django Shell

In a django shell we can replicate this bug using the following lines:

    from django.utils.translation import _trans

    _trans.ngettext("test", "tests", 0)  # 'tests'
    _trans.ngettext("test", "tests", 1)  # 'test'
    _trans.ngettext("test", "tests", 2)  # 'tests'
    _trans.ngettext("test", "tests", '')  # 'tests'
    _trans.activate('fr')  # Changing the language to French
    _trans.ngettext("test", "tests", '')
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "/home/vagrant/.virtualenvs/talentindex/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 369, in ngettext
        return do_ntranslate(singular, plural, number, 'ngettext')
      File "/home/vagrant/.virtualenvs/talentindex/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 356, in do_ntranslate
        return getattr(t, translation_function)(singular, plural, number)
      File "/usr/lib64/python3.4/gettext.py", line 341, in ngettext
        tmsg = self._catalog[(msgid1, self.plural(n))]
      File "<string>", line 1, in <lambda>
    TypeError: unorderable types: str() > int()

Workaround

A workaround to this issue is to make sure that the counter is never None or an empty string. Since on an empty string paginator object in the template actually is None and None.count would result in an empty string it caused the error.

Using {% blocktrans count count=paginator.count|default:0 %} solves it.

Hope this helps anyone.

Upvotes: 2

Related Questions