|
| 1 | +import sys |
| 2 | +import io |
| 3 | +import os |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | +import re |
| 6 | + |
| 7 | +# Define namespaces URIs |
| 8 | +XAML_NS = 'https://github.com/avaloniaui' |
| 9 | +X_NS = 'http://schemas.microsoft.com/winfx/2006/xaml' |
| 10 | + |
| 11 | +def register_namespaces(): |
| 12 | + """Registers namespaces for ElementTree to use when writing the XML file.""" |
| 13 | + ET.register_namespace('', XAML_NS) |
| 14 | + ET.register_namespace('x', X_NS) |
| 15 | + |
| 16 | +def get_locale_dir(): |
| 17 | + """Constructs the absolute path for the locales files""" |
| 18 | + try: |
| 19 | + script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 20 | + project_root = os.path.abspath(os.path.join(script_dir, '.')) |
| 21 | + locales_dir = os.path.join(project_root, 'src', 'Resources', 'Locales') |
| 22 | + except NameError: |
| 23 | + project_root = os.path.abspath(os.getcwd()) |
| 24 | + locales_dir = os.path.join(project_root, 'src', 'Resources', 'Locales') |
| 25 | + |
| 26 | + return locales_dir |
| 27 | + |
| 28 | +def get_locale_files(lang_id): |
| 29 | + """Constructs the absolute paths for the target and reference locale files.""" |
| 30 | + |
| 31 | + # get the locales dir |
| 32 | + locales_dir = get_locale_dir() |
| 33 | + |
| 34 | + # get the target file absolute path |
| 35 | + target_file = os.path.join(locales_dir, f"{lang_id}.axaml") |
| 36 | + |
| 37 | + if not os.path.exists(target_file): |
| 38 | + print(f"Error: Target language file not found at {target_file}") |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + try: |
| 42 | + tree = ET.parse(target_file) |
| 43 | + root = tree.getroot() |
| 44 | + merged_dict = root.find(f"{{{XAML_NS}}}ResourceDictionary.MergedDictionaries") |
| 45 | + if merged_dict is None: |
| 46 | + raise ValueError("Could not find MergedDictionaries tag.") |
| 47 | + |
| 48 | + resource_include = merged_dict.find(f"{{{XAML_NS}}}ResourceInclude") |
| 49 | + if resource_include is None: |
| 50 | + raise ValueError("Could not find ResourceInclude tag.") |
| 51 | + |
| 52 | + include_source = resource_include.get('Source') |
| 53 | + ref_filename_match = re.search(r'([a-zA-Z]{2}_[a-zA-Z]{2}).axaml', include_source) |
| 54 | + if not ref_filename_match: |
| 55 | + raise ValueError("Could not parse reference filename from Source attribute.") |
| 56 | + |
| 57 | + ref_filename = f"{ref_filename_match.group(1)}.axaml" |
| 58 | + ref_file = os.path.join(locales_dir, ref_filename) |
| 59 | + except Exception as e: |
| 60 | + print(f"Error parsing {target_file} to find reference file: {e}") |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + if not os.path.exists(ref_file): |
| 64 | + print(f"Error: Reference language file '{ref_file}' not found.") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + return target_file, ref_file |
| 68 | + |
| 69 | +def get_strings(root): |
| 70 | + """Extracts all translation keys and their text values from an XML root.""" |
| 71 | + strings = {} |
| 72 | + for string_tag in root.findall(f"{{{X_NS}}}String"): |
| 73 | + key = string_tag.get(f"{{{X_NS}}}Key") |
| 74 | + if key: |
| 75 | + strings[key] = string_tag.text if string_tag.text is not None else "" |
| 76 | + return strings |
| 77 | + |
| 78 | +def add_new_string_tag(root, key, value): |
| 79 | + """Adds a new <x:String> tag to the XML root, maintaining some formatting.""" |
| 80 | + |
| 81 | + # create a new tag with Key<>Value |
| 82 | + new_tag = ET.Element(f"{{{X_NS}}}String") |
| 83 | + new_tag.set(f"{{{X_NS}}}Key", key) |
| 84 | + new_tag.set("xml:space", "preserve") |
| 85 | + new_tag.text = value |
| 86 | + |
| 87 | + # try to find the last tag in the |
| 88 | + last_element_index = -1 |
| 89 | + children = list(root) |
| 90 | + |
| 91 | + start = 0 |
| 92 | + end = len(children) - 1 |
| 93 | + middle = -1 |
| 94 | + |
| 95 | + # find the first String tag |
| 96 | + while (children[start].tag != f"{{{X_NS}}}String"): |
| 97 | + start = start + 1 |
| 98 | + |
| 99 | + # find where the insert the new tag |
| 100 | + # so it always keeps the alphabetical order |
| 101 | + while (end - start) > 1: |
| 102 | + middle = int((start + end) / 2) |
| 103 | + |
| 104 | + if (children[middle].tag == f"{{{X_NS}}}String"): |
| 105 | + middle_key = children[middle].get(f"{{{X_NS}}}Key") |
| 106 | + |
| 107 | + if key.lower() < middle_key.lower(): |
| 108 | + end = middle |
| 109 | + else: |
| 110 | + start = middle |
| 111 | + |
| 112 | + # insert after the middle or at the end |
| 113 | + if middle != -1: |
| 114 | + new_tag.tail = root[middle].tail |
| 115 | + root.insert(middle + 1, new_tag) |
| 116 | + else: |
| 117 | + new_tag.tail = "\n " |
| 118 | + root.append(new_tag) |
| 119 | + |
| 120 | +def save_translations(tree, file_path): |
| 121 | + """Saves the XML tree to the file, attempting to preserve formatting.""" |
| 122 | + try: |
| 123 | + ET.indent(tree, space=" ") |
| 124 | + except AttributeError: |
| 125 | + print("Warning: ET.indent not available. Output formatting may not be ideal.") |
| 126 | + |
| 127 | + tree.write(file_path, encoding='utf-8', xml_declaration=False) |
| 128 | + print(f"\nSaved changes to {file_path}") |
| 129 | + |
| 130 | +def main(): |
| 131 | + """Main function to run the translation helper script.""" |
| 132 | + if len(sys.argv) < 2: |
| 133 | + print("Usage: python utils/translate_helper.py <lang_id> [--check]") |
| 134 | + sys.exit(1) |
| 135 | + |
| 136 | + # Force sys.stdin to use UTF-8 decoding |
| 137 | + if sys.stdin.encoding.lower() != 'utf-8': |
| 138 | + print("Changin input encoding to UTF-8") |
| 139 | + sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') |
| 140 | + |
| 141 | + # get arguments |
| 142 | + lang_id = sys.argv[1] |
| 143 | + is_check_mode = len(sys.argv) > 2 and sys.argv[2] == '--check' |
| 144 | + |
| 145 | + # setup XML parser |
| 146 | + register_namespaces() |
| 147 | + |
| 148 | + # try to find XML files |
| 149 | + target_file_path, ref_file_path = get_locale_files(lang_id) |
| 150 | + |
| 151 | + # parse files |
| 152 | + parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) |
| 153 | + target_tree = ET.parse(target_file_path, parser) |
| 154 | + target_root = target_tree.getroot() |
| 155 | + |
| 156 | + ref_tree = ET.parse(ref_file_path) |
| 157 | + ref_root = ref_tree.getroot() |
| 158 | + |
| 159 | + # get all translation keys |
| 160 | + target_strings = get_strings(target_root) |
| 161 | + ref_strings = get_strings(ref_root) |
| 162 | + |
| 163 | + # compute the missing keys between the target and reference files |
| 164 | + missing_keys = sorted([key for key in ref_strings.keys() if key not in target_strings]) |
| 165 | + |
| 166 | + if not missing_keys: |
| 167 | + print("All keys are translated. Nothing to do.") |
| 168 | + return |
| 169 | + |
| 170 | + print(f"Found {len(missing_keys)} missing keys for language '{lang_id}'.") |
| 171 | + |
| 172 | + # running in check mode will only display the missing keys |
| 173 | + if is_check_mode: |
| 174 | + print("Missing keys:") |
| 175 | + for key in missing_keys: |
| 176 | + print(f" - {key}") |
| 177 | + return |
| 178 | + |
| 179 | + # running in normal mode will trigger the translation process |
| 180 | + print("Starting interactive translation...\n") |
| 181 | + changes_made = False |
| 182 | + try: |
| 183 | + # for each missing key |
| 184 | + for i, key in enumerate(missing_keys): |
| 185 | + |
| 186 | + # show the original text |
| 187 | + original_text = ref_strings.get(key, "") |
| 188 | + print("-" * 40) |
| 189 | + print(f"({i+1}/{len(missing_keys)}) Key: '{key}'") |
| 190 | + print(f"Original: '{original_text}'") |
| 191 | + |
| 192 | + # asks for a translated version |
| 193 | + user_input = input("Enter translation (or press Enter to skip, 'q' to save and quit): ") |
| 194 | + |
| 195 | + # if 'q' quit and save |
| 196 | + if user_input.lower() == 'q': |
| 197 | + print("\nQuitting and saving changes...") |
| 198 | + break |
| 199 | + # if valid input, save |
| 200 | + elif user_input: |
| 201 | + add_new_string_tag(target_root, key, user_input) |
| 202 | + changes_made = True |
| 203 | + print(f"Added translation for '{key}'") |
| 204 | + |
| 205 | + except (KeyboardInterrupt, EOFError): |
| 206 | + print("\n\nProcess interrupted. Saving changes...") |
| 207 | + finally: |
| 208 | + # if there was any changes, save back to the target file |
| 209 | + if changes_made: |
| 210 | + save_translations(target_tree, target_file_path) |
| 211 | + else: |
| 212 | + print("\nNo changes were made.") |
| 213 | + |
| 214 | +if __name__ == "__main__": |
| 215 | + main() |
0 commit comments