user1011444
user1011444

Reputation: 1409

How to translate a message/variable in Django templates?

I have messages.success(request, 'sign-up-success') in my view (I wrote a generic name, as I'm going to translate it later anyway), and display it with {{ message }} in my template. I want to be able to translate, so I tried {% blocktrans %}{{ message }}{% endblocktrans %} but this will translate any message, whether it's 'sign-up-failed' or whatever, to the msgstr I define in my django.po file.

How do I translate a message, which is a variable?

Thanks a lot.

Upvotes: 3

Views: 2858

Answers (2)

user188867
user188867

Reputation: 31

Important thing is to have proper order in middleware in settings.py

  MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
    )

Upvotes: 0

Ponytech
Ponytech

Reputation: 1684

You pointed it out: you can not translate a variable.

You have to translate the text where you define it:

from django.utils.translation import ugettext as _
messages.success(request, _('sign-up-success'))

Then follow the standard translation process : https://docs.djangoproject.com/en/dev/topics/i18n/translation/

Upvotes: 3

Related Questions