Add support for new-style Python formatting placeholders #170
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for "new style" Python formatting placeholders based on
str.format
in addition to the existing support for%
operator-based placeholders. Originally this was a Python 3 feature, but it has been backported to Python 2.6 and 2.7.New-style Python formatting comes with many advantages in terms of placeholder syntax and flexibility. This PR allows Flask-Babel users to use both old-style and new-style placeholders side-by-side. For a comparison of the 2 styles, see: https://pyformat.info
gettext(u'Hello {name}!', name='World')
–> "Hello World!"gettext(u'Hello %(name)s!', name='World')
–> "Hello World!"Docstrings have been updated to suggest new-style syntax, so new users are guided towards the current best-practice. PEP 3101 specifies new-style formatting is intended as a replacement for
%
operator-based formatting.This PR also fixes #13 (translations automatically being considered "safe") by disabling Jinja's
newstyle
wrappers for gettext callables, which don't add anything useful on top of Flask-Babel's own gettext callables.Note that while this behavior is safer, this is a potentially breaking change for any folks who may be relying on the current behavior where HTML in translations is not escaped by Jinja. If you want HTML in translations to be rendered instead of escaped, we propose explicitly using Jinja's
|safe
filter. This makes the current (potentially unsafe) behavior opt-in.