0
votes

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.

1
what is the point of posting abierto instead of open for a choice field? (I mean it's just the post API don't make it unecessarily complicated.aliva
Possible duplicate of Django translation reversealiva
I asked the same question, unfortunately I don't get the final say in what we need to support. :) It looks like there isn't a simple built in way to do this from your other question.The Bearded Templar
Having your api accept multiple languages in this way seems completely bonkers to me. I hope the client pays by the hour, because this kind of "feature" would add a huge amount of extra work for testing and QA.Håken Lid
Since status is an IntegerField, you would typically post an integer in the api as well. If you want the translated choices, you can submit an OPTIONS request to the api endpoint.Håken Lid

1 Answers

0
votes

Use ugettext_noop instead ugettext_lazy.