Search code examples
pythondjangodjango-templatesdjango-i18n

Translate string in template with dynamic value


In my template file I have something like this:

{% blocktrans %}There are {{news|length}} news{% endblocktrans %}

But translation always misses the {{news|length}} in it (prints out 'There are news').

My django.po file is auto generated via django-admin.py makemessages --all

msgid "There are %(news|length)s news"
msgstr "%(news|length)s tane haber var"

I know that I can try to pass this string in views.py but is there any way to do it in templates or what am I doing wrong?


Solution

  • You need to use the filter within the blocktrans tag itself.

    {% blocktrans with news=news|length %}There are {{ news }} news{% endblocktrans %}