I have a basic feed I am trying to render in my Django project. I have created a feed.html file as a base template for slight variations of the same type of feed. Among those variations, is the header of the feed. Importantly, I want that header to be translated into multiple languages.
I have implemented this "variation" idea using {% include "feed.html" with variation=variation %}. However, I am having problems translating those variations.
I am trying the following in feed.html:
{% translate header %}
Then in one of the templates where I want a variation of feed.html I have:
{% include "feed.html" with header="Header" %}
The problem is, the string "Header" does not make it into any of my .po files and therefore remains untranslatable.
What am I doing wrong? Should I use different syntax?
The problem is, the string "Header" does not make it into any of my .po files and therefore remains untranslatable.
If that means that the makemessages command doesn't extract the string, then yes, it won't, because there is no string. Only a variable. That variable could take any value at all, makemessages cannot possibly trace that back to all possible locations where you might be setting the value for header. makemessages can only extract what you literally put into {% translate %} tags or _() function calls directly.
The caveat with using variables or computed values, as in the previous two examples, is that Django’s translation-string-detecting utility, django-admin makemessages, won’t be able to find these strings.
https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#standard-translation
You'll want to translate the header value before passing it:
{% include "feed.html" with header=_("Header") %}
Related
I admit the question is vague, but that's the error I'm getting.
I'm trying to create a word document template using a derivative of the python-docx library called python-docx-template, which is using Jinja2.
What I'm doing is generating the data I need using Python, and then sending it to the docx template that I'm building, so that the document outputs the data in a format I want.
My Jinja2 template looks something like this:
{% if EXCHANGE_RATE %}
{%- for stringData in EXCHANGE_RATE %}
{{stringData}}
{%- endfor %}
{%- endif %}
Note that the template code is within a table cell. The entire data is supposed to be generated within the cell itself(ie. not separated by rows).
What is supposed to happen here is that the template checks for the existence of EXCHANGE_RATE list variable (because I may or may not return this data depending on situation) and then loops the data, resulting in something like this:
Exchange Rate:-
1 EUR= 1.0 USD
1 EUR= 1.0 GBP
For some reason, the if endif code seems to add some extra line spacing which I do not want(without it, the output is exactly what I want), hence the usage of the - at various parts.
The issue I have lies in the last line: {%- endif %}.
If I add an extra - at the back (like so {%- endif -%} ), it somehow causes an error whenever I generate and try to open the file, but I'm not exactly sure why.
I have a similar if endif nested for loop elsewhere and it will cause a similar issue, also at its own final {%- endif %} line.
Any suggestions?
Please note I'm still relatively new to Jinja2.
you can define the path of your word file in the trusted locations. for this purpose open MS word -> file -> options -> trust center setting -> trusted location -> add new location
and add the location the template is located in
after that you should not get word experienced error.
I hope :)
Try Edit from word file's context menu. Or you can disable protected view for all documents from Trusted Center settings at the end.
I am using cookiecutter to create a tornado project, using this template (it has several bugs, so you'll probably won't be able to use it out of the box). I have hit a problem which I do not know how to solve:
jinja2.exceptions.TemplateSyntaxError: unexpected char '\\' at 124272
File "./{{cookiecutter.project_slug}}/static/swagger/lib/jsoneditor.min.js", line 10
I am not sure, but I have the impression that cookiecutter is trying to Jinja-process the jsoneditor.min.js, which is not supposed to happen, since the "templating" in that file is not supposed to be processed by cookiecutter, it just happens to include the same escape characters that Jinja is using.
Is it possible to tell cookiecutter not to process files inside a certain directory? This is probably a matter of properly setting up the cookiecutter template, but not sure how this can be specified.
By default cookiecutter will try to process every file as a jinja template which produces wrong results if you have something that looks like a jinja template but is only supposed to be taken literal. Starting with cookiecutter 1.1 one can tell cookiecutter to only copy some files without interpreting them as jinja template (documentation).
To do that you have to add a _copy_without_render key in the cookiecutter config file (cookiecutter.json). It takes a list of regular expressions. If a filename matches the regular expressions it will be copied and not processed as a jinja template.
Example
{
"project_slug": "sample",
"_copy_without_render": [
"*.js",
"not_rendered_dir/*",
"rendered_dir/not_rendered_file.ini"
]
}
This will not process any javascript files (files which end with .js), any files that are in the not_rendered_dir and not the not_rendered_file.ini in the rendered_dir. They will only get copied.
Just came across this question and also this Github Issue.
It seems like a nice addition, that one can partially mark parts of a file or entire template to not be processed by using the {% raw %} tag:
{% raw %}
{% comment %}Whatever jinja code goes here....{% endcomment %}
...
{% endraw %}
They seem to be basically the same EXCEPT that you have to give ssi an absolute path for "security reasons". Except for keyword arguments (which are new since version 1.3), the two seem to have exactly the same capabilities. Are they really redundant?
Without the parsed parameter to {% ssi %} the included file won't be treated as a Django template, it will just be included as normal text. This means that if the included file had template tags/filters, they would not be interpreted.
When you do include the parsed parameter, the differences become more like what you said.
There's added security restrictions with {% ssi %} since you can potentially include any file on the filesystem.
You must use an absolute URI rather than a relative path suitable for a template loader.
Until Django 1.5, the first argument (the path) must be unquoted. This means you cannot use a template variable as the first argument.
There's more details on the last point in the documentation.
I am internationalizing (i18n) our django project, i.e. adding {% blocktrans %} to our templates. I know about using count and {% plural %} to have different strings for varaibles. However I have a string that has two variables that each need to be pluralized, i.e. 4 possible options.
For example, my string is "You have {{ num_unread }} unread message{{ num_unread|pluralize }} out of {{ total }} total message{{ total|pluralize }}"
How would I convert that to blocktrans tags?
After doing some more research and reading, specifically about gettext, I don't think this is possible. gettext documentation only allows one variable to control the pluralization. There are probably problems with having 2 variable pluralization, since in arabic, you'd have to have 36 different strings to translate.
In the end I just worked around my original problem, and split it into two strings.
I'm making a simple script that works on Jinja2 templates. Right now it's just reading files in from disk manually, i.e. no Jinja Loaders. I have 2 strings (A and B), representing 2 templates. I want to make one template (B) inherit from the other (A), i.e. I have {% block body %}{% endblock %} in A, and I want to make the body block be the contents of B. How can I get the rendered output of this?
Normally I'd use {% extends 'filename' %} in B and it'd use the right one, however I don't have the filename (per se) for A.
Your best bet probably to use a different template loader. Take a look at DictLoader and FunctionLoader, or try your hand a writing your own template loader.