ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Ensure Ruff Format handles gettext comments correctly

Open seanbudd opened this issue 7 months ago • 2 comments

issue

Python code using gettext comments can associate in-line comments with gettext. Like this:

# Translators: foo is programming term to represent a sample string
_("Foo")

_(
  # Translators: foo is programming term to represent a sample string
  "Foo"
)

pgettext(
  "sampleStrings",
  # Translators: foo is programming term to represent a sample string
  "Foo"
)

ruff format will reformat something like this

# Translators: foo is programming term to represent a sample string
(_("Foo"), "reallylongstringlongerthanlinelength")

# Translators: foo is programming term to represent a sample string
_("foo, reallylongstringlongerthanlinelength")

# Translators: foo is programming term to represent a sample string
pgettext("tag", "foo, reallylongstringlongerthanlinelength")

to

# Translators: foo is programming term to represent a sample string
(
   _("Foo"),
  "reallylongstringlongerthanlinelength"
)

# Translators: foo is programming term to represent a sample string
_(
   "foo, reallylongstringlongerthanlinelength"
)

# Translators: foo is programming term to represent a sample string
pgettext(
  "tag",
  "foo, reallylongstringlongerthanlinelength"
)

instead of the expected

(
   # Translators: foo is programming term to represent a sample string
   _("Foo"),
  "reallylongstringlongerthanlinelength"
)

_(
   # Translators: foo is programming term to represent a sample string
   "foo, reallylongstringlongerthanlinelength"
)

pgettext(
  "tag",
  # Translators: foo is programming term to represent a sample string
  "foo, reallylongstringlongerthanlinelength"
)

Suggested fix

expand the flake8-gettext config to set a translator comment flag e.g. "Translators:" If this is set, move any comments while formatting, to keep the comments on the line above the target string.

keywords

gettext, translators, translations, translate, i18n, l10n, format, comments

last tested ruff version

v0.5.0

seanbudd avatar Jul 04 '24 05:07 seanbudd