Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new-style Python formatting placeholders #170

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

DriesOeyen
Copy link

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.

DriesOeyen and others added 3 commits July 24, 2020 15:44
This PR adds support for new style Python formatting. 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!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The return value shouldn't be marked as safe.
3 participants