Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: Translation with variables inside

Tags:

django

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...

like image 402
Oleg Tarasenko Avatar asked Jul 29 '13 09:07

Oleg Tarasenko


1 Answers

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}
like image 166
m01 Avatar answered Sep 29 '22 14:09

m01