@@ -12,8 +12,8 @@ def register_namespaces():
12
12
ET .register_namespace ('' , XAML_NS )
13
13
ET .register_namespace ('x' , X_NS )
14
14
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"""
17
17
try :
18
18
script_dir = os .path .dirname (os .path .realpath (__file__ ))
19
19
project_root = os .path .abspath (os .path .join (script_dir , '..' ))
@@ -22,6 +22,15 @@ def get_locale_files(lang_id):
22
22
project_root = os .path .abspath (os .getcwd ())
23
23
locales_dir = os .path .join (project_root , 'src' , 'Resources' , 'Locales' )
24
24
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
25
34
target_file = os .path .join (locales_dir , f"{ lang_id } .axaml" )
26
35
27
36
if not os .path .exists (target_file ):
@@ -67,11 +76,14 @@ def get_strings(root):
67
76
68
77
def add_new_string_tag (root , key , value ):
69
78
"""Adds a new <x:String> tag to the XML root, maintaining some formatting."""
79
+
80
+ # create a new tag with Key<>Value
70
81
new_tag = ET .Element (f"{{{ X_NS } }}String" )
71
82
new_tag .set (f"{{{ X_NS } }}Key" , key )
72
83
new_tag .set ("xml:space" , "preserve" )
73
84
new_tag .text = value
74
85
86
+ # try to find the last tag in the
75
87
last_element_index = - 1
76
88
children = list (root )
77
89
for i in range (len (children ) - 1 , - 1 , - 1 ):
@@ -99,27 +111,33 @@ def save_translations(tree, file_path):
99
111
100
112
def main ():
101
113
"""Main function to run the translation helper script."""
102
- register_namespaces ()
103
-
104
114
if len (sys .argv ) < 2 :
105
115
print ("Usage: python utils/translate_helper.py <lang_id> [--check]" )
106
116
sys .exit (1 )
107
117
118
+ # get arguments
108
119
lang_id = sys .argv [1 ]
109
120
is_check_mode = len (sys .argv ) > 2 and sys .argv [2 ] == '--check'
110
121
122
+ # setup XML parser
123
+ register_namespaces ()
124
+
125
+ # try to find XML files
111
126
target_file_path , ref_file_path = get_locale_files (lang_id )
112
127
128
+ # parse files
113
129
parser = ET .XMLParser (target = ET .TreeBuilder (insert_comments = True ))
114
130
target_tree = ET .parse (target_file_path , parser )
115
131
target_root = target_tree .getroot ()
116
-
132
+
117
133
ref_tree = ET .parse (ref_file_path )
118
134
ref_root = ref_tree .getroot ()
119
135
136
+ # get all translation keys
120
137
target_strings = get_strings (target_root )
121
138
ref_strings = get_strings (ref_root )
122
139
140
+ # compute the missing keys between the target and reference files
123
141
missing_keys = sorted ([key for key in ref_strings .keys () if key not in target_strings ])
124
142
125
143
if not missing_keys :
@@ -128,26 +146,34 @@ def main():
128
146
129
147
print (f"Found { len (missing_keys )} missing keys for language '{ lang_id } '." )
130
148
149
+ # running in check mode will only display the missing keys
131
150
if is_check_mode :
132
151
print ("Missing keys:" )
133
152
for key in missing_keys :
134
153
print (f" - { key } " )
135
154
return
136
155
156
+ # running in normal mode will trigger the translation process
137
157
print ("Starting interactive translation...\n " )
138
158
changes_made = False
139
159
try :
160
+ # for each missing key
140
161
for i , key in enumerate (missing_keys ):
162
+
163
+ # show the original text
141
164
original_text = ref_strings .get (key , "" )
142
165
print ("-" * 40 )
143
166
print (f"({ i + 1 } /{ len (missing_keys )} ) Key: '{ key } '" )
144
167
print (f"Original: '{ original_text } '" )
145
168
169
+ # asks for a translated version
146
170
user_input = input ("Enter translation (or press Enter to skip, 'q' to save and quit): " )
147
171
172
+ # if 'q' quit and save
148
173
if user_input .lower () == 'q' :
149
174
print ("\n Quitting and saving changes..." )
150
175
break
176
+ # if valid input, save
151
177
elif user_input :
152
178
add_new_string_tag (target_root , key , user_input )
153
179
changes_made = True
@@ -156,10 +182,11 @@ def main():
156
182
except (KeyboardInterrupt , EOFError ):
157
183
print ("\n \n Process interrupted. Saving changes..." )
158
184
finally :
185
+ # if there was any changes, save back to the target file
159
186
if changes_made :
160
187
save_translations (target_tree , target_file_path )
161
188
else :
162
189
print ("\n No changes were made." )
163
190
164
191
if __name__ == "__main__" :
165
- main ()
192
+ main ()
0 commit comments