Django i18n提供不完整的翻译

问题描述 投票:0回答:1

我正面临着django翻译的问题。服务器运行时,我的.po文件的某些字符串不会出现。我的意思是一些翻译被显示,而其他翻译则没有。无论它们是否出现,它们都列在django.po文件中。这不是“模糊”标签的问题,因为我删除了它们。在很长的msgid或msgstr之后,这似乎不是空引号的问题。翻译显示错误的唯一原因是format_html()函数,它允许输入更长的帮助文本。你对我如何解决这个案子有什么想法吗?

应用程序/调查/ 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 %} 
python django internationalization translation
1个回答
1
投票

正如我所预期的那样,问题来自format_html()函数和懒惰翻译的需要。 ugettext_lazy()函数不适用于format_html()。它需要一个format_html_lazy()

核心/ utils.py

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

format_html_lazy = lazy(format_html, str)

我在forms.py和models.py中更改了format_html()的调用

forms.朋友

from core.utils import format_html_lazy as format_html

而且你在那里!

© www.soinside.com 2019 - 2024. All rights reserved.