[Django] #19300: capfirst filter breaks translation context in template

25 views
Skip to first unread message

Django

unread,
Nov 15, 2012, 4:51:19 AM11/15/12
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
---------------------------------+------------------------------------
Reporter: dyve | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.4
Severity: Normal | Keywords: trans context capfirst
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Using the cap first filter breaks any context used in translation using
the trans template tag.

{% trans "species" context "singular" %}
# works as expected, results in translation of 'species' with context
'singular'

{% trans "species"|capfirst %}
# works as expected, results in translation of 'species' without context,
capfirst applied

{% trans "species"|capfirst context "singular" %}
# BROKEN, results in translation of 'species' without context, capfirst
applied

--
Ticket URL: <https://code.djangoproject.com/ticket/19300>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 16, 2012, 3:49:02 PM11/16/12
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: dyve | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: trans context | worksforme
capfirst | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed
* needs_better_patch: => 0
* resolution: => worksforme
* needs_tests: => 0
* needs_docs: => 0


Comment:

I can't reproduce the issue on master.

With this template and the appropriate French translations:
{{{
{% trans "species" %}


# works as expected, results in translation of 'species'

{% trans "species" context "singular" %}
# works as expected, results in translation of 'species' with context
'singular'

{% trans "species"|capfirst %}
# works as expected, results in translation of 'species' without context,
capfirst applied

{% trans "species"|capfirst context "singular" %}
# works as expected, results in translation of 'species' with context

'singular', capfirst applied
}}}
I get this output:
{{{
espèces # works as expected, results in translation of 'species'

espèce # works as expected, results in translation of 'species' with
context 'singular'

Espèces # works as expected, results in translation of 'species' without
context, capfirst applied

Espèce # works as expected, results in translation of 'species' with
context 'singular', capfirst applied
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:1>

Django

unread,
Dec 12, 2012, 9:15:11 AM12/12/12
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: dyve | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: trans context | worksforme
capfirst | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by dyve):

Will test again to see if it is fixed.

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:2>

Django

unread,
Jan 4, 2013, 4:59:03 AM1/4/13
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: dyve | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.4
Severity: Normal | Resolution:
Keywords: trans context | worksforme
capfirst | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by dyve):

Confirmed, it works

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:3>

Django

unread,
Nov 15, 2014, 3:03:09 PM11/15/14
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------

Reporter: dyve | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: trans context | Triage Stage:
capfirst | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by liminspace):

* status: closed => new
* version: 1.4 => 1.7
* resolution: worksforme =>


Comment:

The problem is actual again :(

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:4>

Django

unread,
Nov 24, 2014, 1:55:34 PM11/24/14
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: dyve | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: trans context | worksforme
capfirst | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by tricoder42):

* status: new => closed

* resolution: => worksforme


Comment:

Hi,
I tried to reproduce this problem, but it works for me:

{{{
# django.po
#: templates/test.html:3 templates/test.html.py:5 templates/test.html.py:6
msgid "species"
msgstr "espèces"

#: templates/test.html:4
msgctxt "singular"
msgid "species"
msgstr "espèce"
}}}


{{{
# test.html
{% load i18n %}

{% trans "species" %}


{% trans "species" context "singular" %}

{% trans "species"|capfirst %}

{% trans "species"|capfirst context "singular" %}
}}}


{{{#!python
# tests.py
from django.template import Context
from django.template.loader import get_template

tmpl = get_template('test.html')
print(tmpl.render(Context({})))
}}}


{{{
# output
espèces
espèce
Espèces
Espèce
}}}

The message catalogue has wrong references for plural "species", but
references are just informational. It doesn't affect the result.

If it still doesn't work for you (tested against 1.7.1 and master), could
you please provide more info to reproduce this bug?

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:5>

Django

unread,
Jan 3, 2016, 10:23:53 AM1/3/16
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------

