<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
On 17/09/18 19:42, Stephen J. Turnbull wrote:<br>
<blockquote type="cite"
cite="mid:23455.59261.224419.818192@turnbull.sk.tsukuba.ac.jp">
<pre wrap=""> > That's why I would like to see a parameter that can be passed to
> the f-string.
This doesn't make sense to me.</pre>
</blockquote>
If I get a request in English, I need to return English.<br>
If I get a request in French, I need to return French.<br>
<pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:11.3pt;"><span style="color:#808080;font-style:italic;"># At the start of the app, the languages get loaded in memory.</span>
translate = translation(<span style="color:#008080;font-weight:bold;">'app'</span>, <span style="color:#008080;font-weight:bold;">'.locale'</span>)
translate.install()
es = translation(<span style="color:#008080;font-weight:bold;">'app'</span>, <span style="color:#008080;font-weight:bold;">'.locale'</span>, <span style="color:#660099;">languages</span>=[<span style="color:#008080;font-weight:bold;">'es'</span>])
es.install()
<span style="color:#808080;font-style:italic;"># Get the preferred user language from the http request
</span>T(_(<span style="color:#008080;font-weight:bold;">'Hello {user}...'</span>), user_language, <span style="color:#660099;">user</span>=user)
</pre>
<span style="color:#000080;font-weight:bold;">def </span>T(translatable_string,
language=<span style="color:#000080;font-weight:bold;">None</span>,
*args, **kwargs):<br>
<pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:11.3pt;"> <span style="color:#000080;font-weight:bold;">if </span><span style="color:#008080;font-weight:bold;">'es' </span>== language:
</pre>
<pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:11.3pt;"> <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>
<blockquote type="cite"
cite="mid:23455.59261.224419.818192@turnbull.sk.tsukuba.ac.jp">
<pre wrap=""> Such configurations are long-lasting.</pre>
</blockquote>
If it is for the whole app, yes. Not if it is just the request.<br>
<br>
1. No translation, just a regular f-string.
<br>
2. App translation. The f-string gets translated to the configured
language.
Long lasting configuration.<br>
3. On the fly translation. The string gets translated to the
language passed as an argument as required.
<br>
<br>
<blockquote type="cite"
cite="mid:23455.59261.224419.818192@turnbull.sk.tsukuba.ac.jp">
<pre wrap="">What could work is an extension to the formatting language. I suggest
abusing the *conversion flag*. (It's an abuse because I'm going to
apply it to the whole f-string, while the current Language Reference
says it's applied to the value being formatted.[1]) This flag would only
be allowed as the first item in the string. The idea is that
`f"{lang!g}Hello, {user}!"` would be interpreted as
</pre>
</blockquote>
Excellent. The syntax is unimportant to me.<br>
<br>
Cheers,<br>
Hans<br>
<br>
</body>
</html>