In Django templates, what exactly is the difference between these two:
{% blocktrans %}My Text{% endblocktrans %}
{% trans 'My Text' %}
The {% blocktrans %} template tag allows you to mark content that includes literals and variable content using placeholders.
The {% trans %} template tag The {% trans %} tag is useful for simple translation strings, but it cannot handle content for translation that includes variables. Get Django 2 by Example now with the O'Reilly learning platform.
The goal of internationalization and localization is to allow a single web application to offer its content in languages and formats tailored to the audience. Django has full support for translation of text, formatting of dates, times and numbers, and time zones.
Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language. Once the translators have filled in the message file, it must be compiled.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With