【问题标题】:Django i18n gives incomplete translationsDjango i18n 提供不完整的翻译
【发布时间】:2019-03-13 13:35:07
【问题描述】:

我遇到了 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
        )
    ...

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 %} 

【问题讨论】:

  • 确定你已经编译好消息文件了吗??
  • 是的,我做到了!我什至在编译新的之前抑制了以前的 django.mo。
  • “翻译显示不好的唯一原因是 format_html() 函数”——这是否意味着只有在使用 format_html() 时才会丢失翻译?
  • 翻译不显示怎么办?它们是否被空字符串替换?还是源语言字符串?
  • 是的,只有在使用 format_html() 时才会缺少翻译。当不显示翻译时,显示源语言字符串...因此,format_html() 与源语言配合良好,也被makemessages 进程识别,但在运行服务器时翻译不好。

标签: python django internationalization translation


【解决方案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)

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

forms.py

from core.utils import format_html_lazy as format_html

瞧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多