python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

Add support for new-style Python formatting placeholders #170

Open DriesOeyen opened 3 years ago

DriesOeyen commented 3 years ago

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

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.