Best-practice for formatted string literals and localization?

Dieter Maurer dieter at handshake.de
Tue Dec 1 13:40:29 EST 2020


Hartmut Goebel wrote at 2020-11-30 19:30 +0100:
>formatted string literals are great, but can't be used together with
>localization:
>
>    _(f"These are {count} stones")
>
>will crash babel ("NameError: name 'count' is not defined".

Translations are kept in external files (without any runtime
association).
As a consequence, they cannot contain runtime entities;
and therefore, you should not use f-strings for internationalizations.

Usually, the translation machinery has special ways to
provide parameters for translations.
For example with `zope.i18nmessageid`, you can use
`_(msg, mapping=<parameter_dict>)` to provide parameters
to the translations -- as in your case `count`).
Check, what parameter support your translation machinery support.


More information about the Python-list mailing list