Setup Translation in a Template

18 views
Skip to first unread message

Stefan Messmer

unread,
Nov 29, 2025, 9:13:34 AM (5 days ago) Nov 29
to py4web
I should translate some small pieces of text in a template. How can I setup translation to make the function T(text) working correctly? I use a copy of the _scaffold app as starting point.

Many thanks and best regards
Stefan Messmer


laundmo

unread,
Dec 2, 2025, 10:27:45 AM (2 days ago) Dec 2
to py4web
As far as i can tell, the "intended" way is using the Inject fixture to inject T into the template rendering context, which you need to do for each controller which has a template that needs access to T.

But since thats very annoying, i chose the very not ideal workaround of
from py4web.core import HELPERS as TEMPLATE_VARS
TEMPLATE_VARS["T"] = T

which WILL break if you run more than 1 app that tries to do this through a single py4web instance, but since i will never do that it works. (i strongly believe each application should be an isolated python process, perhaps even its own docker container)

Massimo DiPierro

unread,
Dec 2, 2025, 10:14:08 PM (2 days ago) Dec 2
to laundmo, py4web
I agree with you that this is dangerous. i thought about the notion of a per app dictionary of common vars to inject but I think it would add complexity. I think it is better practice to introduce your own custom decorator that all the common operations you want such as auth, DB, T, injections, etc.

i am away from the computer this week but I will have lots of time for py4web after that. :-)

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/py4web/a9585fe7-7c9d-4f04-aa65-292a45102b55n%40googlegroups.com.

Stefan Messmer

unread,
Dec 3, 2025, 2:06:09 AM (yesterday) Dec 3
to py4web
Thanks for your advice. I decided to use the Inject fixture. This works fine...

Stefan Messmer

Reply all
Reply to author
Forward
0 new messages