简体   繁体   中英

Django: How to concatenate translation and constant string in template

I'd like to pass a translation with variable to template tag. I'm trying to do it this way:

{% my_tag _("Entries for tag")|add:" '"|add:tag|add:"'" %}

But this does not work. Is there any solution?

<span>{% my_tag _("Entries for tag") %} '{{tag}}'</span>

One solution would probably be:

{% blocktrans with tag=tag %}
    <span>Entries for tag</span> '{{ tag }}'
{% endblocktrans %}

But putting HTML inside translated strings is not the best idea. What is much worse though is assuming that you can just concatenate a translation of an English string with another string and get a valid sentence. You can never assume that position of {{ tag }} withing the sentence will be the same in other languages. Some language will probably need the translation to be Lorem ipsum „%(tag)s” dolor sit .

But it seems you're trying to apply some HTML dynamically. Maybe what you really want is to ugettext('Entries for tag %(tag)s') inside my_tag and parse it accordingly (say find %(tag)s and add some HTML around it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM