Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django i18n blocktrans vs trans

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

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

{% trans 'My Text' %}
like image 454
tzenderman Avatar asked Jul 12 '13 18:07

tzenderman


People also ask

What is Blocktrans?

The {% blocktrans %} template tag allows you to mark content that includes literals and variable content using placeholders.

What is trans In Django template?

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.

What is internationalization in Django?

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.

How does Django translation work?

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.


1 Answers

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'

like image 107
JcKelley Avatar answered Oct 14 '22 01:10

JcKelley