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

Anders Hovmöller boxed at killingar.net
Wed Sep 19 02:54:37 EDT 2018


> 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).


I guess it wasn't clear before.. that's exactly what I was proposing :)

I'd suggest using parso to do it. It's a really great library to write such transformations.

/ Anders


More information about the Python-ideas mailing list