Skip to content

Commit

Permalink
Allow parsing .po files that have an extant but empty Language header (
Browse files Browse the repository at this point in the history
  • Loading branch information
akx authored Jul 23, 2024
1 parent ad0fabe commit 2ebc47e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,11 @@ def _set_mime_headers(self, headers: Iterable[tuple[str, str]]) -> None:
self.last_translator = value
elif name == 'language':
value = value.replace('-', '_')
self._set_locale(value)
# The `or None` makes sure that the locale is set to None
# if the header's value is an empty string, which is what
# some tools generate (instead of eliding the empty Language
# header altogether).
self._set_locale(value or None)
elif name == 'language-team':
self.language_team = value
elif name == 'content-type':
Expand Down
9 changes: 9 additions & 0 deletions tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,12 @@ def test_iterable_of_strings():
catalog = pofile.read_po(['msgid "foo"', b'msgstr "Voh"'], locale="en_US")
assert catalog.locale == Locale("en", "US")
assert catalog.get("foo").string == "Voh"


def test_issue_1087():
buf = StringIO(r'''
msgid ""
msgstr ""
"Language: \n"
''')
assert pofile.read_po(buf).locale is None

0 comments on commit 2ebc47e

Please sign in to comment.