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
7 changes: 4 additions & 3 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
\%
(?:\(([\w]*)\))?
(
[-#0\ +]?(?:\*|[\d]+)?
(?:\.(?:\*|[\d]+))?
[hlL]?
(?:[-#0+]?(?:\*|[\d]+)?(?:\.(?:\*|[\d]+))?)
|
(?:\ +(?:\.[\d]+|[\d]+(?:\.[\d]+)?))
)
[hlL]?
([diouxXeEfFgGcrs%])
''',
re.VERBOSE,
Expand Down
12 changes: 12 additions & 0 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def test_message_python_format():
assert catalog.PYTHON_FORMAT.search('foo %()s')


def test_message_python_format_prose_percent():
# literal percent signs in prose must not be mistaken for placeholders
assert not catalog.PYTHON_FORMAT.search('100 % done')
assert not catalog.PYTHON_FORMAT.search('50 % off')
assert not catalog.PYTHON_FORMAT.search('10% of')
assert not catalog.PYTHON_FORMAT.search('10% der')
# space-flag placeholders with explicit width/precision are still valid
assert catalog.PYTHON_FORMAT.search('% 5d')
assert catalog.PYTHON_FORMAT.search('% .2f')
assert catalog.PYTHON_FORMAT.search('% 5.2f')


def test_message_python_brace_format():
assert not catalog._has_python_brace_format('')
assert not catalog._has_python_brace_format('foo')
Expand Down
6 changes: 6 additions & 0 deletions tests/messages/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def test_python_format_valid(msgid, msgstr):
'%(foo)d',
"incompatible format for placeholder 'foo': 'd' and 's' are not compatible",
),
('% 5d', '% 5s', "incompatible format for placeholder 1: 'd' and 's' are not compatible"),
],
)
def test__validate_format_invalid(msgid, msgstr, error):
Expand All @@ -412,6 +413,11 @@ def test__validate_format_invalid(msgid, msgstr, error):
('%(foo)s', 'foo'),
('%(foo)s', '%(foo)s %(foo)s'),
('%(bar)s foo %(n)d', '%(n)d foo %(bar)s'),
# literal percent signs in prose are not placeholders
('100 % done', '100 % erledigt'),
('50 % off', '50 % Rabatt'),
# space-flag placeholders with explicit width/precision are still checked
('% 5d', '% 5d'),
],
)
def test__validate_format_valid(msgid, msgstr):
Expand Down