I am just getting started with Django internationalization and trying to understand the best practices for using {% blocktrans %}
. Is it preferable to use one {% blocktrans %}
for each paragraph, or should I have one big {% blocktrans %}
that contains many paragraphs?
Having one big {% blocktrans %}
is faster and makes my template look cleaner, but my concern is that:
<p>...</p>
) to become part of the translation stringmsgid
would change, which seems like it could affect the other paragraphs. If I have smaller blocks, the changes would be more isolated (I suppose).msgid
.I am also wondering about formatting. Are there any complications to having line breaks inside a {% blocktrans %}
? Or having leading spaces? e.g.:
{% blocktrans %}
You have {{ num_messages }} messages.
Another sentence.
{% blocktrans %}
Any recommendations are welcome.
Introducing {% block %} The block tag is used to define a block that can be overridden by child templates. In other words, when you define a block in the base template, you're saying that this area will be populated with content from a different, child template file.
The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.
gettext_lazy is a callable within the django. utils. translation module of the Django project.
Multiple small {% blocktrans %}
blocks are beneficial for various reasons:
Each translatable string ends up in the translation files and these files should be translatable by people that speak the language. They should not have to deal with correctness of HTML tags but they should purely translate a few sentences to that language. Minor markup is fine but not the HTML of the entire page.
You can also think of it this way: the less markup in the translatable strings, the less chances for errors by translators (who may or may not have a technical background).
If a huge translation block changes then all translations need to be done again by each of the translators. If you use small translatable blocks then you can reuse most of the existing translated paragraphs / text and you only need to get updated translations for the parts that actually changed.
So to answer your question: a blocktrans tag per paragraph is a better choice. If you end up changing a paragraph then only that paragraph needs to be checked again by a translator.
Regarding whitespace and newlines: by default these will end up in the PO translation files. In Django 1.7 the blocktrans will have a trimmed
option which removes whitespace and newlines (source):
This option will remove newline characters from the beginning and the end of the content of the {% blocktrans %} 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. This is quite useful for indenting the content of a {% blocktrans %} tag without having the indentation characters end up in the corresponding entry in the PO file, which makes the translation process easier.
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