f-strings and internationalisation.
Barry Scott
barry at barrys-emacs.org
Mon May 24 15:14:07 EDT 2021
> On 24 May 2021, at 19:30, Antoon Pardon <antoon.pardon at rece.vub.ac.be> wrote:
>
> I have now come across several occasion where an author advice the use
> of f-strings above the %-formatting and the format method. However it
> seems these authors were only thinking about rather straight forward
> english communication.
>
> So what if you want your application to work with multiple languages.
> Can that be done with f-strings?
No it cannot be done. This is because a translation can reorder
the parts of the string is drastic ways that f'strings' does not allow for.
You need to use this style:
_('This %(arg1)s and %(arg2)s') % {'arg1': value_arg1, 'arg2': value_arg2}
or
_('This {arg1} and {arg2}').format(arg1=value_args, arg2=values_arg2)
A translator would be free to swap arg1 and arg2 order in a translation.
See https://docs.python.org/3/library/gettext.html for more details.
And https://www.mattlayman.com/blog/2015/i18n/ looks useful as well.
Then you can use use the I18N gettext tools to make a .pot and .po files.
Barry
>
> --
> Antoon Pardon.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list