Opened 12 years ago

Closed 9 years ago

Last modified 7 years ago

#19300 closed Bug (fixed)

capfirst filter breaks translation context in template

Reported by: Dylan Verheul Owned by: nobody
Component: Template system Version: 1.7
Severity: Normal Keywords: trans context capfirst
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (9)

comment:1 by Aymeric Augustin, 12 years ago

Resolution: worksforme
Status: newclosed

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

comment:2 by Dylan Verheul, 12 years ago

Will test again to see if it is fixed.

comment:3 by Dylan Verheul, 12 years ago

Confirmed, it works

comment:4 by None, 10 years ago

Resolution: worksforme
Status: closednew
Version: 1.41.7

The problem is actual again :(

comment:5 by Tomáš Ehrlich, 10 years ago

Resolution: worksforme
Status: newclosed

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" %}
# 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?

comment:6 by Pascal Polleunus, 9 years ago

Resolution: worksforme
Status: closednew

Same problem with Django 1.8.8.

comment:7 by Aymeric Augustin, 9 years ago

Resolution: needsinfo
Status: newclosed

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

comment:8 by Przemysław Czumaj, 7 years ago

Resolution: needsinfofixed

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.

comment:9 by Dylan Verheul, 7 years ago

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.

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