简体   繁体   English

Django:在模板中使用ugettext_lazy

[英]Django: Use ugettext_lazy in templates

Hello I want to know if I can use ugettext_lazy as _ in the templates of django because I want to replace the tags of {% trans %} and {% blocktrans %} because I don't want to make the messages. 您好,我想知道是否可以在django的模板ugettext_lazy as _ ,因为我想替换{% trans %}{% blocktrans %}的标签,因为我不想发送消息。

I want to do something like this: 我想做这样的事情:

<h1>_('hello')</h1>

Instead of this: 代替这个:

<h1>{% trans 'hello' %}</h1>

Thanks 谢谢

It's not possible to call any function from a template that takes any arguments. 从带有任何参数的模板中调用任何函数都是不可能的。 You have to write either tag or filter but I would suggest you to stick with the trans block. 您必须编写标记或过滤器,但我建议您坚持使用trans块。

I want to say that <h1>_('hello')</h1> this functionality not possible in django template. 我想说<h1>_('hello')</h1>此功能在Django模板中是不可能的。

but we have another way of like this with custom templatetag that support your way for translate string in template 但是我们还有另一种使用自定义templatetag的方式,它支持您在模板中翻译字符串的方式

{% some_tag _("Page not found") value|yesno:_("yes,no") %}

above added string will work as translation in django. 上面添加的字符串将在Django中用作翻译。

Read from django documentation https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#string-literals-passed-to-tags-and-filters 从Django文档https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#string-literals-passed-to-tags-and-filters阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM