Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gettext with python >3.6 f-strings

Previously you would use gettext as following:

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

but what about new Python's f-string?

f'Hey {username}'
like image 625
mdargacz Avatar asked Apr 12 '18 13:04

mdargacz


People also ask

Does Python 3.6 support F strings?

As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!

What is f {} in Python?

The f-string was introduced(PEP 498). In short, it is a way to format your string that is more readable and fast. Example: The f or F in front of strings tells Python to look at the values inside {} and substitute them with the values of the variables if exist.

How do you write an F string in Python?

Strings in Python are usually enclosed within double quotes ( "" ) or single quotes ( '' ). To create f-strings, you only need to add an f or an F before the opening quotes of your string. For example, "This" is a string whereas f"This" is an f-String.

How do you use %d strings in Python?

The %d operator is used as a placeholder to specify integer values, decimals or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.


2 Answers

My solution is to make a function f() which performs the f-string interpolation after gettext has been called.

from copy import copy from inspect import currentframe  def f(s):     frame = currentframe().f_back     kwargs = copy(frame.f_globals)     kwargs.update(frame.f_locals)     return eval(s.format(**kwargs)) 

Now you just wrap _(...) in f() and don’t preface the string with an f:

f(_('Hey, {username}')) 

Note of caution

I’m usually against the use of eval as it could make the function potentially unsafe, but I personally think it should be justified here, so long as you’re aware of what’s being formatted. That said use at your own risk.

Remember

This isn’t a perfect solution, this is just my solution. As per PEP 498 states each formatting method “have their advantages, but in addition have disadvantages” including this.

For example if you need to change the expression inside the string then it will no longer match, therefore not be translated unless you also update your .po file as well. Also if you’re not the one translating them and you use an expression that’s hard to decipher what the outcome will be then that can cause miscommunication or other issues in translation.

like image 28
Jab Avatar answered Sep 29 '22 15:09

Jab


'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.

like image 133
Jean-François Fabre Avatar answered Sep 29 '22 13:09

Jean-François Fabre