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
New-style placeholders are now supported: gettext(u'Hello {name}!', name='World') –> "Hello World!"
Old-style placeholders also still work: gettext(u'Hello %(name)s!', name='World') –> "Hello World!"
You should even be able to use both styles side by side. (Not that you should…)
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.
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.