Skip to content

Commit f703481

Browse files
authored
Merge pull request #34 from oxen-io/fix/more-broken-ios-sorting
Reworked iOS JSON sorting as it was still busted
2 parents 2946ff3 + a4671f2 commit f703481

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

crowdin/generate_ios_strings.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ def convert_placeholders_for_plurals(resname, translations):
117117

118118
return converted_translations
119119

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+
120128
def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, target_languages):
121129
string_catalog = {
122130
"sourceLanguage": "en",
@@ -144,9 +152,8 @@ def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, targ
144152
raise ValueError(f"Error processing locale {lang_locale}: {str(e)}")
145153

146154
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():
150157
if resname not in string_catalog["strings"]:
151158
string_catalog["strings"][resname] = {
152159
"extractionState": "manual",
@@ -204,13 +211,18 @@ def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, targ
204211
}
205212
}
206213

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+
207219
output_file = os.path.join(output_dir, 'Localizable.xcstrings')
208220
os.makedirs(output_dir, exist_ok=True)
209221

210222
with open(output_file, 'w', encoding='utf-8') as f:
211223
# We need to add spaces around the `:` in the output beacuse Xcode inserts one when opening
212224
# 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=(',', ' : '))
214226

215227
def convert_non_translatable_strings_to_swift(input_file, output_path):
216228
if not os.path.exists(input_file):

0 commit comments

Comments
 (0)