Internationalize the attributes of an object
My object has an attribute called status. Status can have two states开发者_如何学编程: opened and closed. The value of this attribute should be translated. I've tried to do this in two ways:
<td> {% trans object.status %} </td>
and
<td>
{% blocktrans with object.status as status %} {{ status }} {% endblocktrans %}
</td>
but with no result. In django.po file I have an entry %(status)s. How will Django know how to translate the status?
If status can only have two states, you should use the choices
attribute in the model definition. The values for choices can then be marked for translation:
STATUS_CHOICES = (
('open', _('open')),
('closed', _('closed'))
)
class MyModel(models.Model):
status = models.CharField(max_length=10, choices=STATUS_CHOICES)
and in the template, use the get_status_display
method:
<td> {{ object.get_status_display }} </td>
check if you drop these 4 lines into the po:
msgid "opend"
msgstr "Your translated word"
msgid "closed"
msgstr "your trans word"
and run :
manage.py compiletranslation
because when you edit the .po file with existing .mo file, the change have no effect
精彩评论