Reporter: dyve | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.7
Severity: Normal | Resolution:
Keywords: trans context | Triage Stage:
capfirst | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ppo):

* status: closed => new

* resolution: worksforme =>


Comment:

Same problem with Django 1.8.8.

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:6>

Django

unread,
Jan 3, 2016, 10:39:14 AM1/3/16
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: dyve | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.7
Severity: Normal | Resolution: needsinfo

Keywords: trans context | Triage Stage:
capfirst | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed

* resolution: => needsinfo


Comment:

Several people have tried and failed to reproduce the issue, as shown in
the comments above. Can you provide reproduction instructions? Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:7>

Django

unread,
Jan 16, 2018, 2:19:16 PM1/16/18
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: Dylan Verheul | Owner: nobody

Type: Bug | Status: closed
Component: Template system | Version: 1.7
Severity: Normal | Resolution: fixed

Keywords: trans context | Triage Stage:
capfirst | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Przemysław "Pirax" Czumaj):

* resolution: needsinfo => fixed


Comment:

OK, I'll leave a reproduction example below if anyone will be interested,
but after some more digging I found out what the issue was.

Within ''django/django/utils/translation/trans_real.py'' file there was an
**inline_re** regex and this regex was badly constructed - up to Django
1.8.18. There was no "filters" block between translated string and a
"context" keyword, so construction
{{{
{% trans 'string'|capfirst context 'context' %}
}}}
couldn't be recognised properly. This issue was fixed in Django 1.9.

So that's it. If you are still using Django 1.8 and need a fix for it use
{{{
{% filter capfirst %}{% trans 'string' context 'context' %}{% endfilter %}
}}}
construction instead or update your Django version.


----

I can provide additional information to this issue as I'm fighting with it
right now.

Let's assume I have a template like this:
{{{
{% trans 'test_clean' %}
{% trans 'test_capfirst'|capfirst %}
{% trans 'test_context' context 'test_context' %}
{% trans 'test_context'|capfirst context 'test_context' %}
{% filter capfirst %}{% trans 'test_filter_context' context 'test_context'
%}{% endfilter %}
}}}

Now let's run ''makemessages''...

For:
{{{
{% trans 'test_clean' %}
}}}
I got:
{{{
msgid "test_clean"
msgstr ""
}}}

Result: OK!

For:
{{{
{% trans 'test_capfirst'|capfirst %}
}}}
I got:
{{{
msgid "test_capfirst"
msgstr ""
}}}

Result: OK!

For:
{{{
{% trans 'test_context' context 'test_context' %}
}}}
I got:
{{{
msgctxt "test_context"
msgid "test_context"
msgstr ""
}}}

Result: OK! Context is set as it should be.

For:
{{{
{% trans 'test_context'|capfirst context 'test_context' %}
}}}
I got:
{{{
msgid "test_context_capfirst"
msgstr ""
}}}

Comment: NOT OK! "msgctxt" key is missing, therefore Context is not set.

For:
{{{
{% filter capfirst %}{% trans 'test_filter_context' context 'test_context'
%}{% endfilter %}
}}}
I got:
{{{
msgctxt "test_context"
msgid "test_filter_context"
msgstr ""
}}}

Comment: OK! Context is set as it should be.

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:8>

Django

unread,
Jan 17, 2018, 2:08:52 AM1/17/18
to django-...@googlegroups.com
#19300: capfirst filter breaks translation context in template
-------------------------------------+-------------------------------------
Reporter: Dylan Verheul | Owner: nobody
Type: Bug | Status: closed
Component: Template system | Version: 1.7
Severity: Normal | Resolution: fixed
Keywords: trans context | Triage Stage:
capfirst | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Dylan Verheul):

Thanks for the explanation, explains everything. Good to know it's fixed.
I can confirm we haven't seen this bug since moving away from 1.8.

--
Ticket URL: <https://code.djangoproject.com/ticket/19300#comment:9>

Reply all
Reply to author
Forward
0 new messages