Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_() or {% trans %} in Django templates?

In Django templates, you can use either {{ _("Hello World") }} or {% trans "Hello World" %} to mark strings to be translated. In docs, the “official” approach seems to be the {% trans %} tag, but the _() syntax is mentioned too once.

How these approaches differ (except syntax) and why should be one preferable rather than the other?

One difference is that you obviously can't use {% trans %} with tags and filters. But does that mean that I can just use _() everywhere, like {{ _("String") }}? It works and looks much cleaner and more consistent than using {% trans "String" %} with standalone strings and _() with tags and filters.

like image 662
Anton Strogonoff Avatar asked Sep 21 '11 07:09

Anton Strogonoff


People also ask

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 does {% include %} do in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What is {% block content %} in Django?

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.

What does {% include %} do?

Q13:-What does {% include %} does? It will include another template. It will include content from another template having the same templates defined.


1 Answers

So it seems that there's technically no difference as of Django 1.5. Template engine internally marks a variable for translation (by setting its translate attribute) in two cases:

  • when you do {% trans VAR %} (see TranslateNode), or
  • if the name of a variable starts with _( and ends with ) (see Variable.__init__).

Later, when the variable is being resolved, Django wraps it with ugettext or pgettext if it sees the translate attribute.

However, as can be seen from source code, there are some flexibility considerations in favor of {% trans %} tag:

  • you can do {% trans "String" noop %}, which will put the string for translation into .po files, but won't actually translate the output when rendering (no internal translate attribute on variable, no ugettext call);
  • you can specify message context*, like {% trans "May" context "verb" %};
  • you can put translated message into a variable for later use*, like {% trans "String" as translated_string %}.

* As of Django 1.4.

Please feel free to correct me or post a better answer in case I'm missing anything.

like image 100
Anton Strogonoff Avatar answered Sep 22 '22 05:09

Anton Strogonoff