@@ -68,7 +68,7 @@ def repl(match):
68
68
return re .sub (r'\{([^}]+)\}' , repl , text )
69
69
70
70
71
- def generate_android_xml (translations , app_name ):
71
+ def generate_android_xml (translations , app_name , glossary_dict ):
72
72
sorted_translations = sorted (translations .items ())
73
73
result = '<?xml version="1.0" encoding="utf-8"?>\n '
74
74
result += '<resources>\n '
@@ -80,25 +80,26 @@ def generate_android_xml(translations, app_name):
80
80
if isinstance (target , dict ): # It's a plural group
81
81
result += f' <plurals name="{ resname } ">\n '
82
82
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 , {})
84
84
result += f' <item quantity="{ form } ">{ escaped_value } </item>\n '
85
85
result += ' </plurals>\n '
86
86
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 , {})
88
88
result += f' <string name="{ resname } ">{ escaped_target } </string>\n '
89
89
90
90
result += '</resources>'
91
91
92
92
return result
93
93
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 ):
95
95
if not os .path .exists (input_file ):
96
96
raise FileNotFoundError (f"Could not find '{ input_file } ' in raw translations directory" )
97
97
98
98
# Parse the XLIFF and convert to XML (only include the 'app_name' entry in the source language)
99
99
is_source_language = locale == source_locale
100
100
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 )
102
103
103
104
# android is pretty smart to resolve resources for translations, see the example here:
104
105
# 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):
141
142
142
143
if not app_name :
143
144
raise ValueError ("could not find app_name in glossary_dict" )
144
- return app_name
145
145
146
146
def convert_all_files (input_directory ):
147
147
setup_values = setup_generation (input_directory )
148
148
source_language , rtl_languages , non_translatable_strings_file , target_languages = setup_values .values ()
149
149
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 )
151
151
print (f"\033 [2K{ Fore .GREEN } ✅ Static string generation complete{ Style .RESET_ALL } " )
152
+ glossary_dict = load_glossary_dict (non_translatable_strings_file )
152
153
153
154
# Convert the XLIFF data to the desired format
154
155
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):
161
162
continue
162
163
print (f"\033 [2K{ Fore .WHITE } ⏳ Converting translations for { lang_locale } to target format...{ Style .RESET_ALL } " , end = '\r ' )
163
164
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 )
165
166
print (f"\033 [2K{ Fore .GREEN } ✅ All conversions complete{ Style .RESET_ALL } " )
166
167
167
168
if __name__ == "__main__" :
0 commit comments