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

rule for detecting format error in gettext translations #3294

Closed
spaceone opened this issue Mar 1, 2023 · 5 comments · Fixed by #3741 or #3785
Closed

rule for detecting format error in gettext translations #3294

spaceone opened this issue Mar 1, 2023 · 5 comments · Fixed by #3741 or #3785
Labels
rule Implementing or modifying a lint rule

Comments

@spaceone
Copy link
Contributor

spaceone commented Mar 1, 2023

import gettext
_ = gettext.NullTranslations().gettext # ugettext, ngettext
var = 'baz'
# incorrect:
_('foo %s bar' % var)
_('foo {} bar'.format(var))
_(f'foo {var} bar')
# correct:
_('foo %s bar') % var
_('foo {} bar').format(var)

those 3 cases should be detected in a new rule:
→ calls to _ (the conventional name of gettext) should only be done with a contant string as argument.

@charliermarsh
Copy link
Member

Is the error that you're supposed to leave the parameters unfilled?

@charliermarsh charliermarsh added the question Asking for support or clarification label Mar 1, 2023
@charliermarsh
Copy link
Member

(Seems like a reasonable rule, I'm just not familiar with the API.)

@Jackenmen
Copy link
Contributor

The error is that you're supposed to use constant strings, not dynamic ones as that passed string is used as a key during the lookup in message catalog.

I think that ideally this should support not just the most basic gettext call accepting a single argument but also calls to gettext, ngettext, and so on:
https://docs.python.org/3/library/gettext.html

@spaceone
Copy link
Contributor Author

spaceone commented Mar 2, 2023

thanks for the feedback, I clarified the initial description.

@charliermarsh
Copy link
Member

Thank you!

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule and removed question Asking for support or clarification labels Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants