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
6 changes: 5 additions & 1 deletion babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,11 @@ def _set_mime_headers(self, headers: Iterable[tuple[str, str]]) -> None:
self.charset = params['charset'].lower()
elif name == 'plural-forms':
params = parse_separated_header(f" ;{value}")
self._num_plurals = int(params.get('nplurals', 2))
num_plurals = params.get('nplurals', 2)
try:
self._num_plurals = int(num_plurals)
except (TypeError, ValueError):
self._num_plurals = 2
self._plural_expr = params.get('plural', '(n != 1)')
elif name == 'pot-creation-date':
# Keep last value instead of crashing on blank values
Expand Down
20 changes: 20 additions & 0 deletions tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ def test_read_po_preserves_repeated_comment_lines(prefix, attribute, obsolete):
assert getattr(message, attribute) == comments


def test_read_po_tolerates_placeholder_plural_forms():
source = (
'msgid ""\n'
'msgstr ""\n'
'"Content-Type: text/plain; charset=UTF-8\\n"\n'
'"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n"\n'
"\n"
'msgid "Hello"\n'
'msgid_plural "Hellos"\n'
'msgstr[0] ""\n'
'msgstr[1] ""\n'
)

catalog = pofile.read_po(StringIO(source))

assert catalog.num_plurals == 2
assert catalog.plural_expr == 'EXPRESSION'
assert catalog['Hello'].id == ('Hello', 'Hellos')


@pytest.mark.parametrize('attribute', ['auto_comments', 'user_comments'])
def test_wrapped_comments_roundtrip(attribute):
catalog = Catalog()
Expand Down