I have a template in which you can pass a text variable. I want to include this template into another one but with a translated text as it's variable. How can you achieve this?
I would like something like this:
{% include "a_dir/stuff.html" with text={% trans "Load more promotions" %} %}
I tough about writing my own template tag that will perform a ugettext
but then when creating the .po
file, the text variable will not be taken automatically.
I don't want to do this work in the view
since all our translations take place in the templates.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
The include tag allows you to include content from another template. Place the include tag exactly where you want the content to be displayed. This is useful when you have the same content for many pages.
Use the function django. utils. translation. gettext_noop() to mark a string as a translation string without translating it.
A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.
You can put the translated string into a variable with the as
syntax. For instance:
{% trans "Load more promotions" as promotions %}
{% include "a_dir/stuff.html" with text=promotions %}
See the docs for more details.
A shorter way is
{% include 'a_dir/stuff.html' with text=_("Load more promotions") %}
which also works fine with variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With