为什么pgettext_lazy会破坏我的模板,但ugettext_lazy却没有?

时间:2012-04-13 21:04:57

标签: django internationalization translation lazy-evaluation

当我在模型的help_text上使用pgettext_lazy时,我的模板失败了。它与ugettext_lazy一起工作正常。

错误

Caught TypeError while rendering: Lazy object returned unexpected type.

模型

class BalanceIncreaseOrder(models.Model):
    amount = models.FloatField(help_text=pgettext_lazy("Translators: please localize this to reflect the correct currency", "Note: amount will be billed in United States dollars (USD)"))

表格

class BalanceIncreaseOrderForm(ModelFormRequired):
    class Meta:
        model = BalanceIncreaseOrder
        fields = ("amount",)

模板

{% for field in form %}
    {{ field }}
{% endfor %}

我在单独设置帮助文本后调试了模型。两次打印出来

<django.utils.functional.__proxy__ object at 0x10fcb3a50>

pgettext_lazy中是否有错误?有什么想法吗?

1 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。我用了

unicode(pgettext_lazy('context', 'string'))

这将消除错误,但现在manage.py makemessages不会接收已翻译的行。

也许这会对你有帮助..

编辑:

啊,我找到了解决办法:

pgettext_lazy(u'context', u'string')

这将完成这项工作。