django translate variable content in template
Solution 1
You can use the blocktrans
template tag in this case:
{% blocktrans %} This is the title: {{ myvar }} {% endblocktrans %}
Solution 2
{% trans myvar %}
just works. So check your PO file to make sure that the value of myvar
is in PO msgid.
<title>{% trans myvar %}</title>
For example if myvar
contains "Some Publisher"
you can write the following in the PO file:
msgid "Some Publisher"
msgstr "কিছু প্রকাশক"
Also make sure you have ran:
python manage.py compilemessages
Solution 3
Django can't guess what is in that variable, so you have to translate it yourself by adding both the english (msgid
) and the localized (msgstr
) strings.
Solution 4
My experience here is that variable translation does not work in templates on its own. However I came to a suitable solution when the content of the variables is known (I mean that they are not free text, but a set of choices you set in the database).
You need to force the translation in the view or in a filter tag.
To sum up:
- Use
blocktrans
in your templates - Force variables to be translated
- You either set variables in context that are already marked for transtalion
- or use a filter to translate them
- Generate translations in
.po
file
The story is like this:
views.py
def my_view(request):
return render(request, 'i18n_test.html', {'salutation':"Hola"})
templates/i18n_test.html
...
{% blocktrans %}{{ salutation }}{% endblocktrans %}
...
And when I render the template it always shows Hola whichever the current language is.
To force the translation, in the view we need to use ugettext.
def my_view(request):
return render(request, 'i18n_test.html', {'salutation':ugettext("Hola")})
However it is not always possible to access the view. So I prefer to use a filter like this.
templatetags/i18n_extras.py
@register.filter(name='translate')
def translate(text):
try:
return ugettext(text)
And the template becomes
...
{% blocktrans s=salutation|translate %}{{ s }}{% endblocktrans %}
...
And produces Hola, Hello, Ciao, Salut depending on the current language.
The disadvantage (as pointed out in the docs ) is that makemessages
does not automatically include these translations, so we need to include them manually. In django.po file:
locales/en/django.po
...
msgid "Hola"
msgstr "Hello"
...
Solution 5
You can translate the variable in the python code like here for settings.SITE_NAME
:
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
def processor004(request):
my_dict = {
'site_id004': settings.SITE_ID,
'site_name004': _(settings.SITE_NAME),
'installed_apps004': settings.INSTALLED_APPS,
'embedded_widget004': settings.EMBEDDED_WIDGET,
'base_template004': settings.BASE_TEMPLATE,
}
return my_dict
thoslin
Updated on July 09, 2022Comments
-
thoslin almost 2 years
I'm using {% trans %} template tag. Django docs say:
The {% trans %} template tag translates either a constant string (enclosed in single or double quotes) or variable content:
{% trans "This is the title." %} {% trans myvar %}
https://docs.djangoproject.com/en/1.3/topics/i18n/internationalization/#trans-template-tag
I found it impossible to do {% trans myvar %} because myvar simply doesn't show up in django.po file after running makemessages command.
Am I using it wrong? Could some help me with this?
-
thoslin almost 13 yearsDo I need to specify the file and line number like this:#: templates/foo.html:45 msgid "myvar" msgstr "" ?
-
Gabi Purcaru almost 13 yearsYou don't need too, because those are actually comments, but you might want to.
-
thoslin almost 13 yearsI have added msgid and msgstr in django.po and run compilemessages.But it doesn't seem to work. The varibles haven't been translated after changing locale. Am I missing something?
-
Patrik Beck over 10 yearsI have exactly this problem, however, django compilemessages comments out the msgid and msgstr I added. I find this answer very misleading.
-
miguelfg almost 10 yearsIt doesn't work to me either, i think database values brought to a template as {% blocktrans with aapptype=aapp.type %} {{ aapptype }} {% endblocktrans %} are not translated at all.
-
Moisés over 9 yearsIt worked great for me, just avoid running makemessages again, as those strings won't be found and will be commented.
-
Moisés over 9 yearsAnd if you need to run makemessages again, just uncomment the translations you need afterwards.
-
Moisés over 9 yearsOr create another dummy template file with all the different possible values of your variables, using {% trans "content" %}. In this way, makemessages will add them automatically to your django.po file. Not the most elegant solution but it works.
-
Eduard Luca over 9 yearsShouldn't
noop
be used in this case? From the docs,noop
creates a translation string in the po file, which is not directly used anywhere on the site. This way, you don't need to avoid runningmakemessages
. -
Mohini almost 9 yearsBut when I am using this blocktrans to translate database values, but in that case its not working, will you tell me solution, please ?
-
Lalaphoon over 3 yearsI might be wrong. But you might have to write this way right? : {% bolcktrans myvar=myvar %}