Flask, Jinja2, Babel error on "$" character Flask, Jinja2, Babel error on "$" character flask flask

Flask, Jinja2, Babel error on "$" character


This is a two-fold problem coming from the fact that flask (and jinja more specifically), when interpreting text from templates and applying filters and/or context processors, it uses the string % something operation liberally.

This operation interprets the %1$d text like a formatting string, an invalid one that is causing the error. So what you need to do is replace both of the operations you're doing in the template, since the ones provided by flask (and its extensions, usually) are likely to cause errors because of the aforementioned modulus operation.

First, you can create a context processor using the babel gettext directly:

from flask_babel import gettext#...@app.context_processordef my_gettext():  return {'my_gettext': gettext}

Now a filter for the text replacement:

@app.template_filter()def my_replace(text, old, new):  return text.replace(old, new)

With this, in your template you'd use:

<div class="...">{{ my_gettext('error_long_value') | my_replace('%1$d', '200') }}</div>


It is hard to tell from your questions what is happening exactly, But it looks like you have error message "Error: Max %1$d characters" in Babel config under 'error_long_value' variable and you want do display "Error: Max 200 characters" in the outputted html. And for some reason something on the way does not like the "$" character. You could just change the placeholder to something that does not contain the "$". That will fix at least one problem. As for why it was working in webapp2, it might be different version of Babel or python or who knows what.Other thing you could check is to what exact function is called with _('error_long_value') is it babel gettext() ? where the variable name is assigned?