Django: Translation with variables inside

I have the following code snippet:

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 is not working ...

+1
source share
1 answer

Perhaps try using string placeholders - from the django documentation :

The lines you pass in _ () or ugettext () can take place as specified using the standard default interpolation syntax called Python. 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 get:

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

Source: https://habr.com/ru/post/1545472/


All Articles