Skip to content

Commit

Permalink
Merge pull request #40 from transifex/android_v2_escape
Browse files Browse the repository at this point in the history
Android v2 escape
  • Loading branch information
tabac committed May 9, 2016
2 parents 174d9b0 + b1463b9 commit b378bc8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
19 changes: 18 additions & 1 deletion openformats/formats/android.py
Expand Up @@ -21,7 +21,7 @@ class AndroidHandler(Handler):
name = "ANDROID"
extension = "xml"

EXTRACTS_RAW = False
EXTRACTS_RAW = True

# Where to start parsing the file
PARSE_START = "<resources"
Expand Down Expand Up @@ -546,3 +546,20 @@ def _should_ignore(child):
if filter_attr is not None and filter_attr == value:
return True
return False

# Escaping / Unescaping
# According to:
# http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling # noqa

@staticmethod
def escape(string):
return string.replace('\\', '\\\\').replace('"', '\\"').\
replace("'", "\\'")

@staticmethod
def unescape(string):
if len(string) and string[0] == string[-1] == '"':
return string[1:-1]
else:
return string.replace('\\"', '"').replace("\\'", "'").\
replace('\\\\', '\\')
17 changes: 17 additions & 0 deletions openformats/tests/formats/android/test_android.py
Expand Up @@ -618,3 +618,20 @@ def test_strings_from_plurals_are_always_pluralized(self):
'''
)
self.assertTrue(stringset[0].pluralized)

def test_escape(self):
cases = (('double " quote', 'double \\" quote'),
("single ' quote", "single \\' quote"),
("back \\ slash", "back \\\\ slash"))
for rich, raw in cases:
self.assertEquals(AndroidHandler.escape(rich), raw)

def test_unescape(self):
cases = (('double " quote', 'double \\" quote'),
("single ' quote", "single \\' quote"),
("back \\ slash", "back \\\\ slash"),
('inside double quotes', '"inside double quotes"'),
("single ' quote", '"single \' quote"'),
("back \\ slash", '"back \\ slash"'))
for rich, raw in cases:
self.assertEquals(AndroidHandler.unescape(raw), rich)

0 comments on commit b378bc8

Please sign in to comment.