我在Django翻译方面遇到了问题。服务器运行时,我的.po文件的某些字符串没有出现。 我的意思是显示了一些翻译,而没有显示。 无论它们是否出现,它们都在django.po文件中列出。 这不是“模糊”标签的问题,因为我删除了它们。在很长的msgid或msgstr之后,似乎不是空引号的问题。 糟糕的翻译显示的唯一原因是format_html()函数,该函数允许键入较长的帮助文本。您对我如何解决此案有任何想法吗?
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
)
...
语言环境/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 %}
答案 0 :(得分:1)
在我建议的情况下,问题出在format_html()
函数和 lazy 翻译的需要上。 ugettext_lazy()
函数不适用于format_html()
。它需要一个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)
然后我在form.py和models.py
中更改了format_html()
的调用
forms.py
from core.utils import format_html_lazy as format_html
etvoilà!