开发者

Cannot get Django translation to work for choice field

开发者 https://www.devze.com 2023-02-25 02:27 出处:网络
I\'m using a choice field in a model and when I display it in the template using {{ object.get_FIELD_display }}, it always shows up in the same language.. even if it\'s translated in the po file.

I'm using a choice field in a model and when I display it in the template using {{ object.get_FIELD_display }}, it always shows up in the same language.. even if it's translated in the po file.

Here's a simplified version of my code:

models.py

PRODUCT_WEIGHT_UNIT = (
    ('to', _开发者_StackOverflow社区('ton')),
    ('li', _('pound')),
    ('vg', _(u'vgs³')),
)


class ProduitVrac(models.Model):
    title = models.CharField(_("Title"), max_length=50)
    unit  = models.CharField(max_length=2, choices=PRODUCT_WEIGHT_UNIT) 

template

<ul>
{% for object in object_list %}
<li>
    <h2>{{ object.title }}</h2>
    {# The following will not be translated .. #}
    {{ object.get_unit_display }}
</li>
{% endfor %}
</ul>

Am I missing something ?


Turns out I was using gettext instead of ugettext_lazy.. it solved my problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消