0

I am facing a problem with django translation. Some strings of my .po files doesn't appear when the server runs. I mean some translations are displayed some others are not. wether they appears or not, they are both listed in the django.po file. it's not a question of "fuzzy" tags, as a I removed them. It's seem not to be a question of empty quotes following very long msgid or msgstr. The only reason of that bad translation display are the format_html() function which allow to type a longer help text. Do you have any idea on how I can solve this case ?

apps/survey/forms.py

from django import forms
from django.utils.translation import ugettext_lazy as _
from django.utils.html import format_html
from django.forms.widgets import NumberInput
from survey.models import Survey2019

class Survey2019Form(forms.ModelForm):
    class Meta:
        model = Survey2019
    ...
    lines = forms.IntegerField(
        label=_('Lines'),
        help_text= _("What is the average number of lines contained in your reports ?"),
        widget=NumberInput(attrs={'type':'range', 'step': '1', 'min':'0', 'max':'500', 'step':'5'}),
        initial=0
        )

    categories = forms.IntegerField(
        label=_('Categories'),
        help_text= format_html("{}<br>{}<br>{}<br>{}",
            _("What is the average number of categories or dimensions in your reports ?"),
            _("Categories are mostly not numeric data."),
            _("They can't be computed but provide qualitative informations."),
            _(" For exemple : geographic areas, colors or groups of products...")),
        widget=NumberInput(attrs={'type':'range', 'min':'0', 'max':'100', 'step':'1'}),
        initial=0
        )
    ...

locale/fr/LC_MESSAGES/django.po

    ...
    : apps/survey/forms.py:80
msgid "Lines"
msgstr "lignes"

#: apps/survey/forms.py:81
msgid "What is the average number of lines contained in your reports ?"
msgstr "Quel est le nombre moyen de lignes que comptent vos tableaux de bord ?"


#: apps/survey/forms.py:87
msgid "Categories"
msgstr "Catégories"

#: apps/survey/forms.py:89
msgid "What is the average number of categories or dimensions in your reports ?"
msgstr "Quel est le nombre moyen de catégories que comptent vos tableaux de bord ?"

#: apps/survey/forms.py:90
msgid "Categories are mostly not numeric data."
msgstr "les catégories, ou dimensions, sont souvent des données non numériques"

#: apps/survey/forms.py:91
msgid "They can't be computed but provide qualitative informations."
msgstr "Elles ne peuvent-être calculée mais fournissent des informations qualitatives"

#: apps/survey/forms.py:92
msgid " For exemple : geographic areas, colors or groups of products..."
msgstr ""
"par exemple des zones géographiques, des couleurs, des groupes de produits "
"ou de personnes"
....

survey2019.html

...
{% for field in fillform.visible_fields  %}
<div class="{% if forloop.first %}card{% else %}d-none {% endif %} fl-w-600 fl-h-700 justify-content-between align-items-center" id="id_question_{{forloop.counter}}" refer="id_{{field.name}}">
  <div class="w-100 fl-h-50 fl-bg-prune">
    <h2 class="flowka fl-txt-white text-truncate text-center pt-1">{{field.label}}</h2>
  </div>
  <div class="fl-h-500 p-2 w-100">
    <div class="text-justify p-2 fl-txt-prune fl-txt-md mb-1" style="margin-top:-30px;">
      {{ field.help_text }}
    </div>
    ...  
  </div>
  ...
</div>
{% endfor %} 
6
  • Make sure you have compiled the message file??
    – Pankaj
    Mar 13, 2019 at 13:54
  • yes I did ! I even suppress the previous django.mo before compiling the new one.
    – David
    Mar 13, 2019 at 14:16
  • "The only reason of that bad translation display are the format_html() function" – does that mean that the translations are only missing when format_html() is used?
    – Endre Both
    Mar 13, 2019 at 14:23
  • What happens when the translations are not displayed? Are they replaced by an empty string? Or the source language string?
    – Endre Both
    Mar 13, 2019 at 14:24
  • Yes translations are missing only when using the format_html(). When translations are not displayed, the source language string is displayed... Thus, format_html() works fine with source language and is also recognised by makemessages process but is not translated well when running the server.
    – David
    Mar 13, 2019 at 14:38

1 Answer 1

1

As I ssupected it, the problem comes from the format_html() function and the need of lazy translation. The ugettext_lazy() function does not works with format_html(). It needs a format_html_lazy()

core/utils.py

from django.utils.functional import lazy
from django.utils.html import format_html

format_html_lazy = lazy(format_html, str)

and I changed the call of format_html() in forms.py and models.py

forms.py

from core.utils import format_html_lazy as format_html

et voilà !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.