Assign blocktrans output to a variable

I would like to assign the output of the blocktrans block to a template variable.

You can do this using the trans tag like this:

{% trans "Some text to translate" as foo %} 

However, you cannot do the same with the blocktrans tag. I do not see in the documentation that you can do this.

+7
source share
2 answers

Django 1.9 added the asvar component to blocktrans . Example from the docs :

 {% blocktrans asvar the_title %}The title is {{ title }}.{% endblocktrans %} <title>{{ the_title }}</title> <meta name="description" content="{{ the_title }}"> 

Unfortunately, it seems that before this was added, you needed a different way to do this.

+6
source

This is not possible with the blocktrans tag.

However, you can use this captureas templatetag to capture blocktrans output.

 {% captureas trans_value %} {% blocktrans %} This has some stuff in it which will be translated {{ foo }}. {% endblocktrans %} {% endcaptureas %} {{ trans_value }} 
+4
source

All Articles