Opened 19 months ago

Closed 4 weeks ago

#21695 closed New feature (fixed)

Allow blocktrans to set a context variable

Reported by: mitar Owned by: dracos
Component: Template system Version: 1.6
Severity: Normal Keywords:
Cc: mmitar@…, bmihelac@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, we can use {% trans "string" as var %} to set var to translated string. Nothing similar exists for blocktrans.

Change History (10)

comment:1 Changed 19 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Do any other block tags support as var? I'm not sure it's a good idea (or feasible for that matter). Could you describe your use case? Do you have interest in trying to implement it?

comment:2 Changed 19 months ago by mitar

Ehm, use case is string interpolation. I cannot use {% trans "foo {{ bar }}" as var %}. Having only constant translated strings are vars is a bit problematic.

I use that for setting title of the page ({% blocktrans %}{{ username }}'s block{% endblocktrans %}) and I would like to set it to the context variable so that I can reuse it around (title, heading, links ...).

Currently I am using this custom tag to achieve this. But I think this would be useful in the tag itself. Or maybe {% trans "foo {{ bar }}" as var %} should be supported at least? And _("foo {{ bar }}").

comment:3 Changed 17 months ago by bmihelac

  • Cc bmihelac@… added

comment:4 Changed 17 months ago by bmihelac

Here is a link to branch that implements this feature and two simple tests:

https://github.com/bmihelac/django-1/compare/fix-21695-allow-blocktrans-to-set-a-context-variable?expand=1

Please note that assigning option is named asvar contrary to usual as to avoid confusion between legacy format for using with.

If there is interest in merging this I will add docs and changelog and any more tests if needed.

comment:5 Changed 17 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

It seems that every tag is going to gain this feature eventually...

comment:6 Changed 17 months ago by mitar

This is why I created this tag which simply sets the context based on whatever is in there. Then there is no need to modify all tags to be able to set context. DRY. :-)

comment:7 Changed 7 weeks ago by dracos

  • Has patch set
  • Owner changed from nobody to dracos
  • Status changed from new to assigned

I have rebased bmihelac's patch on current master, made a few changes, added more tests and documentation, and submitted a PR at https://github.com/django/django/pull/4854

I have a need for this at the moment - exactly the same situation as why it's useful for trans, using the output in multiple places (most commonly, title element and heading). It works great for trans, but if the title happens to include a variable, I'm stuck.

comment:8 Changed 4 weeks ago by timgraham

  • Patch needs improvement set

comment:9 Changed 4 weeks ago by dracos

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 4 weeks ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 839edceb:

Fixed #21695 -- Added asvar option to blocktrans.

Thanks Bojan Mihelac for the initial patch.

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