Repository navigation
Closed
Labels
Description
Overview Description
Not sure if this has been answered before. Are f-strings (PEP 498) supported ?
Steps to Reproduce
- I have this input file (
foo.py)
from gettext import gettext as _
def main():
foo = "foo"
print(_(f"Hello {foo}"))
if __name__ == '__main__':
main()- I run
pybabel extract -o locale foo.pyActual Results
I get this error
extracting messages from foo.py
Traceback (most recent call last):
File "/home/merlin/workspace/babel-test/.venv/bin/pybabel", line 11, in <module>
sys.exit(main())
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/frontend.py", line 911, in main
return CommandLineInterface().run(sys.argv)
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/frontend.py", line 835, in run
return cmdinst.run()
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/frontend.py", line 470, in run
for filename, lineno, message, comments, context in extracted:
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/extract.py", line 212, in check_and_call_extract_file
strip_comment_tags=strip_comment_tags
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/extract.py", line 241, in extract_from_file
strip_comment_tags))
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/extract.py", line 322, in extract
for lineno, funcname, messages, comments in results:
File "/home/merlin/workspace/babel-test/.venv/lib/python3.6/site-packages/babel/messages/extract.py", line 484, in extract_python
value = eval(code, {'__builtins__': {}}, {})
File "<string>", line 2, in <module>
NameError: name 'foo' is not defined
Expected Results
I understand it should work similarly to
print(_("Hello {foo}".format(foo=foo)))Additional Information:
Using Babel==2.6.0
Reacted by Will Pimblett, ObserverOfTime, Yuri, Éloi Rivard and Mark
Activity
akx commentedon Jul 2, 2018
Currently not, unfortunately.
Extracting f-strings would require switching to AST analysis (which was briefly discussed in #415 way back when), so it would take some work.
pavlix commentedon Sep 3, 2018
@akx Extracting yes, that would need it. But would it be possible to do the actual translation in the program? As far as I understand the program uses
_()function on the final string which readsf"Hello{foo)"in the code but the actual value is already"Hello John"which cannot be translated using"Hello {foo}"→"Hola {foo}"for example, right? Unless I'm mistaken, PEP 498 completely ignores localization as if you never need to use localized programs with string interpolation.akx commentedon Sep 3, 2018
Heh, that's true. I didn't even think about it that far.
akx commentedon Dec 18, 2018
So yeah – closing as impossible. Unlike ES6's tagged template literals, f-strings don't have a way to call functions to do the interpolation.
fix: make extract
Change some f-strings to format()
[fix] add missing localizable (gettext) messages to searxng.msg
[fix] add missing localizable (gettext) messages to searxng.msg
[fix] add missing localizable (gettext) messages to searxng.msg
[fix] add missing localizable (gettext) messages to searxng.msg
i18n: remove f string usage for compatibility
i18n: remove f string usage for compatibility