Skip to content

Commit

Permalink
Check all plurals in python format checker
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Feb 4, 2025
1 parent 27e7303 commit 48ce7bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 6 additions & 3 deletions babel/messages/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ def python_format(catalog: Catalog | None, message: Message) -> None:
if not isinstance(msgstrs, (list, tuple)):
msgstrs = (msgstrs,)

for msgid, msgstr in zip(msgids, msgstrs):
if msgstr:
_validate_format(msgid, msgstr)
if msgstrs[0]:
_validate_format(msgids[0], msgstrs[0])
if message.pluralizable:
for msgstr in msgstrs[1:]:
if msgstr:
_validate_format(msgids[1], msgstr)


def _validate_format(format: str, alternative: str) -> None:
Expand Down
8 changes: 7 additions & 1 deletion tests/messages/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ class TestPythonFormat:
(('foo %s', 'bar'), ('foo', 'bar')),
(('foo', 'bar %s'), ('foo', 'bar')),
(('foo %s', 'bar'), ('foo')),
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz')),
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz %d', 'qux')),
])
def test_python_format_invalid(self, msgid, msgstr):
msg = Message(msgid, msgstr)
Expand All @@ -346,9 +348,13 @@ def test_python_format_invalid(self, msgid, msgstr):
@pytest.mark.parametrize(('msgid', 'msgstr'), [
('foo', 'foo'),
('foo', 'foo %s'),
('foo %s', ''),
(('foo %s', 'bar %d'), ('foo %s', 'bar %d')),
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz')),
(('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz %d')),
(('foo', 'bar %s'), ('foo')),
(('foo', 'bar %s'), ('', '')),
(('foo', 'bar %s'), ('foo', '')),
(('foo %s', 'bar %d'), ('foo %s', '')),
])
def test_python_format_valid(self, msgid, msgstr):
msg = Message(msgid, msgstr)
Expand Down

0 comments on commit 48ce7bf

Please sign in to comment.