| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 | |
| 3 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 # Usage: generate_localizer [xib_path] [output_dot_h_path] [output_dot_mm_path] | |
| 8 # | |
| 9 # Extracts all the localizable strings that start with "^IDS" from the given | |
| 10 # xib file, and then generates a localizer to process those strings. | |
| 11 | |
| 12 import os.path | |
| 13 import plistlib | |
| 14 import subprocess | |
| 15 import sys | |
| 16 | |
| 17 generate_localizer = "me" | |
| 18 | |
| 19 localizer_template_h = \ | |
| 20 '''// ---------- WARNING ---------- | |
| 21 // THIS IS A GENERATED FILE, DO NOT EDIT IT DIRECTLY! | |
| 22 // | |
| 23 // This header includes the table used by ui_localizer.mm. Nothing else should | |
| 24 // be including this file. | |
| 25 // | |
| 26 // Generated by %(generate_localizer)s. | |
| 27 // Generated from: | |
| 28 // %(xib_files)s | |
| 29 // | |
| 30 | |
| 31 #ifndef UI_LOCALIZER_TABLE_H_ | |
| 32 #define UI_LOCALIZER_TABLE_H_ | |
| 33 | |
| 34 static const UILocalizerResourceMap kUIResources[] = { | |
| 35 %(resource_map_list)s }; | |
| 36 static const size_t kUIResourcesSize = arraysize(kUIResources); | |
| 37 | |
| 38 #endif // UI_LOCALIZER_TABLE_H_ | |
| 39 ''' | |
| 40 | |
| 41 def xib_localizable_strings(xib_path): | |
| 42 """Runs ibtool to extract the localizable strings data from the xib.""" | |
| 43 ibtool_cmd = subprocess.Popen(['/usr/bin/ibtool', '--localizable-strings', | |
| 44 xib_path], | |
| 45 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| 46 (cmd_out, cmd_err) = ibtool_cmd.communicate() | |
| 47 if ibtool_cmd.returncode: | |
| 48 sys.stderr.write('%s:0: error: ibtool on "%s" failed (%d):\n%s\n' % | |
| 49 (generate_localizer, xib_path, ibtool_cmd.returncode, | |
| 50 cmd_err)) | |
| 51 return None | |
| 52 return cmd_out | |
| 53 | |
| 54 def extract_resource_constants(plist_localizable_strings_dict, xib_path): | |
| 55 """Extracts all the values that start with ^IDS from the localizable | |
| 56 strings plist entry.""" | |
| 57 constants_list = [] | |
| 58 for item_dict in plist_localizable_strings_dict.itervalues(): | |
| 59 for item_value in item_dict.itervalues(): | |
| 60 if item_value.startswith('^IDS'): | |
| 61 constants_list.append(item_value) | |
| 62 elif item_value.startswith('IDS'): | |
| 63 sys.stderr.write( | |
| 64 '%s:0: warning: %s found a string with questionable prefix, "%s"\n' | |
| 65 % (xib_path, generate_localizer, item_value)); | |
| 66 return constants_list | |
| 67 | |
| 68 def generate_file_contents(constants_list, xib_paths): | |
| 69 """Generates the header listing the constants.""" | |
| 70 # Bounce through a set to uniq the strings, sort the list, then build the | |
| 71 # values we need from it. | |
| 72 constants_list = sorted(set(constants_list)) | |
| 73 constant_list_str = '' | |
| 74 for item in constants_list: | |
| 75 parts = item.split('$', 1) | |
| 76 label_id = parts[0] | |
| 77 if len(parts) == 2: | |
| 78 label_arg_id = parts[1] | |
| 79 else: | |
| 80 label_arg_id = '0' | |
| 81 constant_list_str += ' { "%s", %s, %s },\n' % \ | |
| 82 ( item, label_id[1:], label_arg_id) | |
| 83 # Assemble the contents from the templates. | |
| 84 values_dict = { | |
| 85 'resource_map_list': constant_list_str, | |
| 86 'generate_localizer': generate_localizer, | |
| 87 'xib_files': "\n// ".join(xib_paths), | |
| 88 } | |
| 89 h_file = localizer_template_h % values_dict | |
| 90 return h_file | |
| 91 | |
| 92 | |
| 93 def Main(argv=None): | |
| 94 global generate_localizer | |
| 95 generate_localizer = os.path.basename(argv[0]) | |
| 96 | |
| 97 # Args | |
| 98 if len(argv) < 3: | |
| 99 sys.stderr.write('%s:0: error: Expected output file and then xibs\n' % | |
| 100 generate_localizer); | |
| 101 return 1 | |
| 102 output_path = argv[1]; | |
| 103 xib_paths = argv[2:] | |
| 104 | |
| 105 full_constants_list = [] | |
| 106 for xib_path in xib_paths: | |
| 107 # Run ibtool and convert to something Python can deal with | |
| 108 plist_string = xib_localizable_strings(xib_path) | |
| 109 if not plist_string: | |
| 110 return 2 | |
| 111 plist = plistlib.readPlistFromString(plist_string) | |
| 112 | |
| 113 # Extract the resource constant strings | |
| 114 localizable_strings = plist['com.apple.ibtool.document.localizable-strings'] | |
| 115 constants_list = extract_resource_constants(localizable_strings, xib_path) | |
| 116 if not constants_list: | |
| 117 sys.stderr.write("%s:0: warning: %s didn't find any resource strings\n" % | |
| 118 (xib_path, generate_localizer)); | |
| 119 full_constants_list.extend(constants_list) | |
| 120 | |
| 121 # Generate our file contents | |
| 122 h_file_content = \ | |
| 123 generate_file_contents(full_constants_list, xib_paths) | |
| 124 | |
| 125 # Write out the file | |
| 126 file_fd = open(output_path, 'w') | |
| 127 file_fd.write(h_file_content) | |
| 128 file_fd.close() | |
| 129 | |
| 130 return 0 | |
| 131 | |
| 132 if __name__ == '__main__': | |
| 133 sys.exit(Main(sys.argv)) | |
| OLD | NEW |