[Python-ideas] Combine f-strings with i18n - How about using PEP 501?

Chris Angelico rosuav at gmail.com
Wed Sep 19 02:48:51 EDT 2018

On Wed, Sep 19, 2018 at 3:55 PM Steve Barnes <gadgetsteve at live.co.uk> wrote:
> Surely the simpler solution is to specify in I18n any items within
> un-escaped {} pairs is excluded from the translation, lookups, etc., and
> that translation needs to take place, also leaving the {} content alone,
> before f string processing. Other than that there is no change. So:
> _(f'Hi {user}') would be in the .po/.mo as just 'Hi ' and if our locale
> is set to FR this gets translated to f'Bonjor {user}' which then gets
> the user variable substituted in.

How about this: Have a script that runs over your code, looking for
"translatable f-strings":

_(f'Hi {user}')

and replaces them with actually-translatable strings:

_('Hi %s') % (user,)
_('Hi {user}').format(user=user)

Take your pick of which way you want to spell it. Either of these is
easily able to be picked up by a standard translation package, is 100%
legal Python code in today's interpreters, and doesn't require any
bizarre markers and such saying that things need to be processed out
of order (the parentheses specify the order for you).

Not everything has to be an f-string.


More information about the Python-ideas mailing list