python - 如何使 Django blocktrans 中的空格和缩进无关紧要?

标签 python django django-templates django-i18n

在一些 Django 模板中绘制以下 blocktrans:

{% blocktrans %}
    Some Text
{% endblocktrans %}

在模板中进行一些更改后,您可能希望缩进该 block :

<div>
    {% blocktrans %}
        Some Text
    {% endblocktrans %}
</div>

这将更改翻译文件中的消息,并将它们标记为模糊。从技术上讲,这是相同的消息(只是缩进不同)。

“消除模糊化”这些消息的整个过程既乏味又容易出错。

到目前为止我尝试了什么:

  • 尽可能多地使用trans——并不总是有效
  • 保持 blocktrans 语句的初始缩进 - 难以维护
  • 试图在文档和其他资源中找到更多信息

有没有办法让 blocktrans 中的缩进变得无关紧要?

最佳答案

根据文档here :

Another feature {% blocktrans %} supports is the trimmed option. This option will remove newline characters from the beginning and the end of the content of the {% blocktrans %} tag, replace any whitespace at the beginning and end of a line and merge all lines into one using a space character to separate them. This is quite useful for indenting the content of a {% blocktrans %} tag without having the indentation characters end up in the corresponding entry in the PO file, which makes the translation process easier.

例如

<div>
    {% blocktrans trimmed %}
        Some Text
    {% endblocktrans %}
</div>

将在您的 PO 文件中生成条目 "Some Text"

关于python - 如何使 Django blocktrans 中的空格和缩进无关紧要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40282464/

相关文章:

javascript - 将所有 Javascript 移动到 Django 中的单独文件

python - 如何将 tweepy 流 json 转储到文本文件中?

Django 和 get_absolute_url

python - Django:使用外键保存动态表单

python - 以这种格式显示数字列表django

django - 如何在另一个应用程序中使用一个应用程序中的模板

python - 重复列表创建指定形状的 numpy 数组

python - 在 Python 中求解多项式方程

python - Django ORM 迁移在 IntegerField 上给出 "ValueError: invalid literal for int()"

python - 如何有效地将数据从 geopandas 保存到 django(从 shapely 转换为 geodjango)?