Django: trans and blocktrans not escaping modulo (%) - django

I'm trying to makemessages on a template that has a translation which contains a modulo, like this;
{% trans "100% escaping problems sucks" %}
But I get this error:
Error: errors happened while running xgettext on site.html
./templates/site.html.py:34: warning: 'msgid' format string with unnamed
arguments cannot be properly localized:
The translator cannot reorder the arguments.
Please consider using a format string with named arguments,
and a mapping instead of a tuple for the arguments.
And if I try to escape it like this;
{% trans "100%% escaping problems sucks" %}
I get this error;
Error: errors happened while running xgettext on site.html.py
xgettext: error while opening "./templates/site.html.py" for
reading: No such file or directory
I have no idea why it is looking for ./templates/site.html.py .. it should be ./templates/site.html
Any idea ?
Edit: I forgot the Django version, it's 1.2.0 beta1

As per this ticket you might try upgrading to at least 1.2.1...

Related

Correct escaping to use in Django templates to avoid Unterminated string literal errors in VS Code

Consider the following in a Django .html template
<button onclick="location.href='{% url 'my-route' pk %}'">
# Warns because of this -------^.......^
VS Code will warn of an unterminated string literal as it doesn't understand that inside {% %} is processed first by the template engine. It works just fine, but the VS Code warnings (complete with red highlighting) are distracting.
Any way to fix this either by escaping ' or " or changing some VS Code config?
Add Django extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=batisteo.vscode-django

Frozen-Flask, url_for() in Jinja2 and string concatenation during build

When running Flask normally, everything described below works absolutely fine.
When running a build in Frozen-Flask however, I run into the following issue:
In my Jinja2 template I tried this at first, where I'm having to create the route strings of many url_for() functions from multiple sources:
{% macro my_macro(foo) %}
Link text
{% endmacro %}
Although this runs fine in Flask, when I do a build with Frozen-Flask I get the following error:
werkzeug.routing.BuildError: Could not build url for endpoint 'index_'. Did you mean...
As you can see, the value of foo is missing.
So I thought that's annoying, but maybe the string concatenation of 'index_' ~ foo happens after the url_for() is parsed. So to test my theory, I tried this instead:
{% macro my_macro(foo) %}
{% set route = 'index_' ~ foo %}
Link text
{% endmacro %}
But I get the exact same error. Exactly. So, as if it's setting the value of set route before concatenating the value of the foo variable onto the end of 'index_', and passing that incomplete value through to url_for().
However, if I substitute foo for 'foo' (so now foo is a string not a variable) then the concatenation works fine. So I can't concatenate string with string variables in Frozen-Flask?
Is there any way around this or is this maybe a possible bug in Frozen-Flask?
Update: It appears to be an issue with data coming from routes.py where I have return render_template('my_template.html', foo='bar'). This foo variable, although it is being passed through to the base template where in turn it is passed through to the macro (it can be output to prove the data present and correct) this data however cannot, it seems, be used to dynamically create a route for use by url_for().
i think will help you a simple line code
Link Text
considering foo as string only will be concatenate, no with string1+strin2, only using html printing.

How to get jinja2 to raise an error when a {% trans %} tag isn't replaced with anything

I'm using django & jinja2 templates for an internationalized website.
In debug mode, I'd like Jinja2 to throw an error when it fails to find a translation for a string marked with
{% trans %}abc{% endtrans %}
Setting it to raise errors when variables were not found in templates helped to find a bunch of problems, and I think this would help with preventing accidentally breaking a translation string, too.

Using blocktrans for translation

I am using the following blocktrans:
{% blocktrans with item|gender_text as gendertext %}
This is {{gendertext}} item
{% endblocktrans %}
In my .po file I have
msgid "This is %(gendertext)s item"
msgstr "Some translation ... %(gendertext)s"
For any language other than english, I would not like to use the gendertext variable. So I would want to have
msgstr "Some translation ..."
However, when I take off the %(gendertext)s from from msgstr, django_admin shouts (when compiling the messages):
'msgstr' is not a valid Python format string, unlike 'msgid'. Reason: The string ends in the middle of a directive.
msgfmt: found 1 fatal error
Any workarounds for this case?
Meir
If I understand your question well, Django is right to give that error beacsue, your key should be there for all languages.For example, there is no such case : in English I have a key gendertext but in Turkish I don't need that key.
One easy solution is keep that key in both languages but keep it empty if you don't want to render.
Other than your question, I strongly recommend you to use rosetta application, http://code.google.com/p/django-rosetta/ for translation processes.

Django template error - only option for 'trans' is 'noop'

This is regarding Django tutorial - Part 2
http://docs.djangoproject.com/en/dev/intro/tutorial02/
In the section to change the template for admin page, I tried to change this section in the base_site.html page.
{% trans 'Django's administration' %}
When I add the apostrophe and s, I ge the error that -
TemplateSyntaxError at /admin/
only option for 'trans' is 'noop'
Why is it so? I thought I should be able to change the site's name. I tried using double quotes and escape sequence also, but it did not work.
I tried using double quotes and escape
sequence also, but it did not work.
That is definitely the problem. That is the only problem that the error message specifies!
Are you positive there are no other places where you've done that?
It should be:
{% trans "Django's administration" %}
That error messages only exists for the tag "trans" and appears if there is any other argument in the tag that is not noop.
The problem is the second single-quote:
{% trans 'Django's administration' %}
Django is treating everything after it as an argument. As the only argument it accepts is noop, this is causing an error.
One way to get around it is to do as Yuji 'Tomita' Tomita suggested, and enclose your translation string in double-quotes.
Another way is to use the blocktrans tag:
{% blocktrans %}
Django's administration
{% endblocktrans %}
As you're not using quotes to denote text which needs to be translated, this won't run into the same issue as the trans tag.