[Python-ideas] Combine f-strings with i18n

Hans Polak hpolak at polak.es
Mon Sep 17 04:11:18 EDT 2018


On 17/09/18 09:53, Niki Spahiev wrote:
>
> Is it possible to use f-strings when making multilingual software?
> When i write non-hobby software translation is hard requirement.
>

At this moment, it seems that this is not possible.

My use case is not very unique and that's why I wrote the proposal in 
the first place. I'm working on a web server / application server. On 
the web, you have to take the users preferences into account, including 
language. If a user has the navigator configured for English, I have to 
return English (if I am doing i18n).

That's why I would like to see a parameter that can be passed to the 
f-string.

I don't think this should be too problematic, really.

pygettext.py extracts strings surrounded by _('') My proposal would be 
to do that with f-strings. Let pygettext.py extract f-strings.

The compiler can then rewrite these to normal unicode strings. For 
instance: f'Hi {user}'.language('es') would become T(_('Hi {user}'), 
'es', user=user)

My first email had pseudo-code. This is my working function.

def T(translatable_string, language=None, *args, **kwargs):
     if args:
         print(args)

     if 'es' == language:
         # Return translated, formatted string return es.gettext(translatable_string).format(**kwargs)

     # Default, return formatted string return translatable_string.format(**kwargs)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180917/9564f414/attachment.html>


More information about the Python-ideas mailing list