Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some of the converted strings into fstrings are no longer processed by babel #487

Closed
iranzo opened this issue Jul 13, 2021 · 7 comments
Closed

Comments

@iranzo
Copy link

iranzo commented Jul 13, 2021

I'm using babel via a custom function _(string,language) that outputs the translated version.

When running pyupgrade over the source files:

@@ -174,7 +173,7 @@ def irccommands(msgdetail):
                         )
                     else:
                         text = _(
-                            "Error opping users from chat %s: %s" % (chat_id, result),
+                            f"Error opping users from chat {chat_id}: {result}",
                             language=mylang,
                         )
 

It causes babel to segfault as per python-babel/babel#715

I would expect pyupgrade not to touch the code in a way that makes it later fail.

@MarcoGorelli
Copy link
Contributor

Hi @iranzo - are you sure this isn't a bug in babel? The conversion here looks correct, do you have a reproducible example of where it isn't?

@iranzo
Copy link
Author

iranzo commented Jul 13, 2021

Hi @MarcoGorelli see the linked issue in babel python-babel/babel#715 and the comment:

I understand that including f-strings inside the _() function is a mistake

Thanks

@MarcoGorelli
Copy link
Contributor

what's the _() function?

@iranzo
Copy link
Author

iranzo commented Jul 13, 2021

Hi Marco, "_()" is usually the function defined for translatting strings, so in your code, you add stuff like:

_("This code works")

This function is defined from gettext (https://docs.python.org/3/library/gettext.html#localizing-your-application) and the way it works is that from all the strings inside it, it will generate a "pot" file with msgid and msgstr

When you run your code in an Italian environment (LANG=it_IT@UTF-8), the code will try to locate the translation strings inside the folder you defined, usually a locale/it/LC_MESSAGES/yourprogram.mo which has been compiled from a text file, usually locale/it/LC_MESSAGES/yourprogram.po, generated out of the 'pot' file for each one of the languages you want to translate with poedit, or some online services.

This makes that the msgid is searched in that catalog, and the msgstr is returned instead

so:
print(_("This code works"))

Will return on English or unstranslated languages:
"This code works"
In Spanish:
"Este código funciona"
In Italian:
"Questo codice funziona"

And so on

The strings are grabbed from the python files by using pybabel via:

pybabel extract -o myprogram.pot mycodefiles.py

And that's what causes the traceback in issue 715, when pyupgrade gets fstrings inside that function

@hugovk
Copy link
Contributor

hugovk commented Jul 13, 2021

Sounds like Babel doesn't support f-strings, which is a Babel bug, not pyupgrade.

But can you use the pyupgrade --keep-percent-format option?

@iranzo
Copy link
Author

iranzo commented Jul 13, 2021

The workaround I'm using is to use instead the {} markers without defined vars that should be good to go, but as the issue arised when using pyupgrade, I raised this here for awareness

@asottile
Copy link
Owner

you're misusing translation -- those strings aren't being translated. if anything babel should produce an error for accidentally using string formatting inside of a translation function

babel will scan those (incorrectly) as "Error opping users from chat %s: %s" -- then at runtime it will try and translate "Error opping users from chat whatever1: whatever2" which will not match any of your translated IDs so you will always end up with english text. (we had a linter when I was at yelp which prevented this sort of translation misuse -- but I believe it was proprietary / lost to closed source)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants