1. Thomas Waldmann
  2. moin-2.0
Issue #99 open

can we use new string formatting using .format() method?

Thomas Waldmann
repo owner created an issue

moin stil uses "%s %d" % (foo, bar) style string formatting.

some work has already been done on replacing this with .format() new style formatting, but it is not completed yet.

especially, a lot of translatable strings still use old %-style.

maybe this is related to the _() method there, this should be checked (and whether we can / should use .format() there.

this needs to be done before doing translations, because change of original english strings would make the translated string invalid.

if we decide on using some other formatting method than now, the respective refactoring should be done globally in the source.

Comments (5)

  1. Julian Brost

    If you look at the gettext implementation of flask-babel, you can see that it just formats the translated string using the % syntax, nothing fancy there.

    Without patching flask-babel, it would be possible to do something like

    _('Good morning, {}!').format('Joe')
    

    but of course it would be much nicer if one could do the same thing like this:

    _('Good morning, {}!', 'Joe')
    

    I don't know if there's a specific reason for this or if it is just out of habit.

    [1] https://github.com/mitsuhiko/flask-babel/blob/master/flaskext/babel.py#L447

  2. Arne Klein

    It seems that a decision has to be made on how to proceed with the gettext translations. I am aware of the following four possibilities:

    1. Use the current recommended flask-babel syntax, which uses formatting with %

      Example:

      _(u'Hello %(name)s!', name='World')

    2. Ask the flask-babel project again (possibly with more explanation, https://github.com/mitsuhiko/flask-babel/issues/16) to provide alternative gettext functions which use .format syntax. This only concerns the functions gettext, ngettext, pgettext and npgettext and in total 8 lines of code.

      Example:

      _(u'Hello {name}!', name='World')

      _(u'Hello {name!s}!', name='World') # calls str() on the argument before replacement

    3. Use the same syntax as in 2., but implement/overwrite the four gettext functions in our code.

    4. Use the current flask-babel gettext functions, applying .format manually.

      Example:

      _(u'Hello {name!s}!').format(name='World')

  3. Log in to comment