Oleg Tarasenko
Oleg Tarasenko

Reputation: 9610

django: Translation with variables inside

I have the following piece of code:

from django.utils.translation import ugettext as _
task = _('You have %s friends') %(c1.task)
// This is translation
#: compositions/views.py:69
#, fuzzy, python-format
msgid "You have %s friends"
msgstr "У вас %s друга"

But for some reason this msgstr does not work...

Upvotes: 7

Views: 7239

Answers (1)

m01
m01

Reputation: 9395

Maybe try using string placeholders - from the django documentation:

The strings you pass to _() or ugettext() can take placeholders, specified with Python’s standard named-string interpolation syntax. Example:

def my_view(request, m, d):
    output = _('Today is %(month)s %(day)s.') % {'month': m, 'day': d}
    return HttpResponse(output)

Applying this to your example, you'd get:

task = _('You have %(num_friends)s friends') % {'num_friends': c1.task}

Upvotes: 8

Related Questions