Add script to fix problematic linebreak/whitespace combinations in strings.xml
This commit is contained in:
parent
9b807c325d
commit
85e55e5408
2 changed files with 87 additions and 0 deletions
11
tools/fix_all_strings.sh
Executable file
11
tools/fix_all_strings.sh
Executable file
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
SCRIPT=$(readlink -f $0)
|
||||
SCRIPTPATH=`dirname $SCRIPT`
|
||||
PROJECTROOT=`dirname $SCRIPTPATH`
|
||||
|
||||
cd $PROJECTROOT
|
||||
|
||||
find res/values* -name "strings.xml" -type f ! -wholename "res/values-fr-rCA/strings.xml" -exec ./tools/fix_strings.py {} \;
|
||||
|
||||
cd -
|
76
tools/fix_strings.py
Executable file
76
tools/fix_strings.py
Executable file
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/env python
|
||||
# Rewrite a strings file to get rid of line break/whitespace combinations that get stripped when building with Gradle.
|
||||
#
|
||||
# Example:
|
||||
# <string name="account_size_changed">
|
||||
# Account \"<xliff:g id="account">%s</xliff:g>\" shrunk from
|
||||
# <xliff:g id="oldSize">%s</xliff:g>
|
||||
# to
|
||||
# <xliff:g id="newSize">%s</xliff:g>
|
||||
# </string>
|
||||
#
|
||||
# will be rendered as
|
||||
#
|
||||
# Account "account" shrunk from10MB to1MB
|
||||
#
|
||||
# when built with Gradle, but displays fine when built with Ant.
|
||||
#
|
||||
#
|
||||
# Written for use with K-9 Mail (https://github.com/k9mail/k-9)
|
||||
# Licensed under the WTFPL (http://www.wtfpl.net/about/)
|
||||
|
||||
import sys
|
||||
import re
|
||||
from lxml import etree
|
||||
|
||||
|
||||
def fix_text(element):
|
||||
if element.text is not None:
|
||||
element.text = re.sub(r'^\n\s*([^\s])', "\\1", element.text)
|
||||
element.text = re.sub(r'\n\s*$', " ", element.text)
|
||||
|
||||
|
||||
def fix_tail(element, is_last):
|
||||
if element.tail is not None:
|
||||
if is_last:
|
||||
replacement = ""
|
||||
else:
|
||||
replacement = " "
|
||||
element.tail = re.sub(r'^\n\s*([^\s])', " \\1", element.tail)
|
||||
element.tail = re.sub(r'\n\s*$', replacement, element.tail)
|
||||
|
||||
|
||||
def cleanup_string_elements(elements):
|
||||
for element in elements:
|
||||
if element.tag is None:
|
||||
continue
|
||||
|
||||
tag = element.tag
|
||||
children = element.getchildren()
|
||||
|
||||
if tag in ["string", "item"]:
|
||||
if len(children) > 0:
|
||||
fix_text(element)
|
||||
|
||||
for child in children:
|
||||
if isinstance(child.tag, basestring):
|
||||
fix_text(child)
|
||||
fix_tail(child, child == children[-1])
|
||||
|
||||
elif tag == "plurals":
|
||||
cleanup_string_elements(children)
|
||||
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print "Usage: fix_strings.py <strings file>"
|
||||
print "Example: fix_strings.py res/values/strings.xml"
|
||||
exit(1)
|
||||
|
||||
strings_file = sys.argv[1]
|
||||
|
||||
parser = etree.XMLParser(strip_cdata=False)
|
||||
strings = etree.parse(strings_file, parser=parser)
|
||||
|
||||
cleanup_string_elements(strings.getroot().getchildren())
|
||||
|
||||
strings.write(strings_file, xml_declaration=True, encoding="UTF-8", pretty_print=True)
|
Loading…
Reference in a new issue