Skip to content

Commit

Permalink
Xliff: Reindent when saving
Browse files Browse the repository at this point in the history
Use generic XML indentation code to achieve consistent indentation after
changing content.

Fixes translate#3424
  • Loading branch information
nijel committed Feb 13, 2019
1 parent 19d4b59 commit 40e84d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions translate/storage/test_xliff.py
Expand Up @@ -479,3 +479,34 @@ def test_multiple_filenodes(self):
assert newxfile.getfilenode("file0") is not None
assert newxfile.getfilenode("file1") is not None
assert not newxfile.getfilenode("foo")

def test_indent(self):
xlfsource = b'''<?xml version='1.0' encoding='UTF-8'?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file original="doc.txt" source-language="en-US">
<body>
<trans-unit id="1" xml:space="preserve">
<source>File</source>
<target/>
</trans-unit>
</body>
</file>
</xliff>
'''
xlfsourcenote = b'''<?xml version='1.0' encoding='UTF-8'?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file original="doc.txt" source-language="en-US">
<body>
<trans-unit id="1" xml:space="preserve">
<source>File</source>
<target/>
<note>Test note</note>
</trans-unit>
</body>
</file>
</xliff>
'''
xfile = xliff.xlifffile.parsestring(xlfsource)
assert bytes(xfile) == xlfsource
xfile.units[0].addnote('Test note')
assert bytes(xfile) == xlfsourcenote
3 changes: 2 additions & 1 deletion translate/storage/xliff.py
Expand Up @@ -27,7 +27,7 @@

from translate.misc.deprecation import deprecated
from translate.misc.multistring import multistring
from translate.misc.xml_helpers import getXMLspace, setXMLlang, setXMLspace
from translate.misc.xml_helpers import getXMLspace, setXMLlang, setXMLspace, reindent
from translate.storage import base, lisa
from translate.storage.placeables.lisa import strelem_to_xml, xml_to_strelem
from translate.storage.workflow import StateEnum as state
Expand Down Expand Up @@ -872,6 +872,7 @@ def creategroup(self, filename="NoName", createifmissing=False, restype=None):

def serialize(self, out):
self.removedefaultfile()
reindent(self.document.getroot(), indent=" ", max_level=4)
super(xlifffile, self).serialize(out)

@classmethod
Expand Down

0 comments on commit 40e84d3

Please sign in to comment.