Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ def extract_python(
buf = []
messages = []
translator_comments = []
comments_used_lineno = None
in_def = in_translator_comments = False
comment_tag = None

Expand All @@ -585,6 +586,9 @@ def extract_python(
current_fstring_lineno = 0

for tok, value, (lineno, _), _, _ in tokens:
if comments_used_lineno is not None and lineno > comments_used_lineno and not funcname:
translator_comments = []
comments_used_lineno = None
if not call_stack and tok == NAME and value in ('def', 'class'):
in_def = True
elif in_def and tok == OP and value == '(':
Expand Down Expand Up @@ -638,12 +642,14 @@ def extract_python(
[comment[1] for comment in translator_comments],
)

# Keep comments available to other translation calls on this line.
comments_used_lineno = call_stack[0]
funcname = lineno = message_lineno = None
call_stack.clear()
messages = []
translator_comments = []
in_translator_comments = False
if nested:
translator_comments = []
funcname = value
elif tok == STRING:
val = _parse_python_string(value, encoding, future_flags)
Expand Down
8 changes: 8 additions & 0 deletions docs/messages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ The resulting output in the catalog template would be something like::
msgid "Foo Bar"
msgstr ""

In Python source, a translator comment can describe several sibling translation
calls on the next line. The following comment is included with both messages:

.. code-block:: python

# NOTE: SQL operators
operators = (_('IS'), _('NOT'))

Now, you might ask, why would I need that?

Consider this simple case; you have a menu item called “manual”. You know what
Expand Down
37 changes: 37 additions & 0 deletions tests/messages/test_extract_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,43 @@ def test_comment_tag_multiline():
assert messages[0][3] == ['NOTE: A translation comment', 'with a second line']


@pytest.mark.parametrize('calls', [
"messages = [_('IS'), _('NOT')]",
"_('IS'); _('NOT')",
"Symbol(key=_('IS'), args=[Symbol(key=_('NOT'))])",
"messages = [_('IS'), _(\n 'NOT'\n)]",
])
@pytest.mark.parametrize('strip_comment_tags', [False, True])
def test_comment_applies_to_calls_on_same_line(calls, strip_comment_tags):
source = f"# NOTE: SQL operators\n{calls}\n_('NEXT')\n"
messages = list(extract.extract(
'python', BytesIO(source.encode()), comment_tags=['NOTE:'],
strip_comment_tags=strip_comment_tags,
))
comments = ['SQL operators' if strip_comment_tags else 'NOTE: SQL operators']
assert [(message, notes) for _, message, notes, _ in messages] == [
('IS', comments),
('NOT', comments),
('NEXT', []),
]


def test_same_line_comments_do_not_leak_into_new_comment_block():
source = BytesIO(b"""\
# NOTE: first pair
_('ONE'); _('TWO')
# NOTE: second pair
_('THREE'); _('FOUR')
""")
messages = list(extract.extract('python', source, comment_tags=['NOTE:']))
assert [(message, notes) for _, message, notes, _ in messages] == [
('ONE', ['NOTE: first pair']),
('TWO', ['NOTE: first pair']),
('THREE', ['NOTE: second pair']),
('FOUR', ['NOTE: second pair']),
]


def test_translator_comments_with_previous_non_translator_comments():
buf = BytesIO(b"""
# This shouldn't be in the output
Expand Down