Skip to content

Commit

Permalink
Android: Use generic XML reindentation
Browse files Browse the repository at this point in the history
This makes the code simpler and avoids explicit tail manipulation.
  • Loading branch information
nijel committed Feb 13, 2019
1 parent d9cecc7 commit 19d4b59
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 73 deletions.
13 changes: 2 additions & 11 deletions translate/storage/aresource.py
Expand Up @@ -29,6 +29,7 @@

from translate.lang import data
from translate.misc.deprecation import deprecated
from translate.misc.xml_helpers import reindent
from translate.storage import base, lisa
from translate.misc.multistring import multistring

Expand Down Expand Up @@ -57,7 +58,6 @@ def __init__(self, source, empty=False, xmlelement=None, **kwargs):
self.xmlelement = etree.Element("plurals")
else:
self.xmlelement = etree.Element("string")
self.xmlelement.tail = '\n'
if source is not None:
self.setid(source)
super(AndroidResourceUnit, self).__init__(source)
Expand Down Expand Up @@ -337,7 +337,6 @@ def target(self, target):
if self.xmlelement.tag != "plurals":
old_id = self.getid()
self.xmlelement = etree.Element("plurals")
self.xmlelement.tail = '\n'
self.setid(old_id)

locale = self.gettargetlanguage().replace('_', '-').split('-')[0]
Expand Down Expand Up @@ -366,16 +365,12 @@ def target(self, target):
item = etree.Element("item")
item.set("quantity", plural_tag)
self.set_xml_text_value(plural_string, item)
item.tail = "\n "
self.xmlelement.append(item)
# Remove the tab from last item
item.tail = "\n"
else:
# Fix the root tag if mismatching
if self.xmlelement.tag != "string":
old_id = self.getid()
self.xmlelement = etree.Element("string")
self.xmlelement.tail = '\n'
self.setid(old_id)

self.set_xml_text_value(target, self.xmlelement)
Expand Down Expand Up @@ -459,6 +454,7 @@ def initbody(self):
def serialize(self, out=None):
"""Converts to a string containing the file's XML"""
out.write(b'<?xml version="1.0" encoding="utf-8"?>\n')
reindent(self.document.getroot(), indent=" ", max_level=3)
self.document.write(out, pretty_print=False, xml_declaration=False,
encoding='utf-8')

Expand Down Expand Up @@ -515,11 +511,6 @@ def addunit(self, unit, new=True):
do_cleanup = True
newns[ns] = unit.xmlelement.nsmap[ns]

# Fixup formatting
if len(self.body):
self.body[-1].tail = '\n '
unit.xmlelement.tail = '\n'

super(AndroidResourceFile, self).addunit(unit, new)
# Move aliased namespaces to the <resources> tag
# The top_nsmap was introduced in LXML 3.5.0
Expand Down

0 comments on commit 19d4b59

Please sign in to comment.