6

I have a template, in which I want to translate a string.

{% blocktrans with "www.mywebsite.com" as website_name %}footer-slogan{{ website_name }}{% endblocktrans %}

I've generated my po file, in which I've translated the string as follow :

msgid "footer-slogan %(website_name)s"
msgstr "This is a test %(website_name)s"

On my generated html file, I get this untranslated element :

footer-slogan www.mywebsite.com

If I remove the variable from the translated string, it works :

msgid "footer-slogan %(website_name)s"
msgstr "This is a test"

I've even tried to remove the variable from the source translation but keeping the variable in the translated string, the issue is the same :

template.html
{% blocktrans with "www.mywebsite.com" as website_name %}footer-slogan{% endblocktrans %}

django.po
msgid "footer-slogan"
msgstr "This is a test %(website_name)s"

I'd prefer to be able to set the variable only on the translated string.

What I'm doing wrong on the translated string?

Fab
  • 97
  • 2
  • 11

2 Answers2

2

You can use it this way:

{% blocktrans %} 
    {% with website_name="www.mywebsite.com" %}
        {% trans 'footer-slogan{{ website_name }}' %}
    {% endwith %}   
{% endblocktrans %}
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
  • I've tried what you've suggested, it's still not working. – Fab Sep 27 '17 at 06:25
  • @Fab, There was a `typo` mistake. Try it now and let me know about it. – Astik Anand Sep 27 '17 at 06:52
  • I've seen your type previously. Even after correcting it, it doesn't work. – Fab Sep 27 '17 at 07:33
  • I've finally find the issue. It was because I was displaying an other language, which wasn't translated yet. And I didn't find in the documentation but for people who are wondering if it's possible to use a variable inside the po file, which is not used in the original translation, it's not possible. It has to be specified. – Fab Oct 02 '17 at 17:39
  • It looks like on the view, it's not mandatory to provide the variable inside the translation ID. But in the template it is. – Fab Oct 02 '17 at 17:46
  • @Fab, Thanks for letting me know. – Astik Anand Oct 02 '17 at 18:05
  • But blocktrans does not allow other block tags in it. – Damiaan Apr 17 '23 at 08:17
2

Little late to answer but for other people that come seeking

`{% blocktrans with site_name="xyz" %}{{ site_name }} - Your account 
 has been successfully created and activated!{% endblocktrans %}`
Sachin
  • 103
  • 7