【问题标题】:Django blocktrans problems with rendered variables渲染变量的Django blocktrans问题
【发布时间】:2020-04-04 11:14:45
【问题描述】:

我已经在这个项目中以同样的方式翻译了同一个目录下的一些类似的模板,几乎和这个相当。但是这个模板让我很无奈。

如果没有翻译标签{% blocktrans %},它可以正常工作并呈现变量。

c_filter_size.html

{% load i18n %}
{% if ffilter %}
  <div class="badge badge-success text-wrap" style="width: 12rem;"">{% trans "Filter sizing check" %}</div>
  <h6><small><p class="p-1 mb-2 bg-info text-white">{% trans "The filter sizing is successfully performed." %}
  </p></small></h6>

    {% if ffilter1 and ffilter.wfsubtype != ffilter1.wfsubtype %}
      <div class="badge badge-success text-wrap" style="width: 12rem;"">{% trans "Filter sizing check" %}</div>
      <h6><small><p class="p-1 mb-2 bg-info text-white">
        If you insist on the fineness, but allow
        to reduce flow rate up to {{ffilter1.flowrate}} m3/hr the filter size and therefore filter
        price can be reduced.
      </p></small></h6>
    {% endif %}

带有翻译标签{% blocktrans %} 它既不能用英语也不能用翻译的语言来渲染变量。其他类似的模板也能顺利工作。

c_filter_size.html

{% load i18n %}
{% if ffilter %}
  <div class="badge badge-success text-wrap" style="width: 12rem;"">{% trans "Filter sizing check" %}</div>
  <h6><small><p class="p-1 mb-2 bg-info text-white">{% trans "The filter sizing is successfully performed." %}
  </p></small></h6>

    {% if ffilter1 and ffilter.wfsubtype != ffilter1.wfsubtype %}
      <div class="badge badge-success text-wrap" style="width: 12rem;"">{% trans "Filter sizing check" %}</div>
      <h6><small><p class="p-1 mb-2 bg-info text-white">
    {% blocktrans %}
        If you insist on the fineness, but allow
        to reduce flow rate up to {{ffilter1.flowrate}} m3/hr the filter size and therefore filter
        price can be reduced.
    {% endblocktrans %}
      </p></small></h6>
    {% endif %}

django.po 

...

#: rsf/templates/rsf/comments/c_filter_size.html:11
#, python-format
msgid ""
"\n"
"        If you insist on the fineness, but allow\n"
"        to reduce flow rate up to <b>%(ffilter1.flowrate)s</b> m3/hr the "
"filter size and therefore filter\n"
"        price can be reduced.\n"
"        "
msgstr ""
"\n"
" Если тонкость фильтрации изменить невозможно, но возможно уменьшить "
"расход до <b>%(ffilter1.flowrate)s</b> м3/час, то "
"размер фильтра и соответственно его цена могут быть уменьшены."

...

谢谢

【问题讨论】:

    标签: django python-3.x django-templates


    【解决方案1】:

    您无法访问blocktrans 中的变量属性。 而不是在blocktrans 中使用{{ffilter1.flowrate}},您应该使用关键字with

    {% blocktrans with flowrate=ffilter1.flowrate %}
        If you insist on the fineness, but allow to reduce
        flow rate up to {{ flowrate }} m3/hr the filter size and
        therefore filter price can be reduced.
    {% endblocktrans %}
    

    此外,为避免在翻译中出现缩进,请使用关键字 trimmed:

    {% blocktrans with flowrate=ffilter1.flowrate trimmed %}
    

    来源:https://docs.djangoproject.com/en/3.0/topics/i18n/translation/#blocktrans-template-tag

    【讨论】:

      【解决方案2】:

      也许你可以把你的反式块分成两部分。

      {% blocktrans %}
              If you insist on the fineness, but allow
              to reduce flow rate up to {% endblocktrans %}
      {{ffilter1.flowrate}}
      {% blocktrans %} m3/hr the filter size and therefore filter
              price can be reduced.
      {% endblocktrans %}
      

      看起来不是最好的,但我认为不可能将变量放在 trans 块中。

      换个说法。我注意到您的内联样式有错误,您在下一行中有一个额外的 " 标记。

      &lt;div class="badge badge-success text-wrap" style="width: 12rem;""&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-11
        • 2016-05-22
        • 1970-01-01
        • 2018-04-20
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        相关资源
        最近更新 更多