[Python-ideas] PEP 501 - i18n with marked strings

Jonathan Slenders jonathan at slenders.be
Wed Aug 12 01:22:41 CEST 2015


Not exactly.

Take this string for instance:

f'hello {name}'


And our FString implementation, very simple:

class FString(str):
    def __init__(self, value, **kwargs):
        super().__init__(value.format(**self.kwargs))

        self.value = value

        self.kwargs = kwargs


What the above f-string should do is create an instance of that class. This
is just a compiler detail. A preprocessor step. Like this:

FString('hello {name}', name=str(name))


FString is just an str instance, it has the actual interpolated value, but
it still contains the original uninterpolated string and all parameters (as
strings as well.)

Now, what gettext can do, if we would wrap this string in the underscore
function, is take the "value" attribute from this string FString, translate
that, and apply the interpolation again.

This way, we are completely compatible with the format() call. There is no
need at all for using globals/locals or _getframe(). The name bindings are
static, this is lintable.

Please tell me if I'm missing something.


2015-08-11 19:12 GMT+02:00 Sven R. Kunze <srkunze at mail.de>:

> I actually thought this was about a two-step process using lazy evaluation.
>
> This way {name:i18n} or {name:later} basically marks lazy evaluation.
>
> But as it seems, i'...' is more supposed to do all (translation +
> formatting) of this at once. My fault, sorry.
>
>
> On 11.08.2015 10:35, Petr Viktorin wrote:
>
>> On Tue, Aug 11, 2015 at 9:36 AM, Sven R. Kunze <srkunze at mail.de> wrote:
>>
>>> Also bare with me but couldn't i18n not just be another format spec?
>>>
>>> i'Hello there, {name:i18n} {age}.'
>>>
>> Usually it's not the substitutions that you need to translate, but the
>> surrounding text.
>>
>
> _______________________________________________
> 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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150812/24d8acb9/attachment.html>


More information about the Python-ideas mailing list