Skip to content

Are f-strings (PEP 498) supported ? #594

Closed

Description

@JoseKilo

Overview Description

Not sure if this has been answered before. Are f-strings (PEP 498) supported ?

Steps to Reproduce

  1. I have this input file (foo.py)
from gettext import gettext as _


def main():
    foo = "foo"
    print(_(f"Hello {foo}"))


if __name__ == '__main__':
    main()
  1. I run
pybabel extract -o locale foo.py

Actual 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

Activity

  1. akx commented on Jul 2, 2018

    @akx
    Member

    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.

  2. pavlix commented on Sep 3, 2018

    @pavlix

    @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 reads f"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.

  3. akx commented on Sep 3, 2018

    @akx
    Member

    Heh, that's true. I didn't even think about it that far.

  4. akx commented on Dec 18, 2018

    @akx
    Member

    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.

  5. added a commit that references this issue on Oct 14, 2020
  6. added a commit that references this issue on Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions