From 2e6113cefad188c5dd6a9bc32670867fbd60e9f1 Mon Sep 17 00:00:00 2001 From: Adam Ratana Date: Sat, 16 Feb 2013 13:49:45 -0500 Subject: [PATCH] added tests, removed redundant clean_android_strings cleanup --- strings_xml_to_localizable.py | 46 +++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/strings_xml_to_localizable.py b/strings_xml_to_localizable.py index 798c267..6f7a975 100644 --- a/strings_xml_to_localizable.py +++ b/strings_xml_to_localizable.py @@ -3,12 +3,13 @@ def transform_android_string(android_string): transformed = re.sub(r'(?:\"?', '\" = \"', transformed) - transformed = re.sub(r'\"?', '\";', transformed) + transformed = re.sub(r'\">', '\" = ', transformed) + transformed = re.sub(r'', ';', transformed) transformed = re.sub(r'', '*/', transformed) transformed = re.sub(r'', '', transformed) #strip resource tags transformed = re.sub(r'', '', transformed) + #todo: universal conversion of all entities transformed = re.sub(r'…', '\\U2026', transformed) #convert xml entity ellipse to unicode return transformed @@ -19,15 +20,50 @@ def convert_android_to_ios(input_file_name, output_file_name): 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(""""test string"""") == """"test_string" = "test string";""", "convert string") + assert_true(transform_android_string(""""test \\"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("""resource tags""") == """resource tags""", "strip resource tags") + +def test_convert_comment(): + assert_true(transform_android_string("""""") == """/* comment */""", "convert comment") + assert_true(transform_android_string(""" """) == """/* 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(sys.argv) - if num_args != 3: + 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: ' + print ' or' + print 'Usage: -test (run all tests)' sys.exit(2) - convert_android_to_ios(sys.argv[1], sys.argv[2]) + convert_android_to_ios(argv[0], argv[1]) if __name__ == "__main__": main(sys.argv[1:]) \ No newline at end of file