This question already has answers here:
Handling percent-sign (%) in Django blocktrans tags
(3 answers)
Closed 7 years ago.
I have a Django 1.6 template with the following content (base language is german, the translation is in english):
{% trans "My App" as appname %}
{% blocktrans with appname=appname %}Garantie: '{{ appname }}' ist 100%% gratis!{% endblocktrans %}
{% trans "Hallo {firstWordInName}," %}
When generating the .po files it looks like that:
msgid "Garantie: '%(appname)s' ist 100%% gratis!"
msgstr "We assure you: '%(appname)s' is 100%% free of charge!"
msgid "Hallo {firstWordInName},"
msgstr "Hello {firstWordInName},"
But when rendering the django template only the string "Hallo {firstWordInName}," is translated. The other one keeps being german. What is the reason for that? I think django translations for strings with percentage signs are really broken....
Thanks in advance! Any help is appreciated!
I found a simple solution, maybe even better than the one suggested in Handling percent-sign (%) in Django blocktrans tags
{% blocktrans with percent="%" %} This is 100{{ percent }} working! {% endblocktrans %}
Related
I am writing a translation program and I am trying to implement a functionality where, If the translation string in the .po file is empty ( msgstr "" ) the parent elements visibility will be set to none.
As an example, if I have this template code:
{% language target_language %}
<td><p> {% translate Hello %} </p></td>
{% endlanguage %}
If the target languages po file should look like this:
#: translation/forms.py:10
msgid "Hello"
msgstr ""
then I would like to set:
{% language target_language %}
<td style="display: none"><p> {% translate Hello %} </p></td>
{% endlanguage %}
How could I go about implementing this? I have worked myself through the Django internationalization but haven't found anything to point me in the right direction. I'm using Django 3.1 and Python 3.8.
Best Regards
Max
I use i18n in my Jinja2 templates and everything works fine for plain text.
But I can't find any info how to use translations with variables, which I can't avoid using. Here is an example:
{% set words = ["Hello", "world"] %}
{% for word in words %}
{{ _(word) }}
{% endfor %}
What should I do to get "Hello" and "world" in my .po file?
It turned out I needed to use Babel for jinja2:
http://jinja.pocoo.org/docs/2.10/integration/
Posting here, because someone can be in the same trouble
I have this block in my HTML
...
<a class="header" href="{% url 'listing' house_post.id %}">
{% blocktrans with house_type=house_post.house_type.name trimmed %}
{{house_type}}
{% endblocktrans %}
</a>
...
One value of house_type is "Condominium". I've added the following entry in my .po file.
msgid "Condominium"
msgstr "ኮንዶሚኒየም"
I've run compilemessages on the po file, and the rest of the translations work when I switch languages. And I've made sure the value of house_type is set to "Condominium". But for some reason it's not being translated.
In addition when I run makemessages the tool comments out additions I've made in the .po files. I'm uncommenting them before running compilemessages. I don't know why it's doing that though it might be a clue.
It is possible to add translation texts to .po files. Isn't it?
It's not translated because {{house_type}} will have the value of house_post.house_type.name.
The blocktrans actually does nothing in your code. You would need it if you want to add a translatable text to the sentence. Ex:
{% blocktrans with house_type=house_post.house_type.name trimmed %}
{{house_type}} Translate this part
{% endblocktrans %}
If you want to have a translated variable, you have to pass the translations to house_post.house_type.name.
The content of your blocktrans is most likely the content of {{house_type}}. Not sure where it comes from, but this is where you have to translate it. Don't forget to insert something like
from django.utils.translation import ugettext_lazy as _
to header of your py-files.
How can I use line breaks in translation strings inside .po files?
Currently I added "\n" and used {{msg|linebreaks}} in my template, but the string is printed in one single line... how can I print it on multiple lines?
I assume you want HTML output. In such case, it would be better to include HTML tags in the string:
{% blocktrans %}
First line<br/>
Second line
{% endblocktrans %}
If you are looking for something else, please describe better what you are actually trying to achieve.
Update:
If the lines are independent, it might be even better to actually split them up:
{% trans "First line" %}
<br/>
{% trans "Second line" %}
bit late, but maybe helps someone, add your translation like this:
po file
msgid "_your_msgid"
msgstr "first line\nsecond line"
template
{% trans '_your_msgid' as local_var %}
{{ local_var|linebreaksbr }}
You can use
character both in django template file and your .po files
Like this .po:
msgid "Psycological
aid"
msgstr "Психологическая
помощь"
django template:
{% trans 'Psycological
aid' %}
Containing block should have white-space: pre-line; css rule
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.
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.
The trans template tag calls the ugettext() function. In Django _() is an alias to ugettext().
This is covered in the django docs.