Django context translations with dynamic variables

1,459 views
Skip to first unread message

Gleb Tocarenco

unread,
Mar 1, 2017, 8:23:43 AM3/1/17
to Django users
Hello,

I am running in with an issue with Django translation tag in case context is present as a dynamic variable.

{% trans 'You have new message' context user.gender %}

In this case django.po files doesn't contains words related to gender context.

My question is if there is possibility to use context in translation tag as dynamic variable and generate django.po records based on it?

 

Jani Tiainen

unread,
Mar 1, 2017, 9:16:45 AM3/1/17
to django...@googlegroups.com

In theory you just define genders to your PO file with translations and it should work.

Not sure will that disable autogeneration and update for your PO file after that.

--
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...@googlegroups.com.
To post to this group, send email to django...@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/5c01a596-23a2-418f-9045-44a947efde0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

Uri Even-Chen

unread,
Mar 2, 2017, 8:00:36 AM3/2/17
to django...@googlegroups.com
Hi,

I'm working with Gleb on Speedy Match [https://github.com/urievenchen/speedy-net]. We need to translate to Hebrew according to context of the user's gender. Gleb suggested that the code will be something like this:

{% if user.gender == GENDER_FEMALE %}
{% trans 'This user blocked you.' context 'female' %}
{% elif user.gender == GENDER_MALE %}
{% trans 'This user blocked you.' context 'male' %}
{% else %}
{% trans 'This user blocked you.' context 'other' %}
{% endif %}

But, is it possible to change it to something like this:

{% trans 'This user blocked you.' context user.gender %}
While still auto-generate the django.po files? Genders are always either "female", "male" or "other". But some text may be related to genders of 2 users - the one using the site right now and another user. How do we do it then? I was thinking about context like "female_female", "female_male" etc. Anyway we don't want the templates to include many times the same text (3 or 9 times), we prefer that each text will appear only once.

Thanks,
Uri.

Uri Even-Chen  
photo Phone: +972-54-3995700
Email: u...@speedy.net
Website: http://www.speedysoftware.com/uri/en/
    

On Wed, Mar 1, 2017 at 4:15 PM, Jani Tiainen <red...@gmail.com> wrote:

In theory you just define genders to your PO file with translations and it should work.

Not sure will that disable autogeneration and update for your PO file after that.

On 01.03.2017 14:59, Gleb Tocarenco wrote:
Hello,

I am running in with an issue with Django translation tag in case context is present as a dynamic variable.

{% trans 'You have new message' context user.gender %}

In this case django.po files doesn't contains words related to gender context.

My question is if there is possibility to use context in translation tag as dynamic variable and generate django.po records based on it?

 
--
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.

-- 
Jani Tiainen

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Jani Tiainen

unread,
Mar 2, 2017, 9:00:55 AM3/2/17
to django...@googlegroups.com

