-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings_xml_to_localizable.py
69 lines (57 loc) · 2.89 KB
/
strings_xml_to_localizable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
import re
def transform_android_string(android_string):
transformed = re.sub(r'(?:<string name=\"|<string name = \")', '\"', android_string) #convert string tags
transformed = re.sub(r'\">', '\" = ', transformed)
transformed = re.sub(r'</string>', ';', transformed)
transformed = re.sub(r'<!--', '/*', transformed) #convert xml comments
transformed = re.sub(r'-->', '*/', transformed)
transformed = re.sub(r'<resources>', '', transformed) #strip resource tags
transformed = re.sub(r'</resources>', '', transformed)
#todo: universal conversion of all entities
transformed = re.sub(r'…', '\\U2026', transformed) #convert xml entity ellipse to unicode
return transformed
def convert_android_to_ios(input_file_name, output_file_name):
inputfile = open(input_file_name,'r')
outputfile = open(output_file_name, 'w')
outputfile.write(transform_android_string(inputfile.read()))
outputfile.close()
inputfile.close()
def assert_true(condition, message):
if condition:
print "OK " + message
else:
print "FAIL " + message
def test_convert_string_tag():
assert_true(transform_android_string("""<string name="test_string">"test string"</string>""") == """"test_string" = "test string";""", "convert string")
assert_true(transform_android_string("""<string name="test_string">"test \\"string\\""</string>""") == """"test_string" = "test \\"string\\"";""", "convert string with escaped quotes")
def test_convert_ellipse_entity():
assert_true(transform_android_string("""…""") == """\\U2026""", "convert ellipse entity")
assert_true(transform_android_string("""………""") == """\\U2026\\U2026\\U2026""", "convert ellipse entity multiple")
def test_strip_resource_tags():
assert_true(transform_android_string("""<resources>resource tags</resources>""") == """resource tags""", "strip resource tags")
def test_convert_comment():
assert_true(transform_android_string("""<!-- comment -->""") == """/* comment */""", "convert comment")
assert_true(transform_android_string("""<!-- comment --> <!-- comment -->""") == """/* comment */ /* comment */""", "convert comment multiple")
def run_all_tests():
test_convert_string_tag()
test_convert_comment()
test_strip_resource_tags()
test_convert_ellipse_entity()
# Very simple tool to convert an android strings.xml file to an iOS Localizable.strings file
# This does not currently handle complex android strings
def main(argv):
num_args = len(argv)
if num_args == 1 and argv[0] == "-test":
run_all_tests()
sys.exit(0)
if num_args != 2:
print 'Converts an Android strings.xml to an iOS Localizable.strings'
print 'Assumes a well formed strings.xml with quotes around each string value. Run clean_android_strings.py first, to ensure.'
print 'Usage: <input.xml> <output.strings>'
print ' or'
print 'Usage: -test (run all tests)'
sys.exit(2)
convert_android_to_ios(argv[0], argv[1])
if __name__ == "__main__":
main(sys.argv[1:])