Skip to content

Commit a37d4f0

Browse files
committed
feature: Added code comments to translation utility script
1 parent a15ca9f commit a37d4f0

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

utils/translate_helper.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def register_namespaces():
1212
ET.register_namespace('', XAML_NS)
1313
ET.register_namespace('x', X_NS)
1414

15-
def get_locale_files(lang_id):
16-
"""Constructs the absolute paths for the target and reference locale files."""
15+
def get_locale_dir():
16+
"""Constructs the absolute path for the locales files"""
1717
try:
1818
script_dir = os.path.dirname(os.path.realpath(__file__))
1919
project_root = os.path.abspath(os.path.join(script_dir, '..'))
@@ -22,6 +22,15 @@ def get_locale_files(lang_id):
2222
project_root = os.path.abspath(os.getcwd())
2323
locales_dir = os.path.join(project_root, 'src', 'Resources', 'Locales')
2424

25+
return locales_dir
26+
27+
def get_locale_files(lang_id):
28+
"""Constructs the absolute paths for the target and reference locale files."""
29+
30+
# get the locales dir
31+
locales_dir = get_locale_dir()
32+
33+
# get the target file absolute path
2534
target_file = os.path.join(locales_dir, f"{lang_id}.axaml")
2635

2736
if not os.path.exists(target_file):
@@ -67,11 +76,14 @@ def get_strings(root):
6776

6877
def add_new_string_tag(root, key, value):
6978
"""Adds a new <x:String> tag to the XML root, maintaining some formatting."""
79+
80+
# create a new tag with Key<>Value
7081
new_tag = ET.Element(f"{{{X_NS}}}String")
7182
new_tag.set(f"{{{X_NS}}}Key", key)
7283
new_tag.set("xml:space", "preserve")
7384
new_tag.text = value
7485

86+
# try to find the last tag in the
7587
last_element_index = -1
7688
children = list(root)
7789
for i in range(len(children) - 1, -1, -1):
@@ -99,27 +111,33 @@ def save_translations(tree, file_path):
99111

100112
def main():
101113
"""Main function to run the translation helper script."""
102-
register_namespaces()
103-
104114
if len(sys.argv) < 2:
105115
print("Usage: python utils/translate_helper.py <lang_id> [--check]")
106116
sys.exit(1)
107117

118+
# get arguments
108119
lang_id = sys.argv[1]
109120
is_check_mode = len(sys.argv) > 2 and sys.argv[2] == '--check'
110121

122+
# setup XML parser
123+
register_namespaces()
124+
125+
# try to find XML files
111126
target_file_path, ref_file_path = get_locale_files(lang_id)
112127

128+
# parse files
113129
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
114130
target_tree = ET.parse(target_file_path, parser)
115131
target_root = target_tree.getroot()
116-
132+
117133
ref_tree = ET.parse(ref_file_path)
118134
ref_root = ref_tree.getroot()
119135

136+
# get all translation keys
120137
target_strings = get_strings(target_root)
121138
ref_strings = get_strings(ref_root)
122139

140+
# compute the missing keys between the target and reference files
123141
missing_keys = sorted([key for key in ref_strings.keys() if key not in target_strings])
124142

125143
if not missing_keys:
@@ -128,26 +146,34 @@ def main():
128146

129147
print(f"Found {len(missing_keys)} missing keys for language '{lang_id}'.")
130148

149+
# running in check mode will only display the missing keys
131150
if is_check_mode:
132151
print("Missing keys:")
133152
for key in missing_keys:
134153
print(f" - {key}")
135154
return
136155

156+
# running in normal mode will trigger the translation process
137157
print("Starting interactive translation...\n")
138158
changes_made = False
139159
try:
160+
# for each missing key
140161
for i, key in enumerate(missing_keys):
162+
163+
# show the original text
141164
original_text = ref_strings.get(key, "")
142165
print("-" * 40)
143166
print(f"({i+1}/{len(missing_keys)}) Key: '{key}'")
144167
print(f"Original: '{original_text}'")
145168

169+
# asks for a translated version
146170
user_input = input("Enter translation (or press Enter to skip, 'q' to save and quit): ")
147171

172+
# if 'q' quit and save
148173
if user_input.lower() == 'q':
149174
print("\nQuitting and saving changes...")
150175
break
176+
# if valid input, save
151177
elif user_input:
152178
add_new_string_tag(target_root, key, user_input)
153179
changes_made = True
@@ -156,10 +182,11 @@ def main():
156182
except (KeyboardInterrupt, EOFError):
157183
print("\n\nProcess interrupted. Saving changes...")
158184
finally:
185+
# if there was any changes, save back to the target file
159186
if changes_made:
160187
save_translations(target_tree, target_file_path)
161188
else:
162189
print("\nNo changes were made.")
163190

164191
if __name__ == "__main__":
165-
main()
192+
main()

0 commit comments

Comments
 (0)