Trimming Whitespace in Jinja Translations
n
i18n extension, a seemingly simple detail can become a big problem: whitespace. While a few extra spaces or a line break might not matter in your rendered HTML, they can create hard-to-read and error-prone strings for translators. Imagine trying to translate a sentence that’s broken up by multiple lines and indentations. It’s confusing for both a human translator and an automated tool. Fortunately, Jinja provides a simple solution to this common headache: the trimmed and notrimmed modifiers.nnnn
nn
The Challenge with Whitespace
nBy default, Jinja includes all whitespace and linebreaks from your template in the translatable string. This is done to preserve the exact formatting you’ve written. For a multi-line trans block, this can result in a messy translation string that looks something like this:n
" This is an example.\n\n This sentence has extra whitespace.\n"n
nThis formatting is not only difficult for a translator to read but can also lead to inconsistencies in translation files and errors in automated parsing. The goal of internationalization is to make content clean and context-free for the translation system. Extra whitespace and linebreaks get in the way of that.nn
nn
nn
Cleaning Up with `trimmed`
nTo solve this, you can mark a trans block with the trimmed modifier. When you do this, Jinja automatically cleans up the whitespace within the block. It replaces all linebreaks and the whitespace surrounding them with a single space. It also removes any leading and trailing whitespace from the entire block. The result is a clean, single-line string that is perfect for translation.n
n{% trans trimmed book_title=book.title %}nThis is {{ book_title }}.nYou should read it!n{% endtrans %}nnThis simple change ensures that the string sent to your translation file will be a clean, compact phrase, such as: "This is %(book_title)s. You should read it!" The %(book_title)s is the standard gettext placeholder for the variable, which is a key part of the internationalization process. This clean string is much easier for translators to work with and ensures consistency across all languages.nn
nn
nn
Overriding with `notrimmed`
nWhat if you’ve enabled whitespace trimming globally for your Jinja environment, but there’s a specific `trans` block where you need to preserve the formatting? For this, Jinja provides the notrimmed modifier. You can use it to disable trimming for a single block, reverting its behavior to the default. This is perfect for situations where you might have a poetic or stylized block of text where the line breaks and spaces are an essential part of the message.n
n{% trans notrimmed %}nA haiku onnthe silent art ofnclean code.n{% endtrans %}nn
nn
nn
trimmed and notrimmed modifiers are small but powerful tools for your internationalization workflow. They provide a simple, explicit way to manage whitespace, which can be the difference between a smooth translation process and a frustrating one. By using these modifiers, you’re not just creating cleaner translation files; you’re building a more professional, reliable, and maintainable application for a global audience.nnn
