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
Related
I've done makemessages and compilemessages and both files contain the translation that is inside a {% blocktranslate %}. I've even re-run makemessages to make sure nothing changed in the msgid and it did not make any change to my .po file except for the POT-Creation-Date. But these {% blocktranslate %} paragraphs are not translating. I'm stuck with the msgid instead of the msgstr.
Is there some trick to very long msgid's?
I'm assuming the keys don't match, but not sure why they don't match since the tools don't change the values when re-run.
The problem has gotten worse, now some of the short translations just aren't working either.
Here is the rendered page:
Here is the source that renders it:
<li class="nav-item {% is_active_tab 'home' %}">
<a class="nav-link" href="{% url 'home' %}">
{% translate "Home" %}
</a>
</li>
{% if request.user.is_authenticated %}
<li class="nav-item {% is_active_tab 'games:list' %}">
<a class="nav-link" href="{% url 'games:list' %}">
{% translate "My Quizzes" %}
</a>
</li>
<li class="nav-item {% is_active_tab 'games:create' %}">
<a class="nav-link" href="{% url 'games:create' %}">
{% translate "New Quiz" %}
</a>
</li>
And here is the .po file
#: templates/site_base.html:41
msgid "Home"
msgstr "Inicio"
#: templates/site_base.html:47
msgid "My Quizzes"
msgstr "Mis Cuestionarios"
#: templates/site_base.html:52
#, fuzzy
msgid "New Quiz"
msgstr "Nuevo Cuestionario"
Yes, I have run compilemessages
(bb) $ manage compilemessages
processing file django.po in .../locale/es_MX/LC_MESSAGES
And based on the first translation it is finding the file, but then it only translates a few of the terms. The Admin translations are better (more complete) but I'm not sure what I'm doing wrong. I thought it might be LOCALE_PATHS so I added this to my settings but then remembered that compilemessages was already finding the file so that had no effect....
LOCALE_PATHS = [
BASE_DIR / 'locale',
]
Update:
I was using the locale of es_MX. When I converted back to just es, it started working, and removing the #fuzzy was the other part of the fix. I'm not sure how you do variations on spanish, but for the moment, I'm just not worrying about that.
Your code does not show any {% blocktranslate %} so it is hard to provide an appropriate answer.
For the short translation, the #, fuzzy disallow the .po entry to be used and it won't translate.
If you want to translate variations of Spanish such as es_MX, then:
./manage.py makemessages -l es_MX
set LANGUAGE_CODE = 'es-MX' in your settings.py
NB: notice the difference, underscore for makemessages, and minus for settings!
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.
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 %}
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