<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
On 17/09/18 09:53, Niki Spahiev wrote:<br>
<blockquote type="cite" cite="mid:pnnmd1$nf8$1@blaine.gmane.org"><br>
Is it possible to use f-strings when making multilingual software?
<br>
When i write non-hobby software translation is hard requirement.
<br>
<br>
</blockquote>
<br>
At this moment, it seems that this is not possible.<br>
<br>
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).<br>
<br>
That's why I would like to see a parameter that can be passed to the
f-string.<br>
<br>
I don't think this should be too problematic, really.<br>
<br>
pygettext.py extracts strings surrounded by _('') My proposal would
be to do that with f-strings. Let pygettext.py extract f-strings.<br>
<br>
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)<br>
<br>
My first email had pseudo-code. This is my working function.<br>
<br>
<pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:11.3pt;"><span style="color:#000080;font-weight:bold;">def </span>T(translatable_string, language=<span style="color:#000080;font-weight:bold;">None</span>, *args, **kwargs):
<span style="color:#000080;font-weight:bold;">if </span>args:
<span style="color:#000080;">print</span>(args)
<span style="color:#000080;font-weight:bold;">if </span><span style="color:#008080;font-weight:bold;">'es' </span>== language:
<span style="color:#808080;font-style:italic;"># Return translated, formatted string
</span><span style="color:#808080;font-style:italic;"> </span><span style="color:#000080;font-weight:bold;">return </span>es.gettext(translatable_string).format(**kwargs)
<span style="color:#808080;font-style:italic;"># Default, return formatted string
</span><span style="color:#808080;font-style:italic;"> </span><span style="color:#000080;font-weight:bold;">return </span>translatable_string.format(**kwargs)
</pre>
</body>
</html>