2

I've successfully installed Django's translation system but have run up to what appears to be a design flaw (and hopefully I'm wrong).

It's easy to create translation blocks as such

<p>{% trans "The rain in Spain falls mainly on the plain" %}</p>

But in some cases, I want just part of the sentence to be a link or have different formatting.

<p>The rain in <a href="...">Spain</a> falls <strong>mainly</strong> on the plain</p>

I realize, I could just create translation blocks within those tags as such

<p>
  {% trans "The rain in " %}
  <a href="...">{% trans "Spain" %}</a>
  {% trans " falls mainly " %}
  <strong>{% trans "on the plain" %}</strong>
</p>

But not only does that make it irritating to translators, it forces a word order that is not guaranteed by the target language.

I've looked at more robust translation solutions like https://github.com/mbi/django-rosetta But I don't see anything in the docs about it accounting for this usage. Is there some HTML trick I can use here?

Adam Grant
  • 12,477
  • 10
  • 58
  • 65

1 Answers1

4

Use blocktrans:

<p>
  {% blocktrans %}
  The rain in <a href="...">Spain</a>
  falls mainly <strong>on the plain</strong>
  {% endblocktrans %}
</p>

It allows more complex contents than the simple trans, which can mostly only be used with standard text. Note that reverse url lookups are not supported within the blocktrans tag, so you'll have to save the URLs to a variable before entering the blocktrans, as shown in this answer.

Community
  • 1
  • 1
voithos
  • 68,482
  • 12
  • 101
  • 116