I'm building an API using Django Rest Framework that needs to support multiple languages, we're trying to use the Django translation framework, and using the answer from here I've decided how to implement translatable choicefields. However my issue is for the reverse case.
Say I have a model like this:
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"))
This works fine for returning the information to the user, we use the stored status value and translate it before returning. However, I also want the user to be able to POST a new payment object using any language. So if the user wants to create an open transaction, they can send {'status':'open'}
but they can also send it in Spanish as {'status':'abierto'}
. Is there a way to handle this automatically?
Basically I want to get the reverse of the ugettext_lazy function.
abierto
instead ofopen
for a choice field? (I mean it's just the post API don't make it unecessarily complicated. – alivastatus
is an IntegerField, you would typically post an integer in the api as well. If you want the translated choices, you can submit anOPTIONS
request to the api endpoint. – Håken Lid