Opened 5 years ago

Last modified 5 years ago

#30081 closed New feature

Translation with context - makemessages doesn't recognize the context — at Initial Version

Reported by: אורי Owned by: nobody
Component: Core (Management commands) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We are using Django for Speedy Net and Speedy Match (currently Django 1.11.18, 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."))

(you can see it on https://github.com/speedy-net/speedy-net/blob/uri_merge_with_master_2018-12-26_a/speedy/core/accounts/forms.py)

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-26_a/speedy/core/base/__translations.py and https://github.com/speedy-net/speedy-net/blob/uri_merge_with_master_2018-12-26_a/speedy/core/templates/__translations.html respectively), and there we can include the same strings with context:

Either:

pgettext_lazy(context="female", message="You can't change your username.")
pgettext_lazy(context="male", message="You can't change your username.")
pgettext_lazy(context="other", message="You can't change your username.")

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?

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top