gpt4 book ai didi

Django 表单和 i18n

转载 作者:行者123 更新时间:2023-12-04 00:43:17 25 4
gpt4 key购买 nike

我有要以不同语言显示的表单:我使用 label 参数设置参数,并在标签上使用 ugettext() :

agreed_tos = forms.BooleanField(label=ugettext('I agree to the terms of service and to the privacy policy.'))

但是当我在模板中渲染表单时,使用
{{form.as_p}}

标签未翻译。有人有解决这个问题的方法吗?

最佳答案

您应该使用 ugettext_lazy() :

from django.utils.translation import ugettext_lazy

# ...
agreed_tos = forms.BooleanField(label=ugettext_lazy('I agree to the terms of service and to the privacy policy.'))

模型和表单属性在 Django 应用程序启动时初始化。如果您使用 ugettext() ,翻译将在初始化时设置一次,永远不会改变。 ugettext_lazy()通过在访问字符串值而不是调用函数时翻译字符串来解决这个问题。

关于Django 表单和 i18n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2694798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com