Skip to content

Commit edcc64f

Browse files
committed
fix: replace hardcoded strings in exports for ios&android too
1 parent 687bfa4 commit edcc64f

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

crowdin/generate_android_strings.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def repl(match):
6868
return re.sub(r'\{([^}]+)\}', repl, text)
6969

7070

71-
def generate_android_xml(translations, app_name):
71+
def generate_android_xml(translations, app_name, glossary_dict):
7272
sorted_translations = sorted(translations.items())
7373
result = '<?xml version="1.0" encoding="utf-8"?>\n'
7474
result += '<resources>\n'
@@ -80,25 +80,26 @@ def generate_android_xml(translations, app_name):
8080
if isinstance(target, dict): # It's a plural group
8181
result += f' <plurals name="{resname}">\n'
8282
for form, value in target.items():
83-
escaped_value = clean_string(convert_placeholders(value), True, {}, {})
83+
escaped_value = clean_string(convert_placeholders(value), True, glossary_dict, {})
8484
result += f' <item quantity="{form}">{escaped_value}</item>\n'
8585
result += ' </plurals>\n'
8686
else: # It's a regular string (for these we DON'T want to convert the placeholders)
87-
escaped_target = clean_string(target, True, {}, {})
87+
escaped_target = clean_string(target, True, glossary_dict, {})
8888
result += f' <string name="{resname}">{escaped_target}</string>\n'
8989

9090
result += '</resources>'
9191

9292
return result
9393

94-
def convert_xliff_to_android_xml(input_file, output_dir, source_locale, locale, app_name):
94+
def convert_xliff_to_android_xml(input_file, output_dir, source_locale, locale, glossary_dict):
9595
if not os.path.exists(input_file):
9696
raise FileNotFoundError(f"Could not find '{input_file}' in raw translations directory")
9797

9898
# Parse the XLIFF and convert to XML (only include the 'app_name' entry in the source language)
9999
is_source_language = locale == source_locale
100100
translations = parse_xliff(input_file)
101-
output_data = generate_android_xml(translations, app_name if is_source_language else None)
101+
app_name = glossary_dict['app_name']
102+
output_data = generate_android_xml(translations, app_name if is_source_language else None, glossary_dict)
102103

103104
# android is pretty smart to resolve resources for translations, see the example here:
104105
# https://developer.android.com/guide/topics/resources/multilingual-support#resource-resolution-examples
@@ -141,14 +142,14 @@ def convert_non_translatable_strings_to_kotlin(input_file, output_path):
141142

142143
if not app_name:
143144
raise ValueError("could not find app_name in glossary_dict")
144-
return app_name
145145

146146
def convert_all_files(input_directory):
147147
setup_values = setup_generation(input_directory)
148148
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
149149

150-
app_name = convert_non_translatable_strings_to_kotlin(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH)
150+
convert_non_translatable_strings_to_kotlin(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH)
151151
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")
152+
glossary_dict = load_glossary_dict(non_translatable_strings_file)
152153

153154
# Convert the XLIFF data to the desired format
154155
print(f"\033[2K{Fore.WHITE}⏳ Converting translations to target format...{Style.RESET_ALL}", end='\r')
@@ -161,7 +162,7 @@ def convert_all_files(input_directory):
161162
continue
162163
print(f"\033[2K{Fore.WHITE}⏳ Converting translations for {lang_locale} to target format...{Style.RESET_ALL}", end='\r')
163164
input_file = os.path.join(input_directory, f"{lang_locale}.xliff")
164-
convert_xliff_to_android_xml(input_file, TRANSLATIONS_OUTPUT_DIRECTORY, source_locale, lang_locale, app_name)
165+
convert_xliff_to_android_xml(input_file, TRANSLATIONS_OUTPUT_DIRECTORY, source_locale, lang_locale, glossary_dict)
165166
print(f"\033[2K{Fore.GREEN}✅ All conversions complete{Style.RESET_ALL}")
166167

167168
if __name__ == "__main__":

crowdin/generate_ios_strings.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ def parse_xliff(file_path):
105105

106106
return translations, target_language
107107

108-
def convert_placeholders_for_plurals(translations):
108+
def convert_placeholders_for_plurals(translations, glossary_dict):
109109
# Replace {count} with %lld for iOS
110110
converted_translations = {}
111111
for form, value in translations.items():
112-
converted_translations[form] = clean_string(value, False, {}, {'{count}': '%lld'})
112+
converted_translations[form] = clean_string(value, False, glossary_dict, {'{count}': '%lld'})
113113

114114
return converted_translations
115115

@@ -121,7 +121,7 @@ def sort_dict_case_insensitive(data):
121121
else:
122122
return data
123123

124-
def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, target_languages):
124+
def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, target_languages, glossary_dict):
125125
string_catalog = {
126126
"sourceLanguage": "en",
127127
"strings": {},
@@ -157,7 +157,7 @@ def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, targ
157157
}
158158

159159
if isinstance(translation, dict): # It's a plural group
160-
converted_translations = convert_placeholders_for_plurals(translation)
160+
converted_translations = convert_placeholders_for_plurals(translation, glossary_dict)
161161

162162
# Check if any of the translations contain '{count}'
163163
contains_count = any('{count}' in value for value in translation.values())
@@ -203,7 +203,7 @@ def convert_xliff_to_string_catalog(input_dir, output_dir, source_language, targ
203203
string_catalog["strings"][resname]["localizations"][target_language] = {
204204
"stringUnit": {
205205
"state": "translated",
206-
"value": clean_string(translation, False, {}, {})
206+
"value": clean_string(translation, False, glossary_dict, {})
207207
}
208208
}
209209

@@ -242,14 +242,15 @@ def convert_non_translatable_strings_to_swift(input_file, output_path):
242242
def convert_all_files(input_directory):
243243
setup_values = setup_generation(input_directory)
244244
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
245+
glossary_dict = load_glossary_dict(non_translatable_strings_file)
245246
print(f"\033[2K{Fore.WHITE}⏳ Generating static strings file...{Style.RESET_ALL}", end='\r')
246247

247248
convert_non_translatable_strings_to_swift(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH)
248249
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")
249250

250251
# Convert the XLIFF data to the desired format
251252
print(f"\033[2K{Fore.WHITE}⏳ Converting translations to target format...{Style.RESET_ALL}", end='\r')
252-
convert_xliff_to_string_catalog(input_directory, TRANSLATIONS_OUTPUT_DIRECTORY, source_language, target_languages)
253+
convert_xliff_to_string_catalog(input_directory, TRANSLATIONS_OUTPUT_DIRECTORY, source_language, target_languages,glossary_dict)
253254
print(f"\033[2K{Fore.GREEN}✅ All conversions complete{Style.RESET_ALL}")
254255

255256
if __name__ == "__main__":

0 commit comments

Comments
 (0)