@@ -117,6 +117,14 @@ def convert_placeholders_for_plurals(resname, translations):
117
117
118
118
return converted_translations
119
119
120
+ def sort_dict_case_insensitive (data ):
121
+ if isinstance (data , dict ):
122
+ return {k : sort_dict_case_insensitive (v ) for k , v in sorted (data .items (), key = lambda item : item [0 ].lower ())}
123
+ elif isinstance (data , list ):
124
+ return [sort_dict_case_insensitive (i ) for i in data ]
125
+ else :
126
+ return data
127
+
120
128
def convert_xliff_to_string_catalog (input_dir , output_dir , source_language , target_languages ):
121
129
string_catalog = {
122
130
"sourceLanguage" : "en" ,
@@ -144,9 +152,8 @@ def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, targ
144
152
raise ValueError (f"Error processing locale { lang_locale } : { str (e )} " )
145
153
146
154
print (f"\033 [2K{ Fore .WHITE } ⏳ Converting translations for { target_language } to target format...{ Style .RESET_ALL } " , end = '\r ' )
147
- sorted_translations = sorted (translations .items ())
148
-
149
- for resname , translation in sorted_translations :
155
+
156
+ for resname , translation in translations .items ():
150
157
if resname not in string_catalog ["strings" ]:
151
158
string_catalog ["strings" ][resname ] = {
152
159
"extractionState" : "manual" ,
@@ -204,13 +211,18 @@ def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, targ
204
211
}
205
212
}
206
213
214
+ # Note: Xcode sorts the strings in a case insensitive way so do the same here, apparently some versions of
215
+ # Python won't maintain insertion order once a dict is manipulated so we need to finalise the dict and then
216
+ # generate a correctly sorted one to be saved to disk
217
+ sorted_string_catalog = sort_dict_case_insensitive (string_catalog )
218
+
207
219
output_file = os .path .join (output_dir , 'Localizable.xcstrings' )
208
220
os .makedirs (output_dir , exist_ok = True )
209
221
210
222
with open (output_file , 'w' , encoding = 'utf-8' ) as f :
211
223
# We need to add spaces around the `:` in the output beacuse Xcode inserts one when opening
212
224
# the `xcstrings` so if we don't then there is an absurd number of diffs...
213
- json .dump (string_catalog , f , ensure_ascii = False , indent = 2 , separators = (',' , ' : ' ))
225
+ json .dump (sorted_string_catalog , f , ensure_ascii = False , indent = 2 , separators = (',' , ' : ' ))
214
226
215
227
def convert_non_translatable_strings_to_swift (input_file , output_path ):
216
228
if not os .path .exists (input_file ):
0 commit comments