Django template: Translate include with variable

2024/10/4 5:28:44

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.

Answer

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.

https://en.xdnf.cn/q/70644.html

Related Q&A

Pandas - Creating a New Column

I have always made new columns in pandas using the following:df[new_column] = valueI am using this method, however, am receiving the warning for setting a copy.What is the way to make a new column with…

Adding an extra column to (big) SQLite database from Pandas dataframe

I feel like Im overlooking something really simple, but I cant make it work. Im using SQLite now, but a solution in SQLAlchemy would also be very helpful.Lets create our original dataset:### This is ju…

error inserting values to db with psycopg2 module [duplicate]

This question already has answers here:psycopg2: cant adapt type numpy.int64(4 answers)Inserting records into postgreSQL database in Python(3 answers)Closed 3 months ago.I am attempting to insert a dat…

NaN values in pivot_table index causes loss of data

Here is a simple DataFrame:> df = pd.DataFrame({a: [a1, a2, a3],b: [optional1, None, optional3],c: [c1, c2, c3],d: [1, 2, 3]}) > dfa b c d 0 a1 optional1 c1 1 1 a2 None c2…

ModuleNotFoundError in Docker

I have imported my entire project into docker, and I am getting a ModuleNotFoundErrorfrom one of the modules I have created.FROM python:3.8 WORKDIR /workspace/ COPY . /workspace/ RUN pip install pipenv…

Can I use md5 authentication with psycopg2?

After two hours of reading documentation, source code and help-threads, Im giving up. I cant get psycopg2 to authenticate with a md5-string. According to this thread I dont have to anything besides ena…

Python checking __init__ parameter

Ive been trying to figuring this out for the last few hours, and Im about to give up.How do you make sure that in python only a matching specific criteria will create the object?For example, lets say …

Minidom getElementById not working

Minidoms getElementById function is returning None for any entry I pass to it.For example, this code:l = minidom.parseString(<node id="node">Node</node>) print(l.getElementById(&q…

Optimization on piecewise linear regression

I am trying to create a piecewise linear regression to minimize the MSE(minimum square errors) then using linear regression directly. The method should be using dynamic programming to calculate the dif…

Python: Check if list of named tuples contains particular attribute value

I have a list of named tuples:from collections import namedtupleT = namedtuple(T, [attr1, attr2, attr3, attr4]) t1 = T(T1, 1, 1234, XYZ) t2 = T(T2, 2, 1254, ABC) t3 = T(T2, 2, 1264, DEF) l = [t1, t2, t…