10
10
# Variables that should be treated as numeric (using %d)
11
11
NUMERIC_VARIABLES = ['count' , 'found_count' , 'total_count' ]
12
12
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
- }
28
13
29
14
# Parse command-line arguments
30
15
parser = argparse .ArgumentParser (description = 'Convert a XLIFF translation files to Android XML.' )
@@ -119,7 +104,7 @@ def generate_android_xml(translations, app_name):
119
104
120
105
return result
121
106
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 ):
123
108
if not os .path .exists (input_file ):
124
109
raise FileNotFoundError (f"Could not find '{ input_file } ' in raw translations directory" )
125
110
@@ -128,14 +113,15 @@ def convert_xliff_to_android_xml(input_file, output_dir, source_locale, locale,
128
113
translations = parse_xliff (input_file )
129
114
output_data = generate_android_xml (translations , app_name if is_source_language else None )
130
115
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 ( '-' , '+' ) } "
134
119
120
+ # Generate output files
135
121
if is_source_language :
136
122
language_output_dir = os .path .join (output_dir , 'values' )
137
123
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 } ' )
139
125
140
126
os .makedirs (language_output_dir , exist_ok = True )
141
127
language_output_file = os .path .join (language_output_dir , 'strings.xml' )
@@ -207,10 +193,13 @@ def convert_all_files(input_directory):
207
193
source_locale = source_language ['locale' ]
208
194
for language in [source_language ] + target_languages :
209
195
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
211
200
print (f"\033 [2K{ Fore .WHITE } ⏳ Converting translations for { lang_locale } to target format...{ Style .RESET_ALL } " , end = '\r ' )
212
201
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 )
214
203
print (f"\033 [2K{ Fore .GREEN } ✅ All conversions complete{ Style .RESET_ALL } " )
215
204
216
205
if __name__ == "__main__" :
0 commit comments