1010# Variables that should be treated as numeric (using %d)
1111NUMERIC_VARIABLES = ['count' , 'found_count' , 'total_count' ]
1212
13- # Customizable mapping for output folder hierarchy
14- # Add entries here to customize the output path for specific locales
15- # Format: 'input_locale': 'output_path'
16- LOCALE_PATH_MAPPING = {
17- 'es-419' : 'b+es+419' ,
18- 'kmr-TR' : 'kmr' ,
19- 'hy-AM' : 'b+hy' ,
20- 'pt-BR' : 'b+pt+BR' ,
21- 'pt-PT' : 'b+pt+PT' ,
22- 'zh-CN' : 'b+zh+CN' ,
23- 'zh-TW' : 'b+zh+TW' ,
24- 'sr-CS' : 'b+sr+CS' ,
25- 'sr-SP' : 'b+sr+SP'
26- # Add more mappings as needed
27- }
2813
2914# Parse command-line arguments
3015parser = argparse .ArgumentParser (description = 'Convert a XLIFF translation files to Android XML.' )
@@ -119,7 +104,7 @@ def generate_android_xml(translations, app_name):
119104
120105 return result
121106
122- def convert_xliff_to_android_xml (input_file , output_dir , source_locale , locale , locale_two_letter_code , app_name ):
107+ def convert_xliff_to_android_xml (input_file , output_dir , source_locale , locale , app_name ):
123108 if not os .path .exists (input_file ):
124109 raise FileNotFoundError (f"Could not find '{ input_file } ' in raw translations directory" )
125110
@@ -128,14 +113,15 @@ def convert_xliff_to_android_xml(input_file, output_dir, source_locale, locale,
128113 translations = parse_xliff (input_file )
129114 output_data = generate_android_xml (translations , app_name if is_source_language else None )
130115
131- # Generate output files
132- output_locale = LOCALE_PATH_MAPPING . get ( locale , LOCALE_PATH_MAPPING . get ( locale_two_letter_code , locale_two_letter_code ))
133-
116+ # android is pretty smart to resolve resources for translations, see the example here:
117+ # https://developer.android.com/guide/topics/resources/multilingual-support#resource-resolution-examples
118+ android_safe_locale = f"b+ { locale . replace ( '-' , '+' ) } "
134119
120+ # Generate output files
135121 if is_source_language :
136122 language_output_dir = os .path .join (output_dir , 'values' )
137123 else :
138- language_output_dir = os .path .join (output_dir , f'values-{ output_locale } ' )
124+ language_output_dir = os .path .join (output_dir , f'values-{ android_safe_locale } ' )
139125
140126 os .makedirs (language_output_dir , exist_ok = True )
141127 language_output_file = os .path .join (language_output_dir , 'strings.xml' )
@@ -207,10 +193,13 @@ def convert_all_files(input_directory):
207193 source_locale = source_language ['locale' ]
208194 for language in [source_language ] + target_languages :
209195 lang_locale = language ['locale' ]
210- lang_two_letter_code = language ['twoLettersCode' ]
196+ if lang_locale == 'sh-HR' :
197+ # see https://en.wikipedia.org/wiki/Language_secessionism#In_Serbo-Croatian
198+ print (f"\033 [2K{ Fore .WHITE } ⏳ Skipping { lang_locale } as unsupported by android{ Style .RESET_ALL } " )
199+ continue
211200 print (f"\033 [2K{ Fore .WHITE } ⏳ Converting translations for { lang_locale } to target format...{ Style .RESET_ALL } " , end = '\r ' )
212201 input_file = os .path .join (input_directory , f"{ lang_locale } .xliff" )
213- convert_xliff_to_android_xml (input_file , TRANSLATIONS_OUTPUT_DIRECTORY , source_locale , lang_locale , lang_two_letter_code , app_name )
202+ convert_xliff_to_android_xml (input_file , TRANSLATIONS_OUTPUT_DIRECTORY , source_locale , lang_locale , app_name )
214203 print (f"\033 [2K{ Fore .GREEN } ✅ All conversions complete{ Style .RESET_ALL } " )
215204
216205if __name__ == "__main__" :
0 commit comments