Andrea
Andrea

Reputation: 658

Escape quotes in django's trans method

I'm trying to internationalize my webpages by using the {% trans "string" %} method in django.

However I get an error when I try to use quotes in the string. I've already tried to escape them with a backslash with no luck.

How can I escape something like this:

{% trans "foo's is like "foo"." %}

As I said, this doesn't work:

{% trans "foo\'s is like \"foo\"." %}

Any idea?

Thanks

Upvotes: 6

Views: 4511

Answers (3)

Flimm
Flimm

Reputation: 151203

Note that the {% translate %} and the {% blocktranslate %} template tags are generally meant to be used with HTML. So you could simply escape them as you would in HTML:

{% blocktranslate %}foo's is like "foo"{% endblocktranslate %}
<br>
{% trans "foo's is like &quot;foo&quot;" %}

Assuming that the language is set to the default language (English), this would send this HTML to the browser:

foo's is like &quot;foo&quot;
<br>
foo's is like &quot;foo&quot;

The user would see on the screen this text:

foo's is like "foo"

foo's is like "foo"

By the way, the template tag {% translate %} is just an alias for {% trans %}, and {% blocktranslate %} is just an alias for {% blocktrans %}.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799190

Use blocktrans instead.

Example
{% blocktranslate %}Just an example block to translate with {{value}} and text.{% endblocktranslate %}

Upvotes: 13

StefanNch
StefanNch

Reputation: 2609

{{ _("foo's is like \"foo\".") }}

Upvotes: 0

Related Questions