Skip to content

Commit f1c8633

Browse files
gabe-shermantomasr8akx
authored
PO files: Consider a message without a translation to have an empty translation (#1135)
Fixes an index out of bounds error in add_message. Co-authored-by: Tomas R <[email protected]> Co-authored-by: Aarni Koskela <[email protected]>
1 parent b246a5f commit f1c8633

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

babel/messages/pofile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ def _add_message(self) -> None:
247247

248248
def _finish_current_message(self) -> None:
249249
if self.messages:
250+
if not self.translations:
251+
self._invalid_pofile("", self.offset, f"missing msgstr for msgid '{self.messages[0].denormalize()}'")
252+
self.translations.append([0, _NormalizedString("")])
250253
self._add_message()
251254

252255
def _process_message_line(self, lineno, line, obsolete=False) -> None:

tests/messages/test_pofile.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,20 @@ def test_issue_1087():
10141014
"Language: \n"
10151015
''')
10161016
assert pofile.read_po(buf).locale is None
1017+
1018+
1019+
@pytest.mark.parametrize("case", ['msgid "foo"', 'msgid "foo"\nmsgid_plural "foos"'])
1020+
@pytest.mark.parametrize("abort_invalid", [False, True])
1021+
def test_issue_1134(case: str, abort_invalid: bool):
1022+
buf = StringIO(case)
1023+
1024+
if abort_invalid:
1025+
# Catalog not created, aborted with PoFileError
1026+
with pytest.raises(pofile.PoFileError) as excinfo:
1027+
pofile.read_po(buf, abort_invalid=True)
1028+
assert str(excinfo.value) == "missing msgstr for msgid 'foo' on 0"
1029+
else:
1030+
# Catalog is created with warning, no abort
1031+
output = pofile.read_po(buf)
1032+
assert len(output) == 1
1033+
assert output["foo"].string in ((''), ('', ''))

0 commit comments

Comments
 (0)