Unfortunately makemessages can't guess what your context is if it's variable (I'm not sure would it even consider it as a translatable text at all)

First version would guarantee you to have translatable strings with genders, latter one either generates one string (and you have to manually create missing ones) or it won't find it at all.

You could test that very quickly with simple template with given fragments.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

Uri Even-Chen

unread,
Mar 2, 2017, 12:44:09 PM3/2/17
to django...@googlegroups.com
What if we "cheat" makemessages like this:

{% if 0 %}

{% trans 'This user blocked you.' context 'female' %}
    {% trans 'This user blocked you.' context 'male' %}
    {% trans 'This user blocked you.' context 'other' %}
{% endif %}
{% trans 'This user blocked you.' context user.gender %}
(or maybe we should filter user.gender with a filter that returns "female", "male" or "other" (because they are integers).

It looks better to me and less work to "cheat" makemessages like this, unless there is a better solution? Because translations which don't exist in the code will be commented every time we run makemessages. Maybe this is a feature we can add to the next Django release? And then we will not have to "cheat"?

Thanks,
Uri.

Uri Even-Chen  
photo Phone: +972-54-3995700
Email: u...@speedy.net
Website: http://www.speedysoftware.com/uri/en/
    

-- 
Jani Tiainen

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Mar 2, 2017, 1:34:32 PM3/2/17
to django...@googlegroups.com

On Thursday 02 March 2017 19:43:07 Uri Even-Chen wrote:

 

 

> Maybe this is a feature we can add to the next Django release? And

> then we will not have to "cheat"?


This would be a difficult thing to do, unless the trans tag is altered, cause otherwise how would specify the available contexts for a context? Your makemessages command would get complex.

 

Something like this maybe:

 

{%trans 'dancer' context_choices=gender_choices choice=request.user.gender %}

 

Then makemessages should generate a string with msgctxt for each of the choices. You could do this already, by rolling your own "trans" tag and "makemessages" command.

--

Melvyn Sopacua

Uri Even-Chen

unread,
Mar 2, 2017, 1:55:40 PM3/2/17
to django...@googlegroups.com

 

Then makemessages should generate a string with msgctxt for each of the choices. You could do this already, by rolling your own "trans" tag and "makemessages" command.


I don't understand, how can I do it already? I want makemessages to generate a string with all the possible contexts (which may be for example 3 options or 9), and not to have to "cheat" it by {% if 0 %} etc. But how do I do it? By the way where is the makemessages code?

Melvyn Sopacua

unread,
Mar 2, 2017, 6:21:35 PM3/2/17
to django...@googlegroups.com

On Thursday 02 March 2017 20:54:07 Uri Even-Chen wrote:

> > Then makemessages should generate a string with msgctxt for each of

> > the choices. You could do this already, by rolling your own "trans"

> > tag and "makemessages" command.

> >

> >

> I don't understand, how can I do it already? I want makemessages to

> generate a string with all the possible contexts (which may be for

> example 3 options or 9), and not to have to "cheat" it by {% if 0 %}

> etc. But how do I do it? By the way where is the makemessages code?

 

Wel, you can create your own commands:

https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/

 

And your own tags:

https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/

 

So if waiting for a next release that may or may not have this functionality does not fit your timeline, this is what you can do.

--

Melvyn Sopacua

Jani Tiainen

unread,
Mar 3, 2017, 2:36:59 AM3/3/17
to django...@googlegroups.com

Hi,


On 02.03.2017 19:43, Uri Even-Chen wrote:
What if we "cheat" makemessages like this:

{% if 0 %}
    {% trans 'This user blocked you.' context 'female' %}
    {% trans 'This user blocked you.' context 'male' %}
    {% trans 'This user blocked you.' context 'other' %}
{% endif %}
{% trans 'This user blocked you.' context user.gender %}
(or maybe we should filter user.gender with a filter that returns "female", "male" or "other" (because they are integers).

It looks better to me and less work to "cheat" makemessages like this, unless there is a better solution? Because translations which don't exist in the code will be commented every time we run makemessages. Maybe this is a feature we can add to the next Django release? And then we will not have to "cheat"?

This will be really problematic.

First, how makemessage command would know all the possible values of "user.gender"? Where that information would be obtained? It must be available somewhere when processing strings for translations.

If user.gender is fixed list and you know that, you could pick existing code of makemessages and enhance it with your custom code that magically knows values for user.gender. So you need to code that part yourself and it would be special command to your app(s).

Another option that Melvyn suggested is to have totally custom tag that contains possible values and then variable to choose among them. That could work just fine, but still you need to do custom makemessages command that would understand your tag and it's syntax.

Makemessages managemen command code can be found at:
https://github.com/django/django/blob/master/django/core/management/commands/makemessages.py

-- 
Jani Tiainen

Uri Even-Chen

unread,
Mar 3, 2017, 3:21:29 AM3/3/17
to django...@googlegroups.com
Thank you Melvyn and Jani!
Reply all
Reply to author
Forward
0 new messages