@@ -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
146146def 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
167168if __name__ == "__main__" :
0 commit comments