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

Steve Barnes gadgetsteve at live.co.uk
Wed Sep 19 01:54:42 EDT 2018

On 18/09/2018 08:59, Hans Polak wrote:
>>> 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.
> Actually, no, I do not want the expression to be automatically 
> translated at compile time. It should be translated at run-time. There 
> are three situations.
> 1. No translation, just a regular f-string.
> 2. App translation. The f-string gets translated to the configured 
> language.
> 3. On the fly translation. The string gets translated to the language 
> passed as an argument as required.
> In code, this would be.
> 1. f'Hi {user}'
> 2. f'{!g}Hi {user}'
> 3. f'{lang!g}Hi {user}'
> Cases 2 and 3 need some additional code, just like with gettext.
> I'm sorry if that wasn't clear from the start. All I want is the code to 
> be simpler to write and maintain. I do not want to have complicated 
> parsing for the compiler.
>>> Another way forward could be a preprocessor. All this can be done 
>>> with a fairly simple script using parso.
> This is probably the idea.
> Cheers,
> Hans
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

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.

If you wanted to insert into an f string a value that is itself subject 
to I18n you need to mark the content assigned to that value for 
translation. For example:

parts_of_day = [_("Morning"), _("Afternoon"), _("Evening"), _("Night"), ]
tod = lookup_time_as_pod()
greeting = _(f"Good {tod}")

If our locale happens to be a German one and our current time of day is 
morning then tod will be assigned as "morgan" and our greeting will be 
"Gutten Morgan", etc.

This should work without any major problems whether our locale is fixed 
at start-up or changes dynamically.

As far as I can see the only possibly required change to the core python 
language is that the evaluation order may need to be able to be 
override-able so that the translate function, (with the leave {.*} alone 
rule), is called _before_ the f string formatting, (I think that with 
current precedence it would not). Is there, or could there be, an 
"@eager" or "@push_precedence" decorator, or some such, that could be 
added to translate so as to do this? The remaining changes would be in 
the translate/I18n package(s) and the documents of course.

Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

This email has been checked for viruses by AVG.

More information about the Python-ideas mailing list