Skip to content

Commit

Permalink
po2resx - Fixing an issue around comments being duplicated if there i…
Browse files Browse the repository at this point in the history
…s leading or trailing whitespace. Added unit test to cover this scenario.
  • Loading branch information
sjhale authored and unho committed May 11, 2015
1 parent 40ab1e7 commit b880dce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 18 additions & 0 deletions translate/convert/test_po2resx.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ def test_automaticcomments_existingduplicatecomment(self):
resx_file = self.po2resx(resx_template, po_source)
assert resx_file == expected_output

def test_automaticcomments_existingduplicatecommentwithwhitespace(self):
""" Tests there is no duplication of automatic comments if it already exists, hasn't changed but has leading or
trailing whitespaces """
po_source = r'''#. This is an existing comment with leading and trailing spaces
#: ResourceKey
msgid "Bézier curve"
msgstr "Bézier-kurwe"
'''
resx_template = self.XMLskeleton % '''<data name="ResourceKey" xml:space="preserve">
<value></value>
<comment> This is an existing comment with leading and trailing spaces </comment></data>'''
expected_output = self.XMLskeleton % '''<data name="ResourceKey" xml:space="preserve">
<value>Bézier-kurwe</value>
<comment>This is an existing comment with leading and trailing spaces</comment></data>'''
resx_file = self.po2resx(resx_template, po_source)
assert resx_file == expected_output

def test_translatorcomments(self):
""" Tests that translator comments are imported """
po_source = r'''# This is a translator comment : 22.12.14
Expand Down Expand Up @@ -410,6 +427,7 @@ def test_existingcomments(self):
resx_file = self.po2resx(resx_template, po_source)
assert resx_file == expected_output


class TestPO2TSCommand(test_convert.TestConvertCommand, TestPO2RESX):
""" Tests running actual po2ts commands on files """
convertmodule = po2resx
Expand Down
9 changes: 5 additions & 4 deletions translate/storage/resx.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ def addnote(self, text, origin=None, position="append"):
current_notes = self.getnotes(origin)
self.removenotes(origin)
note = etree.SubElement(self.xmlelement, self.namespaced("comment"))
text_stripped = text.strip()
if position == "append":
if current_notes in text:
if current_notes.strip() in text_stripped:
# Don't add duplicate comments
note.text = text.strip()
note.text = text_stripped
else:
note.text = "\n".join(filter(None, [current_notes, text.strip()]))
note.text = "\n".join(filter(None, [current_notes, text_stripped]))
else:
note.text = text.strip()
note.text = text_stripped

def getnotes(self, origin=None):
comments = []
Expand Down

0 comments on commit b880dce

Please sign in to comment.