Django Translating Choice Fields
Solution 1:
It seems to me that this is related to the makemessages
command, which for some reason struggles with non-static strings.
I couldn't elaborate on the why, but here's how you can solve the problem:
You actually have to manually create the strings in the translation files (django.po):
#: .\polls\models.py:enum_to_choices
msgid "open"
msgstr "ouvert"
msgid "closed"
msgstr "fermé"
msgid "balance"
msgstr "solde"
Don't forget to django-admin compilemessages
, and translated strings should appear!
This is not ideal, especially for long enums, but better than nothing.
If someone here knows the inner workings of makemessages
(and the xgettext
program it uses) and has an explanation, please let us know ^^
Other solution: use recommended choices structure
Another solution, if you don't absolutely need the choices to be in enums, is using the choices structure as shown in the documentation:
from django.utils.translation import ugettext_lazy as _
class Payment(models.Model):
OPEN = 0
CLOSED = 1
BALANCE = 2
STATUS_CHOICES = (
(OPEN, _('open')),
(CLOSED, _('closed')),
(BALANCE, _('balance')),
)
status = models.IntegerField(choices=STATUS_CHOICES, default=OPEN, verbose_name=_("Status"))
And then on django-admin makemessages
the strings will be added to the translation files.
You can easily get the value of a choice, as you would with an enum :
from .models importPaymentnew_payment= Payment.objects.create(status=Payment.OPEN)
Post a Comment for "Django Translating Choice Fields"