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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ def init_app(self, app):
)
app.jinja_env.add_extension('jinja2.ext.i18n')
app.jinja_env.install_gettext_callables(
lambda x: get_translations().ugettext(x),
lambda s, p, n: get_translations().ungettext(s, p, n),
newstyle=True
# Use Flask-Babel's own gettext functions
lambda x, **v: gettext(x, **v),
lambda s, p, n, **v: ngettext(s, p, n, **v),
# Jinja's "newstyle" wrappers aren't necessary,
# Flask-Babel's own gettext functions handle everything.
newstyle=False
# NOTE: Jinja's "newstyle" wrappers mark strings
# as "safe" with MarkupSafe, while Flask-Babel's own
# gettext functions do not. This means HTML in PO-files
# will be rendered as text. This is intentional:
# translation files should not contain markup.
)

def localeselector(self, f):
Expand Down Expand Up @@ -543,13 +551,13 @@ def gettext(string, **variables):
::

gettext(u'Hello World!')
gettext(u'Hello %(name)s!', name='World')
gettext(u'Hello {name}!', name='World')
"""
t = get_translations()
if t is None:
return string if not variables else string % variables
return string if not variables else string.format(**variables) % variables
s = t.ugettext(string)
return s if not variables else s % variables
return s if not variables else s.format(**variables) % variables
_ = gettext


Expand All @@ -558,21 +566,21 @@ def ngettext(singular, plural, num, **variables):
given keyword arguments as mapping to a string formatting string.
The `num` parameter is used to dispatch between singular and various
plural forms of the message. It is available in the format string
as ``%(num)d`` or ``%(num)s``. The source language should be
as ``{num}``. The source language should be
English or a similar language which only has one plural form.

::

ngettext(u'%(num)d Apple', u'%(num)d Apples', num=len(apples))
ngettext(u'{num} Apple', u'{num} Apples', num=len(apples))
"""
variables.setdefault('num', num)
t = get_translations()
if t is None:
s = singular if num == 1 else plural
return s if not variables else s % variables
return s if not variables else s.format(**variables) % variables

s = t.ungettext(singular, plural, num)
return s if not variables else s % variables
return s if not variables else s.format(**variables) % variables


def pgettext(context, string, **variables):
Expand All @@ -582,9 +590,9 @@ def pgettext(context, string, **variables):
"""
t = get_translations()
if t is None:
return string if not variables else string % variables
return string if not variables else string.format(**variables) % variables
s = t.upgettext(context, string)
return s if not variables else s % variables
return s if not variables else s.format(**variables) % variables


def npgettext(context, singular, plural, num, **variables):
Expand All @@ -596,9 +604,9 @@ def npgettext(context, singular, plural, num, **variables):
t = get_translations()
if t is None:
s = singular if num == 1 else plural
return s if not variables else s % variables
return s if not variables else s.format(**variables) % variables
s = t.unpgettext(context, singular, plural, num)
return s if not variables else s % variables
return s if not variables else s.format(**variables) % variables


def lazy_gettext(string, **variables):
Expand All @@ -622,7 +630,7 @@ def lazy_ngettext(singular, plural, num, **variables):

Example::

apples = lazy_ngettext(u'%(num)d Apple', u'%(num)d Apples', num=len(apples))
apples = lazy_ngettext(u'{num} Apple', u'{num} Apples', num=len(apples))

@app.route('/')
def index():
Expand Down