Search code examples
djangointernationalizationdjango-i18n

Django i18n blocktrans vs trans


In Django templates, what exactly is the difference between these two:

{% blocktrans %}My Text{% endblocktrans %}

{% trans 'My Text' %}

Solution

  • From Django Docs

    Trans template tag

    The {% trans %} template tag translates either a constant string (enclosed in single or >double quotes) or variable content:

    With a Trans tag, you are limited to a single constant string or variable. So you would have to use

    {# These Would Work! #}
    title>{% trans "This is the title." %}</title>
    <title>{% trans myvar %}</title>
    

    But could not use

    {%trans "This is my title {{ myvar }}" %}
    

    Blocktrans template tag

    Contrarily to the trans tag, the blocktrans tag allows you to mark complex sentences consisting of literals and variable content for translation by making use of placeholders:

    With a Blocktrans, this kind of code is possible:

        {% blocktrans with book_t=book|title author_t=author|title %}
           This is {{ book_t }} by {{ author_t }}
        {% endblocktrans %}
    

    So Blocktrans is going to allow you to be a bit more complex and through in your output.

    But to answer your question literally: not much. Except for the presentation style, both will be sent to the translator as the string 'My Text'