Opened 17 years ago
Closed 15 years ago
#5972 closed (fixed)
Use filters with {% trans %} tag - depends on #5971
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Internationalization | Version: | dev |
Severity: | Keywords: | i18n template i18n-nofix | |
Cc: | Ben Spaulding | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I want to apply filters to my translations in the templates. So.....
I have created a patch which allows use of the following syntax:
{% trans "username"|capfirst|slice:"2:" noop %} {% trans somevar|slice:"2:" %}
The filters are applied on the result of the translation, and not the
translation id string.
While this is already possible by using the {{ _("username")|filter }}
syntax, I think that {% trans %} is more "django-ish" and looks nicer
too...
An important note:
This patch depends on ticket 5971.
I have a patch that is free from that dependency, however since I got a positive response on said ticket I am posting the dependent patch. It's a bit smaller and fancier :)
Attachments (5)
Change History (16)
by , 17 years ago
Attachment: | trans_understands_filter_alternate_version.diff added |
---|
by , 17 years ago
Attachment: | trans_understands_filter_alternate_version.2.diff added |
---|
by , 17 years ago
Attachment: | trans_understands_filter_alternate_version.3.diff added |
---|
Cosmetic fix.
comment:1 by , 17 years ago
This patch has a possible backwards compatiblity issue.
Existing {% trans 'Something "or" other' %} strings will be converted into:
"Something "or" other" (including the outer quotes), so they will become invalid strings.
I am working on some ideas for this, but essentially it might require some work within FilterExpression itself...
by , 17 years ago
Attachment: | trans_understands_filter_alternate_version.4.diff added |
---|
Fixes the backwards compatiblity issue and knocks out an inconsistency in the FilterExpression parser at the same time.
comment:2 by , 17 years ago
Patch needs improvement: | set |
---|
Current patch is a little broken, for some reason setting the filter_expression.var breaks a few things.
Working on it.
by , 17 years ago
Attachment: | trans_understands_filters.diff added |
---|
This patch should be now be fully functional, however it the patch in ticket 5980 is strongly recommended.
comment:3 by , 17 years ago
Patch needs improvement: | unset |
---|
comment:4 by , 17 years ago
We are discussing this issue here with the three of us, but are wondering what a possible use case would be for providing this change?
Possibilities we see are:
- Your string is hardcoded and translations are done or in your python code by using gettext, or doing the same thing in your template by the facilities django templates provide.
- Your values come from a database backend and you take care of translations there.
Could you please explain to us what you are trying to do here?
comment:5 by , 17 years ago
I am just trying to avoid double translations.
In the django admin you can set verbose_name for models and fields, these are usually set using gettext_lazy. Django admin capitalizes these verbose_names sometimes and other times it uses the lower case version. Thus you only need one translation, for the lower case version.
Also, picture localizations in Japanese or Chinese. There are no capital letters in either language, so an english localisation would use automatic capitalisation and this would not require japanese or chinese translators to translate duplicate versions of many words.
This of course only affects titles and names of objects, not phrases and messages.
Example 1 - after patch:
{% trans "search" %} {% trans "search"|capfirst %}
Example 2 - before patch:
{% trans "search" %} {% trans "Search" %}
Example 1 requires only one translation while example 2 requiers two translations.
So the two main reasons are verbose_name in Admin and languages without capital letters.
However I am sure there are other reasons that I haven't thought of.
comment:6 by , 17 years ago
Summary: | Use filters with {% trans %} tag → Use filters with {% trans %} tag - depends on #5971 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
Ticket #5971 has been accepted, but needs tests. Once that patch is applied, this ticket should be evaluated.
comment:7 by , 16 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
In addition to the use cases and rationale for this feature Dmitri mention both in the description and in comment 5 I think it's worth emphasizing that the
{% trans somevar|somefilter[|...] %}
case (i.e. argument to trans
is a variable) is specially useful.
Currently, this can be implemented with a more verbose construct like:
{% filter somefilter[|...] %} {% trans somevar %} {% endfilter %}
But I've found it only works when somevar
is a context variable itself (e.g. name
) but now when it's an attribute of a variable (e.g. app.name
)
comment:8 by , 16 years ago
Cc: | added |
---|
comment:9 by , 16 years ago
The possibility to use filters in arguments was implemented for non-i18n template tags in r10119.
comment:10 by , 15 years ago
Keywords: | i18n-nofix added |
---|
comment:11 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
the backwards compatiblity fix in the previous patch was a little weak. This one does the trick a little better.