Django offers us the blocktranslate tag to generate expansive or complex translation strings (messages), but it is not without its quirks.

My pet peeve with block translations is that by default, they do not strip leading and trailing whitespace. While there is a case for this behavior, it will quickly lead to unintended results.

Much like with the HTML <pre> element, code indentation will be part of your strings. Changing the indentation level of such a string will change your translation message.

And again, like the <pre> element, you may find yourself ugly-formatting your blocktranslate code to avoid a whole heap of tabs and spaces…

…or will you?

Examples of undesired behavior

To illustrate my previous points, here are some examples of undesired behavior.

Indentation changes

Translation messages show leading and trailing whitespace characters within the tags. This includes, but is not limited to, newlines, tab characters, and spaces.

I use tabs as indents in my code, hence you will see a mix of newlines ("\n") and tab delimiters ("\t").

<p>{% blocktranslate %}
	With leading and trailing whitespace.
{% endblocktranslate %}</p>

<div>
	<p>{% blocktranslate %}
		With leading and trailing whitespace.
	{% endblocktranslate %}</p>
</div>

See how one indentation level change updates the entire message string:

msgid ""
"\n"
"\tWith leading and trailing whitespace.\n"
msgstr ""

msgid ""
"\n"
"\t\tWith leading and trailing whitespace.\n"
"\t"
msgstr ""

Templates change, linters happen, extra elements get added, old elements get removed; you can see how quickly these changes lead to vast changelogs in message files.

For maintainability, this is both risky and a potential time sink.

Avoiding whitespace

Let's get rid of (some of) this extra whitespace by removing indentation and newlines:

<div>
<p>{% blocktranslate %}
Without indentation.
{% endblocktranslate %}</p>
<div>

<div>
<p>{% blocktranslate %}No newlines either.{% endblocktranslate %}</p>
<div>

Which results in messages with fewer whitespace at the cost of legibility of the source code:

msgid ""
"\n"
"Without indentation.\n"
msgstr ""

msgid "No newlines either."
msgstr ""

The trimmed argument to the rescue!

Alright, so one-lining block translations is one way of reducing extra whitespace, but the resulting code quickly turns into an unreadable mess.

Luckily, the blocktranslate accepts an option called trimmed, which does this, according to the docs:

This option will remove newline characters from the beginning and the end of the content of the {% blocktranslate %} tag, replace any whitespace at the beginning and end of a line and merge all lines into one using a space character to separate them.

And suddenly, our problems have been solved:

<p>{% blocktranslate trimmed %}
	Trimmed.
{% endblocktranslate %}</p>

<div>
	<p>{% blocktranslate trimmed %}
		Still trimmed.
	{% endblocktranslate %}</p>
</div>

Whitespace? What whitespace?

msgid "Trimmed."
msgstr ""

msgid "Still trimmed."
msgstr ""

Wrap-up

The trimmed argument is a must-have for blocktranslate in my book.

There may be cases where you need to retain the whitespace in a code block, but more often, you do not. So: try to get this argument into your system.

I even have a code snippet for simple paragraphs:

<p>{% blocktranslate trimmed %}
	[place editor cursor here]
{% endblocktranslate %}</p>

NB. Remember that this argument only removes leading and trailing whitespace from the entire string, not between the lines.

Enjoy!