How to use gettext with python >3.6 f-strings

'Hey {},' is contained in your translation dictionary as is.

If you use f'Hey {username},', that creates another string, which won’t be translated.

In that case, the format method remains the only one useable, but you could approach the f-string features by using named parameters

_('Hey {username},').format(username=username)

or if you have a dictionary containing your data, this cool trick where format picks the required information in the input dictionary:

d = {"username":"John", "city":"New York", "unused":"doesn't matter"}

_('Hey {username} from {city},').format(**d)

Leave a Comment