Tuesday, December 25, 2018

Re: Translation with context

Hi,

Django doesn't have translation system of own but relies on gettext tools (and python wrappers) to handle translations. Those tools doesn't know anything that your python code returns to context so no, there is no simple way to do that automatically. 

It's a limitation of gettext that there is not simple way handle genders etc, in translations.

‪On Tue, Dec 25, 2018 at 11:57 AM ‫אורי‬‎ <uri@speedy.net> wrote:‬
Hi,

We are using Django for Speedy Net and Speedy Match (currently Django 1.11.17, we can't upgrade to a newer version of Django because of one of our requirements, django-modeltranslation). I have a problem with translating strings with context. We use context mainly for translating strings differently depending on the user's gender, which translates differently in languages such as Hebrew, although the English strings are the same. For example, I have this code:

raise ValidationError(pgettext_lazy(context=self.instance.get_gender(), message="You can't change your username."))


The problem is that manage.py makemessages doesn't recognize the context here. A workaround we found is to include this text manually in files we don't use at all, such as __translations.py or __translations.html (you can see them on https://github.com/speedy-net/speedy-net/blob/uri_merge_with_master_2018-12-23_a/speedy/core/base/__translations.py and https://github.com/speedy-net/speedy-net/blob/uri_merge_with_master_2018-12-23_a/speedy/core/templates/__translations.html respectively), and there we can include the same strings with context:

Either:

pgettext_lazy(context="female", message='Your new password has been saved.')
pgettext_lazy(context="male", message='Your new password has been saved.')
pgettext_lazy(context="other", message='Your new password has been saved.')

Or:

{% trans "You can't change your username." context 'female' %}
{% trans "You can't change your username." context 'male' %}
{% trans "You can't change your username." context 'other' %}

But this is a lot of work and we have each string 4 times in our code (not including translations), and it's very difficult to maintain such a code. And as I said, these files are not used at all, they are only for ./make_all_messages.sh to work properly. So my question is - is there a way to pass all the possible contexts as a list to manage.py makemessages? For example add another argument to pgettext_lazy or add a specific comment which will be read by manage.py makemessages? I checked and currently we use translation with context about 100 times in this project, and it would be a lot of work to generate all this code (100 * 3 times) just for manage.py makemessages to work.

By the way, the method get_gender() always returns one of these strings: "female", "male" or "other". There is also another method, get_match_gender(), which returns the same values.

Is it possible to define the context "other" as default, so if a different context (or none) is passed and there is no translation with the given context, "other" will be used?

By the way, did someone ask this question before? Is there any solution I don't know?

Thanks,
אורי (Uri)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABD5YeH4Nwz0g3w3FDw3A4mTdRXFu9PR26%2BfiPCTXzN5GTtVUg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91odLs_XLA40awVVMz5Peth%2B9yOQjMdc0OpLoPKqUX2Gt9w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment