Skip to content

Commit 2ebc47e

Browse files
authored
Allow parsing .po files that have an extant but empty Language header (#1101)
Fixes #1087
1 parent ad0fabe commit 2ebc47e

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

babel/messages/catalog.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,11 @@ def _set_mime_headers(self, headers: Iterable[tuple[str, str]]) -> None:
479479
self.last_translator = value
480480
elif name == 'language':
481481
value = value.replace('-', '_')
482-
self._set_locale(value)
482+
# The `or None` makes sure that the locale is set to None
483+
# if the header's value is an empty string, which is what
484+
# some tools generate (instead of eliding the empty Language
485+
# header altogether).
486+
self._set_locale(value or None)
483487
elif name == 'language-team':
484488
self.language_team = value
485489
elif name == 'content-type':

tests/messages/test_pofile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,12 @@ def test_iterable_of_strings():
893893
catalog = pofile.read_po(['msgid "foo"', b'msgstr "Voh"'], locale="en_US")
894894
assert catalog.locale == Locale("en", "US")
895895
assert catalog.get("foo").string == "Voh"
896+
897+
898+
def test_issue_1087():
899+
buf = StringIO(r'''
900+
msgid ""
901+
msgstr ""
902+
"Language: \n"
903+
''')
904+
assert pofile.read_po(buf).locale is None

0 commit comments

Comments
 (0)