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

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Sep 18 00:23:26 EDT 2018


Eric V. Smith writes:

 > See also PEP 501, which could be used for i18n.

I don't see how this immediately helps the OP, who wants a *literal*
expression that automatically invokes the translation machinery as
well as the interpolation machinery.  The translation machinery needs
access to the raw template string, which is what (human) translators
will be provided by pygettext.  But as far as I can see there is
nothing in i-string processing that provides a hook for this.

It presumably wouldn't be impossible to provide a class or factory
function derived from InterpolationTemplate that transparently does
the lookup, and then constructs an InterpolationTemplate (duplicating
the compiler's work?)  But a direct compilation to
InterpolationTemplate is hard-wired for (literal) i-strings.  AFAICS
this is the *only* benefit of PEP 501 over simply defining the
InterpolationTemplate class in a library module, and it's not usable
by gettext-style I18N!

We could add a `translate` method, to completely replace the
raw_template *before* the compiler parses it into parsed_template.
But this gets complicated in the sense of the Zen, because we want to
be able to change the translate method on the fly (if the target
language changes, or to change the gettext domain, or whatever), or if
we have non-I18N uses for i-strings in our application.

I hope I'm missing something!

Steve


More information about the Python-ideas mailing list